Can't access env variables in RUN script.sh Dockerfile - bash

I have this Dockerfile:
FROM php:5.6-apache
WORKDIR /var/www/html/
# ENV VARIABLES
ENV INI_FOLDER /usr/local/etc/php
ENV WWW_FOLDER /var/www
# ADD THE ENV CONFIGURATOR AND SET PERMISSIONS
ADD env.sh $WWW_FOLDER/
RUN chmod +x $WWW_FOLDER/env.sh
RUN /var/www/env.sh
The problems is that in env.sh I don't have access to the variables set on the docker-compose. Is there any workaround to fix this?
UPDATE: added docker-compose
version: '2.0'
services:
app:
env_file:
- app/mysql.env
- app/app.env
volumes:
- C:\Users\svirl\Documents\workspace\docker\my-app:/var/www/html/:rw
build: app

Related

Mounting Volume in DockerFile on Windows

I am working on a Springboot project with docker. I tried to mount volume so I could have access to generated files from the Springboot application in my local directory. The data is generated in the docker container but I can not find it the local directory.
I have read many topics but none seems to be helpful.
Please, I am still new to docker and would appreciate suggestions to assist.
I have tried to mount the volume directly in the dockerfile as there is a docker compose file to run the service alongside others. Below is what I have in my Dockerfile and docker-compose
Dockerfile
FROM iron/java:1.8
EXPOSE 8080
ENV USER_NAME myprofile
ENV APP_HOME /home/$USER_NAME/app
#Test Script>>>>>>>>>>>>>>>>>>>>>>
#Modifiable
ENV SQL_SCRIPT $APP_HOME/SCRIPTS_TO_RUN
ENV SQL_OUTPUT_FILE $SQL_SCRIPT/data
ENV NO_OF_USERS 3
ENV RANGE_OF_SKILLS "1-4"
ENV HOST_PATH C:"/Users/user1/IdeaProjects/path/logs"
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
RUN adduser -S $USER_NAME
RUN mkdir $APP_HOME
RUN mkdir $SQL_SCRIPT
RUN chown $USER_NAME $SQL_SCRIPT
VOLUME $HOST_PATH: $SQL_SCRIPT
ADD myprofile-*.jar $APP_HOME/myprofile.jar
RUN chown $USER_NAME $APP_HOME/myprofile.jar
USER $USER_NAME
WORKDIR $APP_HOME
RUN sh -c 'touch myprofile.jar'
ENTRYPOINT ["sh", "-c","java -Djava.security.egd=file:/dev/./urandom -jar myprofile.jar -o $SQL_OUTPUT_FILE -n $NO_OF_USERS -r $RANGE_OF_SKILLS"]
Docker-compose
myprofile-backend:
extra_hosts:
- remotehost
container_name: samplecontainer-name
image: sampleimagename
links:
- rabbitmq
- db:redis
expose:
- "8080"
ports:
- "8082:8080"
volumes:
- ./logs/:/tmp/logs
- ./logs/:/app
The problem here is that you are mounting the same folder ./logs twice. Docker-compose volume mount syntax is - <your-host-path>:<your-container-path>. Also, its better to use relative paths when you are building the application. So change docker-compose file to (assuming you want to see the files in ./target relative to the Dockerfile:
myprofile-backend:
extra_hosts:
- remotehost
container_name: samplecontainer-name
image: sampleimagename
links:
- rabbitmq
- db:redis
expose:
- "8080"
ports:
- "8082:8080"
volumes:
- ./logs/:/tmp/logs
- ./target/:/app

User in docker can't create folders and files

I have a problem with creating folders and files within a docker container. I have a Ruby and Hanami web app deployed using Dockerfile and docker-compose yaml. Here is the content of both files for reference.
Dockerfile:
FROM ruby:2.7.5-bullseye
RUN apt-get update && apt-get install vim -y
RUN bundle config --global frozen 1
RUN adduser --disabled-login app_owner
USER app_owner
WORKDIR /usr/src/app
COPY --chown=app_owner Gemfile Gemfile.lock ./
COPY --chown=app_owner . ./
RUN gem install bundler:1.17.3
RUN bundle install
ENV HANAMI_HOST=0.0.0.0
ENV HANAMI_ENV=production
EXPOSE 2300
docker-compose.yml:
version: '3'
services:
postgres:
image: postgres
restart: unless-stopped
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
ports:
- 5432
volumes:
- postgres_staging:/var/lib/postgresql/data
web:
build: .
restart: unless-stopped
command: >
bash -c "bundle exec hanami db migrate
&& bundle exec rake initial_settings:add_default_language
&& bundle exec rake initial_settings:add_session_validity
&& bundle exec rake import_user:create
&& bundle exec rake super_admin:create
&& bundle exec rake create_parser_rules:start
&& bundle exec hanami assets precompile
&& cp -r apps/myapp/assets/webfonts public/webfonts
&& cp -r apps/myapp/assets/webfonts public/assets/webfonts
&& cp -r apps/myapp/assets/images/sort*.png public/assets
&& cp -r apps/myapp/assets/images/sort*.png public
&& cp -r apps/myapp/assets/images/ui-icons*.png public/assets/wordrocket
&& mkdir public/assets/images
&& cp -r apps/myapp/assets/images/sort*.png public/assets/images
&& bundle exec hanami server"
volumes:
- ./hanami_log:/usr/src/app/hanami_log
links:
- postgres
depends_on:
- postgres
nginx:
image: nginx:alpine
restart: unless-stopped
tty: true
ports:
- "${NGINX_PORT}:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- web
volumes:
postgres_staging:
web:
nginx:
The docker-compose has some initial commands that need to be run and the ones creating folders and files are failing. For example bundle exec hanami assets precompile fails with: Permission denied # dir_s_mkdir - /usr/src/app/public
The command created the folder and then copies over some files. I've confirmed this error by omitting the command from docker-compose and then tried running the command manually in the running container. I get the same error.
Is my configuration incorrect?
EDIT: Forgot one important thing: the problem occurs on the client's server running Ubuntu 20.04, while it works without issues on my dev laptop.
Thank you.
Seba

docker-compose does not create none-existing variable in .env file

This is a setup for laravel project:
Dockerfile:
FROM php:8.1.2-cli-buster
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN apt-get update && apt install git -y
WORKDIR /var/www/html
COPY . .
RUN composer install --no-dev
RUN mv ./.env.example .env
RUN php artisan key:generate
CMD ["/bin/bash", "-c", "php artisan serve --host 0.0.0.0 --port 8000"]
Look at this variable inside docker-compse.yml
version: "3.8"
services:
artisan:
build: .
environment:
CUSTOM_VAR: custom-value-from-compse
ports:
- '8000:8000'
api.php
Route::get('test', function () {
dump(env('CUSTOM_VAR')); //CUSTOM_VAR is null
});
The above route should dump custom-value-from-compse value but it dumpnull, What is the problem here ? It only overide existing variable in .env file, I mean if i set CUSTOM_VAR in .env file to 'some-value' it does not override it to the value inside of the docker compose
Note: the CUSTOM_VAR wil be null even i put it in Dockerfile...
Maybe try this:
version: "3.8"
services:
artisan:
build: .
environment:
# laravel uses Dotenv to set and load environment variables,by default it will not
# overwrite existing environment variables. So we set env variables for app container here
# then laravel will use these env values instead of the same env variables defined in .env file.
- "CUSTOM_VAR=custom-value-from-compse"
ports:
- '8000:8000'
Source: https://gist.github.com/kevinyan815/fa0760902d29f19a4213b4a16fe0501b#file-docker-compose-yml-for-laravel-L11
You can set default values for variables in docker-compose file. If variables in .env file doesn't exist the default value will be set, otherwise it will be override it to the value inside of the docker compose.
version: "3.8"
services:
artisan:
build: .
environment:
- CUSTOM_VAR=${CUSTOM_VAR_FROM_ENV_FILE:-custom-value-from-compse}
https://docs.docker.com/compose/environment-variables/

Docker-compose custom .env file unexpected behaviour

Example
Consider this example docker-compose file with custom .env file:
version: '3'
services:
service_example:
build:
dockerfile: Dockerfile
context: .
args:
AAA: ${AAA}
command: python3 src/service/run.py
env_file:
- custom_env.env
custom_env.env:
AAA=qqq
When I run docker-compose config I get the following output:
WARNING: The AAA variable is not set. Defaulting to a blank string.
services:
service_example:
build:
args:
AAA: '' <----------------------------- ??????
context: /Users/examples
dockerfile: Dockerfile
command: python3 src/service/run.py
environment:
AAA: qqq
version: '3'
Question
Why AAA is unset in build section?
What should I do to set it properly (to the value provided from custom file: AAA=qqq)?
I've also noticed that if I change the env file name to the default setting mv custom_env.env .env and remove env_file section from docker-compose.yml - everything will be just fine:
services:
service_example:
build:
args:
AAA: qqq
context: /Users/examples
dockerfile: Dockerfile
command: python3 src/service/run.py
version: '3'
Quick Answer
docker-compose --env-file custom_env.env config
Answers Explanation
Question-1: Why AAA is unset in build section?
Because the env file specified in env_file property custom_env.env is specific for the Container only, i.e. those variables are to be passed to container while running not during image build.
Question-2: What should I do to set it properly (to the value provided from custom file: AAA=qqq)?
To provide environment variables for build step in docker-compose file using custom env file, we need to specify the custom file path. Like
Syntax: docker-compose --env-file FILE_PATH config
Example: docker-compose --env-file custom_env.env config
Question-3: How .env works?
Because that is the default file for which docker-compose looks for.
Summary
So, In docker-compose for current scenario we can consider 2 stages for specifying env
Build Stage(Image)
Running Stage(Container)
For Build stage - we can use .env default file or we can use --env-file option to specify custom env file
For Running Stage - we can specify environment variables using environment: property or we can use env_file: property to specify a env file
References
https://docs.docker.com/compose/env-file/
https://docs.docker.com/compose/environment-variables/
https://docs.docker.com/compose/compose-file/#env_file
https://docs.docker.com/compose/compose-file/#environment

Docker compose on Windows: no such file or directory

I'm trying to run two services with docker compose: a MySQL server and a SAMP server.
My docker-compose.yml looks like this:
version: '3.9'
services:
mysql:
image: mysql:8.0.28
volumes:
- ./mysql:/var/nw-mysql-data
env_file:
- .env
samp:
build:
context: .
dockerfile: Dockerfile
command: ./samp03svr
ports:
- "7777:7777"
env_file:
- .env
depends_on:
- mysql
And my Dockerfile:
FROM ubuntu:20.04
RUN mkdir /app
COPY . /app/
WORKDIR /app
COPY ./samp03svr /app/
The problem is in the command of the samp service. It throws the following exception when I run docker-compose up:
standard_init_linux.go:228: exec user process caused: no such file or directory
The weird thing, is that if I change the samp's command for an ls like this:
command: ls
I can see the binary file that I'm trying to execute listed, and docker is saying that there's no such file...
Help?
EDIT: Here is a screenshot of the ls command return, ran from the command instruction of the samp service:

Resources