0

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

PoJam
  • 127
  • 6
  • 1
    Have you tried putting the dump in the container's `/docker-entrypoint-initdb.d` directory, and not trying to manually load it yourself, as in [How to create User/Database in script for Docker Postgres](https://stackoverflow.com/questions/26598738/how-to-create-user-database-in-script-for-docker-postgres/26599273#26599273)? – David Maze Dec 04 '21 at 21:53
  • Thanks for the link @DavidMaze, I will give it a shot in the morning. However, from the comments there, it seems not many had success with it. fingers crossed. – PoJam Dec 04 '21 at 23:54
  • ok, the link you shared led me to solving the problem. Thanks a lot! – PoJam Dec 05 '21 at 14:54

1 Answers1

0

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
PoJam
  • 127
  • 6