Docker Compose .env File Not Read - docker

I have a .env file I'm trying to use in a Docker Compose file to pass to the container to be used in the entrypoint script defined in the Dockerfile.
I have the following effective Dockerfile (spread out over two files, one override). The .env.dev file is in the same directory as the docker compose files. The environment variable value is not getting passed to the container. When I add "=${RUN_MIGRATIONS_ON_START}", the variable value is blank. If I leave that off, then the variables aren't even set in the container.
Docker compose files:
Main docker compose file:
version: '3.4'
services:
web:
build
context: .
Override docker compose file:
version: '3.4'
services:
web:
environment:
- RUN_MIGRATIONS_ON_START=${RUN_MIGRATIONS_ON_START}
- WS_SCHEME=${WS_SCHEME}
env_file:
- .env.dev

Solution
docker-compose.override.yml
version: '3.4'
services:
web:
env_file:
- .env.dev
.env.dev
RUN_MIGRATIONS_ON_START=FOO
WS_SCHEME=BAR
Why
environment:
- X=${Y} # Y is a variable from the local shell environment, not from .env
Described in detail in documentation
Your configuration options can contain environment variables. Compose
uses the variable values from the shell environment in which
docker-compose is run.

Related

Injecting environment variable to docker containers using docker-compose

Is there a way to inject environment variables to all of my docker-compose services, without explicitly declaring them in each service's configuration?
1)using the env_file directive
you can use the env_file directive within your docker-compose.yml file
You can pass multiple environment variables from an external file
through to a service’s containers with the ‘env_file’ option, just
like with docker run --env-file=FILE ...:
you will have to declare the env file for each service that will use it and that is it.
example :
the docker-compose.yml file :
version: "3"
services:
database:
image: "ubuntu"
tty: true
env_file:
- same-variables.env
web:
image: "ubuntu"
tty: true
env_file:
- same-variables.env
the same-variables.env file
IS_DOCKER_COMPOSE=yes
then if you do opening a terminal :
docker exec -it <docker_container> "echo $IS_DOCKER_COMPOSE"
result will be :
yes
2)using the .env file in project root
According to the doc: https://docs.docker.com/compose/environment-variables/#the-env-file
You can set default values for any environment variables referenced in
the Compose file, or used to configure Compose, in an environment file
named .env. The .env file path is as follows
Create an .env file as the root of your project as follow.
example :
your .env file :
TAG=v1.5
your docker-compose.yml file :
version: '3'
services:
web:
image: "webapp:$TAG"
organization of your project should be :
root_folder
|-.env
|-docker-compose.yaml
with the .env file with all your variable
the env file will work for all of them at the same time

Docker: Why does my project have a .env file?

I'm working on a group project involving Docker that has a .env file, which looks like this:
DATABASE_URL=xxx
DJANGO_SETTINGS_MODULE=xxx
SECRET_KEY=xxx
Couldn't this just be declared inside the Dockerfile? If so, what is the advantage of making a .env file?
Not sure if I'm going in the right direction with this, but this Docker Docs page says (emphasis my own):
Your configuration options can contain environment variables. Compose
uses the variable values from the shell environment in which
docker-compose is run. For example, suppose the shell contains
POSTGRES_VERSION=9.3 and you supply this configuration:
db:
`image: "postgres:${POSTGRES_VERSION}"`
When you run docker-compose up with this configuration, Compose looks for the POSTGRES_VERSION environment variable in the shell and substitutes its value in. For this example, Compose resolves the image to postgres:9.3 before running the configuration.
If an environment variable is not set, Compose substitutes with an empty string. In the example above, if POSTGRES_VERSION is not set, the value for the image option is postgres:.
You can set default values for environment variables using a .env file, which Compose automatically looks for. Values set in the shell environment override those set in the .env file.
If we're using a .env file, then wouldn't I see some ${...} syntax in our docker-compose.yml file? I don't see anything like that, though.
Here's our docker-compose.yml file:
version: '3'
services:
server:
build:
context: ./server
dockerfile: Dockerfile
env_file: .env.dev
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- ./server:/app
ports:
- "8500:8000"
depends_on:
- db
stdin_open: true
tty: true
db:
image: postgres
client:
build:
context: ./client
dockerfile: Dockerfile
command: bash -c "npm install; npm run start"
volumes:
- ./client:/app
- /app/node_modules
ports:
- "3000:3000"
depends_on:
- server
Idea there is probably to have a place to keep secrets separated from docker-compose.yml, which you then can keep in VCS and/or share.

