0

Trying to set up a Django multicontainer application for local development purposes. I haven't found a way to customize pg_hba.conf and postgresql.conf directly from the compose file. I don't want to rely on external scripts or even external files to be copied to the container since the pg_hba.conf and postgresql.conf are rather simple and additional files would add clutter in the project directory. Having a second Dockerfile for the database also is not an optimal solution for me since, as stated, I just need a few lines to be configured/uncommented on those target files.

Below, the current configuration for my compose file.

version: '3.8'

services:

#-------------------------------------------------------#
  # postgres 
  suapdb:
    hostname: 'suapdb'
    ports:
      - "5432:5432"
    environment: 
      POSTGRES_USER: postgres
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_PASSWORD: '' 
      POSTGRES_DB: suap_dev
      PGDATA: /var/lib/postgresql/data/
    networks:
      - suap_dev_net
    volumes:
      - postgres:/var/lib/postgresql/data
    command: >
      ash -c "echo host all all 0.0.0.0/0 trust > /var/lib/postgresql/data/pg_hba.conf &&
             echo local all postgres trust >> /var/lib/postgresql/data/pg_hba.conf &&
             echo local all all ident >> /var/lib/postgresql/data/pg_hba.conf &&
             echo listen_addresses = \'*\' >> /var/lib/postgresql/data/postgresql.conf"
    image: postgres:13-alpine

The commented lines on the suapdb service are the ones I hoped would work out (as I have seen in other posts) but it hasn't.

Any contribution appreciated.

João Victor
  • 65
  • 1
  • 1
  • 5
  • "Having a second Dockerfile for the database also is not an optimal solution for me since, as stated, I just need a few lines to be configured/uncommented on those target files." that's the wrong criterion for deciding when you need a separate docker file. Actually it is an optimal solution for several reasons: you can rebuild app without restarting db; and instead of re-engineering https://hub.docker.com/_/postgres you can just use that which has all this figured out for you – erik258 Oct 09 '22 at 12:38
  • @erik258 Thanks for your input. What I tried to explain is that I only need those lines (commented lines on the code) added to those target files and nothing else will be done to the database at that point. – João Victor Oct 09 '22 at 12:40
  • So, what happens when you do it? – jjanes Oct 09 '22 at 14:41
  • @jjanes Basically, when I run docker compose up, the suapdb container simple exits with code 0 and the files untouched. Weird that I have seen that code in other threads but it is not working on this specific case. – João Victor Oct 09 '22 at 16:19
  • I get syntax errors because the quoting in the command is wrong, errors about networks and volumes not being created, and errors about directories not existing. – jjanes Oct 09 '22 at 20:37
  • Changed the code in the topic fixing quoting and isolated the database container. Still no progress. The container and volume are created but exited abruptly. When the code is commented out, the container runs just fine. output: `exited with code 0` – João Victor Oct 10 '22 at 13:28

0 Answers0