Can't replace pg_hba.conf when create my custom container

I need the next:

 1. Create custom image base on image postgres.9.6
    2. Create custom container from my custom image
    3. Replace original file pg_hba.conf by my custom file

The difference between this to files is only in this line:

In origal pg_hba.conf file:

host all all 127.0.0.1/0 trust

In my pg_hba.conf file.

host all all 0.0.0.0/0 trust

Here my Dockerfile.

FROM postgres:9.6.24
    
    ENV POSTGRES_HOST_AUTH_METHOD=trust
    
    # Create folder Downloads in Docker
    WORKDIR /Downloads
    
    COPY /plv8_v.2.x ./Downloads
    
    RUN dpkg -i Downloads/plv8-96_2.1.0-2_amd64.deb
    RUN dpkg -i Downloads/v8_3.14.5.10-26_amd64.deb
    
    COPY /postgresql /usr/share/postgresql/9.6/extension/
    
    # Overwrite file pg_hba.conf to avoid password prompt
    COPY pg_hba.conf /var/lib/postgresql/data/

Create my custom image by this command:

`docker build -t my_image .`

Image success created. Nice.

Create my custom container from my custom imeage

docker run --name my_container -d -p 127.0.0.1:5432:5432 my_image

But the container is not started.
If I comment this line

COPY pg_hba.conf /var/lib/postgresql/data/

the problem is gone. Then the container is success start.
I want to avoid of PostgreSQLā€™s password prompt. Thatā€™s why I want to replace pg_hba.conf

So I need to replace pg_hba.conf. So how I can do this?

Is it a task at school? If you are just practicing thatā€™s fine, otherwise I answered in the other topic how you should trust (usually you should not) in all hosts

Since I gave you the wrong reason why the changes were lost, I updated my answer.

An error message (docker logs CONTAINERNAME) and the whole content of the config would be helpful, but I guess you copied the file with wrong permissions. Make sure the postgresql user can read and write it but nobody else.

8.0K -rw-------  1 postgres postgres 4.5K Feb 15 19:06 pg_hba.conf

I found solution.
Not need to copy pg_hba.conf to Docker container. Itā€™s enought to use

environment virable:

ENV POSTGRES_HOST_AUTH_METHOD=trust

So here result Dockerfile:

FROM postgres:9.6.24
    
    ENV POSTGRES_HOST_AUTH_METHOD=trust
    
    # Create folder Downloads in Docker
    WORKDIR /Downloads
    
    COPY /plv8_v.2.x ./Downloads
    
    RUN dpkg -i Downloads/plv8-96_2.1.0-2_amd64.deb
    RUN dpkg -i Downloads/v8_3.14.5.10-26_amd64.deb
    
    COPY /postgresql /usr/share/postgresql/9.6/extension/

And now itā€™s work. Itā€™s not ask me about PostgreSQL password anymore.

Yes, This is what I have been trying to tell you from the beginning :slight_smile:

2 Likes