Docker-compose environment variables Docker-compose environment variables docker docker

Docker-compose environment variables


I struggled with this for a while and wasn't having luck with the accepted answer, I finally got it to work by removing the container:

docker-compose rm postgres

And then the volume as well:

docker volume rm myapp_postgres

Then when I did a fresh docker-compose up I saw CREATE ROLE fly by, which I'm assuming is what was missed on the initial up.


The reasons for this are elaborated on here, on the Git repo for the Docker official image for postgres.


If you're using Docker.

Try to check if your local DB is active because mostly it's conflicting with Docker, if so, you can deactivate it or change port number or uninstall it in order to avoid the conflict.


The authentication error you got would help a lot!

I fired up the postgres image with your arguments:

docker run --name db -d -e POSTGRES_PASSWORD=docker -e POSTGRES_USER=docker postgres

Then I exec'ed in :

docker exec -it db psql -U docker userpsql: FATAL:  database "user" does not exist

I get the error message you are expecting because I have trust authentication :

docker exec -it db cat /var/lib/postgresql/data/pg_hba.conf | grep -v '^#'local   all             all                                     trusthost    all             all             127.0.0.1/32            trusthost    all             all             ::1/128                 trusthost all all 0.0.0.0/0 md5

To simulate your web container, I'll run another instance of the postgres container and link the db container and then connect back to the db container:

core@ku1 /tmp/i $ docker run --rm --name web --link db:db -it postgres psql -h db -Udocker userPassword for user docker: psql: FATAL:  password authentication failed for user "docker"

I get an authentication error if I enter the incorrect password. But, if I enter the correct password:

core@ku1 /tmp/i $ docker run --rm --name web --link db:db -it postgres psql -h db -Udocker userPassword for user docker: psql: FATAL:  database "user" does not exist

It all seems to be working correctly. I put it all in a yaml file and tested it that way as well:

web:  image: postgres  command: sleep 999  ports:    - "62576:62576"  links:   - dbdb:  image: postgres  environment:    POSTGRES_PASSWORD: docker    POSTGRES_USER: docker

then fired it up with docker-compose:

core@ku1 /tmp/i $ docker-compose -f dc.yaml upCreating i_db_1...Creating i_web_1...Attaching to i_db_1, i_web_1db_1  | okdb_1  | creating template1 database in /var/lib/postgresql/data/base/1 ... okdb_1  | initializing pg_authid ... okdb_1  | initializing dependencies ... okdb_1  | creating system views ... okdb_1  | loading system objects' descriptions ... okdb_1  | creating collations ... okdb_1  | creating conversions ... okdb_1  | creating dictionaries ... okdb_1  | setting privileges on built-in objects ... okdb_1  | creating information schema ... okdb_1  | loading PL/pgSQL server-side language ... okdb_1  | vacuuming database template1 ... okdb_1  | copying template1 to template0 ... okdb_1  | copying template1 to postgres ... okdb_1  | syncing data to disk ... okdb_1  | db_1  | WARNING: enabling "trust" authentication for local connectionsdb_1  | You can change this by editing pg_hba.conf or using the option -A, ordb_1  | --auth-local and --auth-host, the next time you run initdb.db_1  | db_1  | Success. You can now start the database server using:db_1  | db_1  |     postgres -D /var/lib/postgresql/datadb_1  | ordb_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile startdb_1  | db_1  | db_1  | PostgreSQL stand-alone backend 9.4.1db_1  | backend> statement: CREATE DATABASE "docker" ;db_1  | db_1  | backend> db_1  | db_1  | PostgreSQL stand-alone backend 9.4.1db_1  | backend> statement: CREATE USER "docker" WITH SUPERUSER PASSWORD 'docker' ;db_1  | db_1  | backend> db_1  | LOG:  database system was shut down at 2015-04-12 22:01:12 UTCdb_1  | LOG:  database system is ready to accept connectionsdb_1  | LOG:  autovacuum launcher started^Z[1]+  Stopped                 docker-compose -f dc.yaml upcore@ku1 /tmp/i $ bg

you can see that the user and password were created. I exec in:

core@ku1 /tmp/i $ docker exec -it i_web_1 psql -Udocker -h db userPassword for user docker: psql: FATAL:  password authentication failed for user "docker"core@ku1 /tmp/i $db_1  | FATAL:  password authentication failed for user "docker"db_1  | DETAIL:  Connection matched pg_hba.conf line 95: "host all all 0.0.0.0/0 md5"core@ku1 /tmp/i $ docker exec -it i_web_1 psql -Udocker -h db userPassword for user docker: psql: FATAL:  database "user" does not existdb_1  | FATAL:  database "user" does not exist

So the only thing I can think of is that you are trying to connect to the database from your host, not the web container? Or your web container is not using the 'db' as the host to connect to? Your definition for the web container does not contain any errors that I can see.