Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose and postgres official image environment variables

Actually I'm using the following docker-compose.yml file

version: '3.3'

  services:
    postgres:
      container_name: postgres
      image: postgres:latest
      restart: always
      environment:
        POSTGRES_USER: ${POSTGRES_USER}
        POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
        POSTGRES_DB: ${POSTGRES_DB}
        PGDATA: /var/lib/postgresql/data/pgdata
      ports:
        - "5432:5432"
      volumes:
        - ./data/postgres/pgdata:/var/lib/postgresql/data/pgdata

I use also this .env file in the same directory of the docker-compose.yml file:

POSTGRES_USER=dbadm
POSTGRES_PASSWORD=dbpwd
POSTGRES_DB=db

Then I run a bash shell into container this way:

docker exec -ti postgres bash

And after this invoke the command:

psql -h postgres -U dbadm db

And I get the error:

psql: FATAL:  password authentication failed for user "dbadm"

The strange fact is that if use the default image parameters:

psql -h postgres -U admin database

And insert the default password "password", it logs me in, and seems it's ignoring the environment variables.

What am I missing?

Additional logs from docker-compose up logs:

postgres    | 2017-10-15 09:19:15.502 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres    | 2017-10-15 09:19:15.502 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres    | 2017-10-15 09:19:15.505 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres    | 2017-10-15 09:19:15.524 UTC [22] LOG:  database system was shut down at 2017-10-15 09:02:21 UTC
postgres    | 2017-10-15 09:19:15.530 UTC [1] LOG:  database system is ready to accept connections

Cannot see any "RUN" line about user, database and password setup.

like image 596
Michele Carino Avatar asked Oct 15 '17 08:10

Michele Carino


People also ask

How do I pass an environment variable in docker run?

Using –env, -e When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.

Can Dockerfile access environment variables?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

What is Postgres_password?

POSTGRES_PASSWORD. This environment variable is required for you to use the PostgreSQL image. It must not be empty or undefined. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable.


2 Answers

I have created a docker-compose yml and .env file with the details you've provided and everything works fine as you can see from the pictures below:

enter image description here enter image description here

I think your problem lies when you are passing the -h parameter.

Inside the container will always be localhost however, outside you will have to pass:

hostname: postgres

inside your docker-compose file so it will have the postgres hostname

like image 137
Sergiu Avatar answered Sep 28 '22 08:09

Sergiu


according to https://hub.docker.com/_/postgres documentation.

Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.

like image 45
kris mwas Avatar answered Sep 28 '22 10:09

kris mwas