New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using env_file with environment #1171
Comments
@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 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 |
Doesn't the |
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.
and this in env.conf:
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 |
Closing: docker/docs#4177 |
@lupa18 how did you solve? @shin- I'm not seeing the env_file respected.
POSTGRES_USER=root
POSTGRES_PASSWORD=webapp
POSTGRES_DB=webapp
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
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: compose-fileversion: '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 scripts/env-dev.conf
export NODE_ENV=dev; export NAME=app_db;export VERSION=1.1.0;docker-compose up -d |
envrionments attribute is a mapping not a list See caused confusion docker/compose#1171 (comment)
I hit the same issue, and the above solution doesn't work. The variable definition from the default I just cannot see this behavior described in the docs.
Sounds much like the |
The above recommended by @rajeshhazari does not work. Slightly simplified to avoid needing to set any variables in your shell compose.yaml
env.conf
output of
|
I believe the answer is as always in documentation. But two things are happening here. https://docs.docker.com/compose/environment-variables/envvars-precedence/ First, those variables, as they are not defined in your shell ENV are just replaced by blanks. Effectively overriding your env.conf, higher precedence. Try to set one of your VARIABLES in shell, eg. The '.env' file is really treated in a special way with docker compose and is, AFAIK only source used to interpolate compose.yml file. https://docs.docker.com/compose/environment-variables/set-environment-variables/
|
The documentation is slightly ambiguous about what happens when you specify environment variables using both
env_file
andenvironment
. It does say "Environment variables specified in environment override these values" but what about when environment doesn't specify the values like so:It'd be nice to have
POSTGRES_PASSWORD
andPOSTGRES_USER
in.env
and have them indocker-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 rundocker-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.
The text was updated successfully, but these errors were encountered: