compose: env variables from .env file are not set in docker compose file
ENVIRONMENT VARIABLES from .env
are setting to empty string in docker compose file
Context information (for bug reports)
Output of docker-compose version
docker-compose version 1.23.2, build 1110ad01
docker-py version: 3.6.0
CPython version: 3.6.6
OpenSSL version: OpenSSL 1.1.0h 27 Mar 2018
Output of docker version
Client: Docker Engine - Community
Version: 18.09.2
API version: 1.39
Go version: go1.10.8
Git commit: 6247962
Built: Sun Feb 10 04:12:39 2019
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.2
API version: 1.39 (minimum version 1.12)
Go version: go1.10.6
Git commit: 6247962
Built: Sun Feb 10 04:13:06 2019
OS/Arch: linux/amd64
Experimental: true
Output of docker-compose config
(Make sure to add the relevant -f
and other flags)
WARNING: The POSTGRES_VERSION variable is not set. Defaulting to a blank string.
WARNING: The POSTGRES_USER variable is not set. Defaulting to a blank string.
WARNING: The POSTGRES_PASSWORD variable is not set. Defaulting to a blank string.
WARNING: The POSTGRES_DB variable is not set. Defaulting to a blank string.
services:
testpg:
environment:
POSTGRES_DB: ''
POSTGRES_PASSWORD: ''
POSTGRES_USER: ''
POSTGRES_VERSION: '9.6'
expose:
- 5432
image: 'testpgimage:'
ports:
- published: 5432
target: 5432
version: '3.7'
Steps to reproduce the issue
postgreServer-test.yml
# To execute this docker-compose yml file use docker-compose -f <file_name> up
# Add the "-d" flag at the end for detached execution
version: "3.7"
services:
testpg:
env_file:
- POSTGRES_SERVER.env
image: testpgimage:${POSTGRES_VERSION}
ports:
- 5432:5432
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
expose:
- 5432
POSTGRES_SERVER.env
POSTGRES_VERSION=9.6
POSTGRES_USER=testuser
POSTGRES_PASSWORD=testPWD
POSTGRES_DB=testdb
Expected result
services:
testpg:
environment:
POSTGRES_VERSION='9.6'
POSTGRES_USER='testuser'
POSTGRES_PASSWORD='testPWD'
POSTGRES_DB='testdb'
expose:
- 5432
image: 'testpgimage:9.6'
ports:
- published: 5432
target: 5432
version: '3.7'
Additional information
OS version / distribution, docker-compose
install method, etc.
Mac OS 10.14.3, docker installed as desktop
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 22
- Comments: 23
There’s an obvious confusion here between:
.env
file which is used for variable substitution in the docker-compose.yaml file, and should resolve when you rundocker-compose config
env_file
element in a service definition, which is just setting the environment file to send to docker engine when starting container, as a definition of runtime environment. Compose will not parse this file which is opaque to him.So there’s nothing wrong here.
the question that i’d have to y’all is from which working directory are invoking Compose. because i encounter the same when i have one of the subfolders beside of a Compose project as a working directory.
Thanks for replying @ps23. Can I also know which OS you ran on… I’m running in Mac OS 10.14.3
tried above with
.env
file which resides in the same folder as.yml
file, but still not populatingdocker-compose -f postgreServer-test.yml config
+1
LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: Fedora Description: Fedora release 29 (Twenty Nine) Release: 29 Codename: TwentyNine
Docker version 18.09.5, build e8ff056
docker-compose version 1.22.0, build f46880f
it is run with
however
WARNING: The VALUE variable is not set. Defaulting to a blank string. services: cli: command: echo -e “VALUE= VALUE=” environment: VALUE: ‘’ image: debian:stretch-slim version: '3.7
The following pulls the environment variables into the container that is created. It does NOT substitute the placeholders in the yml file.
Steps to correct your approach
postgreServer-test.yml
.env
docker-compose -f postgreServer-test.yml config
Maybe this sounds stupid and its obvious. But i had this similar issue until i sorted the variables in the .env file and docker-compose.yml in the same order.
I saw this diff on last version if this may help.
I’ve 2 same docker compose-files
compose content:
I’ve following env file:
– the following didn’t work:
this works:
or
So why is
docker-compose
not sourcing the.env
files relatively to the passeddocker-compose.yml
? The commandenv $(cat <PATH>/.env | xargs)
does its job but it makes the script less slim and has an imho unnecessary repetition in it, e.g.:compared to
I know I can also do
but that feels even worse 😞
Hmm I had similar issue, as it turned out I called
docker-compose up
from the service directory 😅 so I guess you should call it from thedocker-compose.yml
directory levelNevermind, this one did the trick:
eval $(egrep -v '^#' ./db/.env | xargs) docker-compose config
Thanks!
I have similar issue. The difference for me is that
docker-compose --file docker-compose.yml up
does not work either. But theworks.
The package versions are:
OS:
@ps23 Thanks for your reply!
However, it seems compose can’t read the env variables set. I created a run.sh file at the root of my project and the result is this:
and this is the output:
The variable content is printed, but it’s not passed down to compose. I’m running this in a mac os, BTW.
@mcnesium Because you can pass in multiple yml files from different locations on a single compose command