docker-library / postgres

Docker Official Image packaging for Postgres
http://www.postgresql.org
MIT License
2.16k stars 1.13k forks source link

Container won’t start if POSTGRES_PASSWORD is set via --env-file but not declared in your YAML file #1093

Closed alfonsomunozpomer closed 1 year ago

alfonsomunozpomer commented 1 year ago

docker-compose.yml:

version: '3.6'

services:
  db:
    image: postgres
``

`dev.env`:
```yaml
POSTGRES_PASSWORD=foobar
$ docker compose --env-file dev.env up
[+] Running 2/2
 ✔ Network dc_default  Created                                                                                                 0.1s 
 ✔ Container dc-db-1   Created                                                                                                 0.1s 
Attaching to dc-db-1
dc-db-1  | Error: Database is uninitialized and superuser password is not specified.
dc-db-1  |        You must specify POSTGRES_PASSWORD to a non-empty value for the
dc-db-1  |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
dc-db-1  | 
dc-db-1  |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
dc-db-1  |        connections without a password. This is *not* recommended.
dc-db-1  | 
dc-db-1  |        See PostgreSQL documentation about "trust":
dc-db-1  |        https://www.postgresql.org/docs/current/auth-trust.html
dc-db-1 exited with code 1

This can be fixed by declaring (but not setting) POSTGRES_PASSWORD. I’ve raised an issue in the Docker Compose GitHub repo because this is due to --env-file not behaving exactly the same as having an .env file or using env_file. I don’t know if this is though by design and maybe Postgres images could do something to fix it.

yosifkit commented 1 year ago

There is nothing the PostgreSQL image/container can do with values not passed to it.

I would guess that compose is making the variables explicit to limit the scope of which containers have access to which values (and so users can map them to different names for each container).