Why are docker-compose “environment” variables treated differently to environment variables defined via the -e flag using docker run?

…衆ロ難τιáo~ 提交于 2019-12-08 01:03:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!