Overview
This is a memo on how to control the startup order of containers in Docker Compose using Dockerize.
github.com - jwilder/dockerize
Why Use Dockerize
Instead of Dockerize, you could also use a pure bash script method like wait-for-it.
cf. Docker-docs-ja - Controlling Startup Order in Compose
The purpose of using Dockerize is to intentionally control the startup order of multiple containers.
For example, if you have an application container and a test database container, and the application container needs to perform tests using the database, the database container must start before the application container.
In short, the goal is to resolve the dependency of startup order between containers.
Docker Compose provides options like depends_on and links. However, depends_on only controls the order in which containers are created, and links adds name resolution between containers in addition to the functionality of depends_on. Neither of these options controls the actual startup order.
As I later found out, links is automatically executed from version 2 onwards and has become somewhat legacy.
Example
Here’s an example from one of my applications.
docker-compose.yml
The gobel_app container needs to wait for the gobel_test_db to start. The entrypoint specifies the Dockerize command.
Here is the Dockerfile used to build the gobel_app container:
This includes the installation of Dockerize.
Thoughts
It’s easy to implement. While it might be feasible to prepare your own script like wait-for-it, considering the potential complexity of container management, using Dockerize seems like a reasonable choice.