env_file is ignored in docker-compose and cause "The ... variable is not set" warning

I simply want to use a environment variable loaded from file in my docker-compose file. But after running the container, I only got
WARNING: The TESTVAR variable is not set. Defaulting to a blank string.
Only found this topic, but I'm using a later version of docker like there (docker-compose: 1.14.0, docker: 17.05.0-ce). And I changed the encoding to ISO 8859-1, since I found a github issue where strange behavior with encodings was detected. Both doesn't work.
My docker-compose file
version: '2'
services:
mysql:
container_name: test_${TESTVAR}
build: mysql
mem_limit: 1G
env_file:
- credentials.env
credentials.env contains only TESTVAR=test123. To start, I run docker-compose up mysql and I also tried to specify the environment variables directly in the compose file like this:
environment:
- TESTVAR=1234
Not working, too.
If you want to use variables in the docker-compose.yml you can do it with .env file, docker docs
$ cat .env
TAG=v1.5
TESTVAR=123
$ cat docker-compose.yml
version: '3'
services:
web:
image: "webapp:${TAG}"
environment: ["TESTVAR=${TESTVAR}"]

How to pass variables from a .env file to Docker?

I have a .env file with a large list of variables in the root of my node project. In the root is also my docker-compose.yml file:
version: '3'
services:
api:
build: .
env_file:
- .env
ports:
- 5000:5000
I'm trying to pass in these variables to Docker (from here), but when I run docker-compose up, I'm running into an error in my node app because it doesn't have access to these variables. What am I doing wrong? Thanks.
Change the file name to something different like node.env. The .env file is used by the docker-compose itself to define the content of the docker-compose file itself.
Example:
$ cat .env
TAG=v1.5
$ cat docker-compose.yml
version: '3'
services:
web:
image: "webapp:${TAG}"
The content of this file is not used inside the docker instance itself.

Set extra host in environment variables

I'm using docker compose to run my application. And for do that I need to set the hosts inside container (it's depends on the environment i'm running).
My approach was:
Create an environment file and set the variable:
#application.env
SERVER_IP=10.10.9.134
My docker compose file looks like:
version: '2'
services:
api:
container_name: myApplication
env_file:
- application.env
build: ./myApplication/
entrypoint: ./docker/api-startup.sh
ports:
- "8080:8080"
depends_on:
- redis
extra_hosts: &extra_hosts
myip: $SERVER_IP
But my problem is that the variable SERVER_IP is never replaced.
When I run docker-compose config I see:
services:
api:
build:
context: /...../myApplication
container_name: myApplication
depends_on:
- redis
entrypoint: ./docker/api-startup.sh
environment:
SERVER_IP: 10.10.9.134
extra_hosts:
myip: ''
ports:
- 8080:8080
I've tried to replace the variable reference using $SERVER_IP or ${SERVER_IP} but it didn't work.
I created a file .env, added single line HOST=test.example.com, then did this in docker-compose:
extra_hosts:
- myip:${HOST}
docker-compose config then shows
extra_hosts:
myip: test.example.com
To do this I followed the documentation from Docker-compose environment variables the section about .env file
UPDATE
According to the Docker documentation,
Note: If your service specifies a build option, variables defined in
environment files will not be automatically visible during the build.
Use the args sub-option of build to define build-time environment
variables.
It basically means if you place your variables in .env file, you can use them for substitution in docker-compose.yml, but if you use env_file option for the particular container, you can only see the variables inside the Docker container, not during the build. It is also logical, env_file replaces docker run --env-file=FILE ... and nothing else.
So, you can only place your values into .env. Alternatively, as William described, you can use host's environment variables.
EDIT
Try the following:
version: '2'
services:
api:
container_name: myApplication
env_file:
- application.env
build: ./myApplication/
entrypoint: ./docker/api-startup.sh
ports:
- "8080:8080"
depends_on:
- redis
extra_hosts:
- "myip:${SERVER_IP}"
Ensure curly bracers and that the environment variable exists on the host os.

Resources