Docker compose start containers after host reboot best practice


I have a host works as gitlab runner to build projects with docker
and I built a mariadb container to serve all of my projects which they built as containers.

yesterday docker daemon stop working properly and I restart it.

when docker daemon trying to start, it starts to run the containers randomly but all of them need database container to start first to so they can connect to it and start.

docker daemon looped until I delete all containers first then I recreate mariadb container and run it
and rebuild all my projects again one by one.

how can I implement a solution to avoid this problem, is there any priority number could I set to each container to run them on specific order.

starting first my host containers environment (mariadb, swag, etc…) then docker runs the projects containers next.

I hope my explanation the problem was understandable to you.

Thanks in advance.

Use depends_on? (Doc)

There is no container start order. You could use depends_on if all containers are in the same compose project, but I doubt this will apply on later docker engine starts as well.

The correct solution would be to either make your containers wait in their entrypoint script until the database is available, or let them die if the database is not available, so they can retry after the next start. Of course the first approach is more reliable and has less impact on the host ressources.

Kubernetes allows to configure init containers that are executed before the main container(s) start. Sadly docker doesn’t have such thing, and you need to implement such a behavior in the containers entrypoint script.