Home > Back-end >  Trying to override pg_hba.conf in docker
Trying to override pg_hba.conf in docker

Time:02-03

I'm using the lastest Docker PostgreSQL 14.1 image and trying to change the file pg_hba.conf to use md5 (Dev machine):

# host all all all scram-sha-256
host all all all md5

I have a modified pg_hba.conf file locally that I'm trying to use. In my Dockerfile I do the following:

RUN rm -f /var/lib/postgresql/data/pg_hba.conf
COPY ./postgresql/pg_hba.conf /var/lib/postgresql/data/pg_hba.conf

where ./postgresql/pg_hba.conf is the modified version that allows the use of md5

When I run docker-compose build everything seems to be working as expected.

Step 5/7 : RUN rm -f /var/lib/postgresql/data/pg_hba.conf
---> Using cache
---> 20e82128335a
Step 6/7 : COPY ./postgresql/pg_hba.conf /var/lib/postgresql/data/pg_hba.conf
---> Using cache
---> cc41cb9267d8
Step 7/7 : CMD ["tail", "-F", "-n0", "/etc/hosts"]
---> Using cache
---> 9cb31d31a1a3
Successfully built 9cb31d31a1a3
Successfully tagged fileshare_db:latest

Currently, I'm not running the database and just using the following so I can keep the container without turning on the database so I can see if the commands work:

CMD ["tail", "-F", "-n0", "/etc/hosts"]
# CMD ["postgres"]

But when I go into the container and do a cat command on pg_hba.conf it still has the old settings

# TYPE  DATABASE        USER            ADDRESS                 METHOD
...
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

host all all all scram-sha-256

Here is part of my compose file that is specific to the postgresl

    db:
    container_name: db
    restart: always
    build:
        context: .
        dockerfile: ./Dockerfile.postgres
    ports:
        - "5432:5432"
    networks:
        - fileshare-network
    volumes:
        - db-data:/var/lib/postgresql/data
        - ../../backups/database:/var/backups

...
volumes:
    db-data:
        driver: "local"

Update For now, I'm just copying the file manually. I put it in /var/backups and overwrite the file in /var/lib/postgresql/data and then restart to get it to work.

The one positive is I only have to do this once.

CodePudding user response:

Try to change your Dockefile:

ENV POSTGRES_HOST_AUTH_METHOD=md5

Or docker-compose.yaml:

db:
  environment:
    POSTGRES_HOST_AUTH_METHOD: md5
    PGDATA=/var/lib/postgresql/data/pgdata

And you can delete these lines:

RUN rm -f /var/lib/postgresql/data/pg_hba.conf
COPY ./postgresql/pg_hba.conf /var/lib/postgresql/data/pg_hba.conf
  •  Tags:  
  • Related