Search code examples
postgresqldockerdocker-composenetbox

Docker Compose - Container Bash Forking


I am trying to run netbox based on their standard guide on Docker Hub with a slight difference that I need our existing postgres dump to be restored when the postgres container starts.

I have tried a few approaches like defining a command option in docker-compose file like (and a few more combinations):

sleep 2 && psql -U netbox -f netbox.sql

sleep is required to prevent psql command running before the postgres service is started.

Or defining a bash script that does the database restore but all these approaches cause the container to exit after that command/script is run.

My last resort was to utilize bash forking and this is what the postgres snippet of docker-compose looks like:

postgres:
  image: postgres:13-alpine
  env_file: env/postgres.env
  command:
    - sh
    - -c
    - (sleep 3 && cd /home && psql -U netbox -f netbox.sql) & su -c postgres postgres
  volumes:
    - ./my_db:/home/
    - netbox-postgres-data:/var/lib/postgresql/data

Sadly this throws results in:

postgres: could not access the server configuration file
"/var/lib/postgresql/data/postgresql.conf": No such file or directory

If I omit the command section of docker-compose, the container starts up fine and I can navigate and ls the directory in the error message but it is not what I really need because this container will go on to be part of a much larger jungle of an ecosystem with little to no control over it afterwards.

Could it be my bash forking or the problem lies somewhere else?

Thanks in advance


Solution

  • I was able to find a solution by going through the thread that David Maze shared in the comments.

    In my case, placing the *.sql file inside /docker-entrypoint-initdb.d did not work but I wrote a bash script, placed it in /docker-entrypoint-initdb.d directory and it got triggered.

    The bash script was a very simple one, it would cd to the directory containing the sql dump and then restore it by running psql:

    psql -U netbox -f netbox.sql