The documentation is slightly ambiguous about what happens when you specify environment variables using both env_file
and environment
. It _does_ say "Environment variables specified in environment override these values" but what about when environment doesn't specify the values like so:
environment:
POSTGRES_PASSWORD:
POSTGRES_USER:
env_file: .env
It'd be nice to have POSTGRES_PASSWORD
and POSTGRES_USER
in .env
and have them in docker-compose.yml
at the same time. Basically telling people, "hey, you need to provide these values" either in .env
or in the shell environment you run docker-compose up
in.
If that's not possible or would only add confusion, then it'd be cool to add that the override happens _even when_ they are blank and read from the shell environment.
@ferrouswheel Sorry for off topic, but have you actually got POSTGRES_PASSWORD and POSTGRES_USER to work? If I specify those variable with -e to docker run everything works fine, but using docker-compose cannot connect
@cressie176 I have. I use it for my open source docker-based CI tool. See the docker-compose.yml there and the README.md.
As I remember it, the postgres container sets the user/password the _first time you run it_, so if you run it once with no credentials, it'll use the defaults the next time your start the container. Try removing the container completely and recreating it with the credentials you want to use.
Makes sense. Thanks
@ferrouswheel so, this issue is resolved, or did I interpret your previous comment incorrectly?
@thaJeztah No the issue still stands. My comment was just a reply to the unrelated question.
+1, just got bit by this (even the specific case of POSTGRES_PASSWORD!).
I provided POSTGRES_PASSWORD
as an unset value in environment
, and POSTGRES_PASSWORD=foo
in .env
. Compose used the empty value in my environment instead of the value in .env
.
Building on this (can open another issue, but compose already has a few too many of those...), there should be a way to require strict checking of all (or a subset of) environment variables, so that containers will fail to start if those variables are not set.
The simplest thing that could work is adding a new key called environment_strict
.
Doesn't the extends
solve this?
https://docs.docker.com/compose/extends/#define-the-production-environment
I'm not sure if here or I must open a new issue, but.. when I use _environment_ values are parsing correct, but if I use _env_file_ with exactly the same values, container doesn't works well.
The error can be reproduced for example with this:
db:
image: mdillon/postgis
env_file: env.conf
and this in env.conf:
POSTGRES_USER: user
POSTGRES_PASSWORD: secret
POSTGRES_DB: base
Then, no error appears, but user and database are not created. If I put them inside compose file with _environment_ it works well.
docker-compose version 1.5.2
thanks in advance !
I don't think that's a valid env_file
. It should be POSTGRES_USER=user
, etc. https://docs.docker.com/compose/compose-file/#env-file
@lupa18 how did you solve?
@shin- I'm not seeing the env_file respected.
.env.development
file:
POSTGRES_USER=root
POSTGRES_PASSWORD=webapp
POSTGRES_DB=webapp
docker-compose.yml
file:
db:
env_file: .env.${NODE_ENV}
environment:
- POSTGRES_USER=${POSTGRES_USER:?}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:?}
- POSTGRES_DB=${POSTGRES_DB:?}
image: postgres:12-alpine
container_name: ${POSTGRES_DB:?}-db
volumes:
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./pg-data:/var/lib/postgresql/data
ports:
- 5432:5432
restart: unless-stopped
$ NODE_ENV=development NAME=webapp VERSION=1.1.0 docker-compose up --force-recreate --always-recreate-deps --renew-anon-volumes --remove-orphans --detach --build db
ERROR: Missing mandatory value for "environment" option in service "db":
Weird thing is this happens only with postgres image, because the other image in the yml is built correctly using the envs.
I've this config that works, you have defined your environment variables as array is the issue:
version: '3'
services:
app-dev:
env_file: ./scripts/env-${NODE_ENV}.conf
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
image: postgres:12-alpine
container_name: "${POSTGRES_DB}-db"
restart: always
volumes:
- ./pg-data:/var/lib/postgresql/data
command: postgres -c config_file=/etc/postgresql/postgresql.conf
ports:
- 6432:5432
networks:
- dbnet
networks:
dbnet:
driver: bridge
POSTGRES_PASSWORD=user321
POSTGRES_USER=user
POSTGRES_DB=app
export NODE_ENV=dev; export NAME=app_db;export VERSION=1.1.0;docker-compose up -d
Most helpful comment
@ferrouswheel Sorry for off topic, but have you actually got POSTGRES_PASSWORD and POSTGRES_USER to work? If I specify those variable with -e to docker run everything works fine, but using docker-compose cannot connect