Django's runserver relies on a working database connection during startup. If this is not the case runserver fails entirely and the container has to be restarted.
To circumvent that, the runserver call should be wrapped in a function that checks if the postgres database is up and running.
This only affects the development environment. Production is fine.
There's an example on Docker Compose docs of a container waiting for postgres to start, that can be useful: https://docs.docker.com/compose/startup-order/
This sounds like something that could be solved with modifying the command in the docker-compose file.
https://github.com/vishnubob/wait-for-it
Maybe placing this file (wait-for-it.sh) in the utilities directory could be useful? The script waits for the host and port to spin up (with a timeout) before exiting. The command in docker-compose would then be changed to the following:
command: bash -c "./utilities/wait-for-it.sh postgres:5432 && python /app/manage.py runserver_plus 0.0.0.0:8000"
Most helpful comment
This sounds like something that could be solved with modifying the command in the docker-compose file.
https://github.com/vishnubob/wait-for-it
Maybe placing this file (
wait-for-it.sh) in theutilitiesdirectory could be useful? The script waits for the host and port to spin up (with a timeout) before exiting. The command in docker-compose would then be changed to the following: