1

Set a postgres docker config file as

Dockerfile

FROM postgres:12-alpine

USER postgres

RUN chmod 0700 /var/lib/postgresql/data &&\
    initdb /var/lib/postgresql/data &&\
    echo "host all  all    0.0.0.0/0  md5" >> /var/lib/postgresql/data/pg_hba.conf &&\
    echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf
    pg_ctl start &&\
    psql -c "ALTER USER postgres WITH ENCRYPTED PASSWORD 'mypassword';"

EXPOSE 5432

Deployed it on a server with docker-compose

version: "3.8"
services:
  postgresql:
    build:
      context: ./postgresql
      dockerfile: Dockerfile
    image: postgresql
    container_name: postgresql
    hostname: postgresql

When build it, it can successful finished. But when up it, got

docker-compose up postgresql
Creating postgresql ... done
Attaching to postgresql
postgresql       | Error: Database is uninitialized and superuser password is not specified.
postgresql       |        You must specify POSTGRES_PASSWORD to a non-empty value for the
postgresql       |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgresql       |
postgresql       |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgresql       |        connections without a password. This is *not* recommended.
postgresql       |
postgresql       |        See PostgreSQL documentation about "trust":
postgresql       |        https://www.postgresql.org/docs/current/auth-trust.html
postgresql exited with code 1

Check docker container ls -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
1lsls201qs0h        postgresql          "docker-entrypoint.s…"   2 minutes ago       Exited (1) 2 minutes ago                       postgresql

The file permission on the server

ls -la postgresql/
Total 4
drwxr-xr-x 2 runuser runuser  24  April 14 01:02 .
drwxr-xr-x 5 runuser runuser  54  April 14 01:02 ..
-rwxr-xr-x 1 runuser runuser 541  April 14 01:02 Dockerfile

ls -la docker-compose.yaml
-rwxr-xr-x 1 runuser runuser 1110  April 14 01:02 docker-compose.yaml

I tested it on local, it works. The container can run correctly and the initial password setting also works. But on the server, is there any permission should be changed?

0

1 Answer 1

5

You don't need to create image by modifiying postgress dockerfile.Just download the image and run the image. write your own separate pg_hba.conf and postgresql.conf and saved it to a host directory. Now mount that directory at container runtime.check the volume and add this to docker compose.

volume:
- host_directory_that_contains_conf/pg_hba.conf: /var/lib/postgresql/data/pg_hba.conf
- host_directory_that_contains_conf/postgresql.conf: /var/lib/postgresql/data/postgresql.conf

Add this volume in your docker-compose and run the docker compose up. There is a another alternative solutions which is using env variable. You can add all of this in ENVIRONMENT variable.

Sign up to request clarification or add additional context in comments.

1 Comment

do you have example how to allow remote connections with environment variables?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.