Assign value of environment variable to another variable in .env file - bash

I define a variable in an .env file:
LOCAL_IP=127.0.0.1
When using this in the following docker-compose.yml I can see that the variable is expanded to 127.0.0.1 by running docker-compose config.
docker-compose.yml
services:
my-service:
environment:
# This gets expanded to http://127.0.0.1/services
SERVICE_ENDPOINT: http://${LOCAL_IP}/services
Now I'd like to reference the value of LOCAL_IP in another variable in the same file like:
LOCAL_IP=127.0.0.1
SOME_OTHER_VAR=$LOCAL_IP
Updated docker-compose.yml
services:
my-service:
environment:
SERVICE_ENDPOINT: http://${SOME_OTHER_VAR}/services
Running docker-compose config then gives me:
services:
my-service:
environment:
# I'd have expected http://127.0.0.1/services
SERVICE_ENDPOINT: http://$$LOCAL_IP:8090/services

Related

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

Environment variable from docker-compose not being passed to container

I am trying to set an environment variable in my docker-compose file and it is not able to be accessed when running the container.
version: "2"
services:
newman_runner:
build:
context: https://github.com/postmanlabs/newman/tree/develop/docker/images/alpine
args:
NEWMAN_VERSION: 4.6.0
image: postman/newman:alpine
environment:
- FOO=BOO
volumes:
- ./postman:/etc/newman
entrypoint: echo ${FOO}
# command:
# run /etc/newman/Google_Civic_Information_API.postman_collection.json
# --env-var API_KEY=${API_KEY}
FOO comes back as Empty and I get the following WARNING after running docker-compose.exe up
WARNING: The FOO variable is not set. Defaulting to a blank string.
I've tried to set FOO as both an array and a dictionary with similar effects.
FOO: BOO vs - FOO=BOO
Any ideas?
I'm on windows at the moment if that makes a difference.
What ended up working was this:
version: "2"
services:
newman_runner:
build:
context: https://github.com/postmanlabs/newman/tree/develop/docker/images/alpine
args:
NEWMAN_VERSION: 4.6.0
image: postman/newman:alpine
environment:
- FOO
From there I can export FOO before running docker-compose
export FOO=BOO
docker-compose up

docker-compose.yml file errors on running docker-compose up

Here is my docker-compose.yml file:
version:'2':
services:
redis:
image: redis
environment:
- HOST='localhost'
- PORT=6379
ports:
-"0.0.0.0:${PORT}:6379"
I get this error on running docker-compose up:
ERROR: The Compose file './docker-compose.yml' is invalid because:
Invalid service name 'services' - only [a-zA-Z0-9\._\-] characters are allowed
Unsupported config option for services: 'redis'
There are multiple problems with your file. The one causing the syntax error is that you have an extra colon on the first line:
version:'2':
that way you define a scalar string key version:'2' with value of null. Since you are therefore not defining the version of the docker compose file, the rest of the file (which is version 2 oriented) fails. This is best resolved by adding a space after version:
In addition your ports definition is incorrect, the value for that should be a sequence/list, and you again specify a scalar string -"0.0.0.0:${PORT}:6379" because there is no space after the initial dash.
Change your docker_compose.yaml file to:
version: '2'
# ^ no colon here
# ^ space here
services:
redis:
image: redis
environment:
- HOST='localhost'
- PORT=6379
ports:
- "0.0.0.0:${PORT}:6379"
# ^ extra space here
just remove last character ":" into string version:'2':
after it docker-compose.yml must be like
version:'2'
services:
redis:
image: redis
environment:
- HOST='localhost'
- PORT=6379
ports:
-"0.0.0.0:${PORT}:6379"

Pass multiple system parameters thorugh docker-compose-->dockerfile--> springboot application housed in docker container

Below is the content of my docker-compose.yml file:
eureka-server:
image: controlsplm/eureka-server
environment:
HOST_IP: X.X.X.X
ACTIVE_PROFILE=docker-development-cloud
ports:
- "8761:8761"
restart: always
And below is the content of my docker file:
FROM java:8
VOLUME /tmp
ADD eureka-server-0.1.0-SNAPSHOT.jar app.jar
EXPOSE 8761
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Denvironment=$HOST_IP","-Dspring.profiles.active=$ACTIVE_PROFILE","-jar","/app.jar"]
But when i run the docker container using compose, HOST_IP is picked up but not ACTIVE_PROFILE. AM i missing anything here? Kindly help...
Indeed, if you have Spring properties
my.spring.property.one=green
my.spring.property.two=blue
You can include them as follows in the docker-compose.yml:
environment:
- my_spring_property_one=green
- my_spring_property_two=blue
worked for my with
Spring Boot v1.5.10.RELEASE
Spring v4.3.14.RELEASE
docker version '3.6'
I see two problems:
the format of the ACTIVE_PROFILE is wrong, it would be ACTIVE_PROFILE: docker-development-cloud as mentioned by #andreas-jägle in the comments
The entrypoint uses json array notation, which means the command is exec'ed directly without a shell. The shell (usually bash) is what replaced the variables with their values, so you need to run in a shell to use those variables. You can either use the string form of ENTRYPOINT, or use:
ENTRYPOINT ["bash", "-c" ,"java - Dspring.profiles.active=$ACTIVE_PROFILE ..."]
I resolved the issue by adding below lines to my docker-compose.yml file:
eureka-server:
image: controlsplm/eureka-server:latest
environment:
HOST_IP: X.X.X.X
SPRING_PROFILES_ACTIVE: docker-development-cloud
ports:
- "8761:8761"
restart: always
and below lines in dockerfile:
FROM java:8
VOLUME /tmp
ADD eureka-server-0.1.0-SNAPSHOT.jar app.jar
EXPOSE 8761
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Denvironment=$HOST_IP","-jar","/app.jar"]

Resources