问题
Looking at a PostGIS Docker image, the instructions indicate to use via the following docker run command:
docker run --name some-postgis -e POSTGRES_PASSWORD=mysecretpassword -d -P mdillon/postgis
Specifically, when running this command I see that the -e flag is used (i.e. that the environment variable POSTGRES_PASSWORD is set. And I can test that I cannot login to the PostgreSQL server unless I specify that password.
I'm trying to run this Docker image using docker-compose, but I'm finding that the environment variable is NOT being set correctly:
version: '3.7'
volumes:
postgres:
services:
postgres:
image: mdillon/postgis
container_name: postgres
restart: always
environment:
- POSTGRES_PASSWORD=mysecretpassword
ports:
- 5432:5432
volumes:
- postgres:/var/lib/postgresql/data
Ans then I run the container: docker-compose up -d --force-recreate --build. I find that I can access the PostGIS database WITHOUT a password. This is the equivalent of running the Docker image without the -e flag: docker run --name some-postgis -P -d mdillon/postgis
Looking at another StackOverflow question: docker-compose environment not the same as Docker -e, I then looked at what environment variables are being created when I run docker-compose. Running:
docker-compose exec some-postgis sh, and then
env
I get the following:
...
POSTGRES_PASSWORD=mysecretpassword
...
So the POSTGRES_PASSWORD is being set. What are the reasons that a Docker container created by docker run treats environment variables differently to containers created via docker-compose.
来源:https://stackoverflow.com/questions/58394801/why-are-docker-compose-environment-variables-treated-differently-to-environmen