Waiting for containers in Docker Compose

Using the dockerize tool to wait for containers to startup in Docker Compose

When I first started using Docker Compose I attempted to use the depends_on functionality thinking that it would properly wait for other containers to start. After reading the documentation further I found this this was not the case and I was left wondering how to orchestrate the startup order of services that depends on other containers.

Dockerize

After looking at a number of scripts I settled on using jwilder/Dockerize due to it being east to install in containers and its syntax. It allows you to wait for services based on a number of different protocols which helps when you need to wait for servers, databases, or even files to be available.

Using Dockerize

The README of jwilder/Dockerize has just about everything you need to get started. It even has its own Docker container that you can extend from. I prefer to simply include the following in my test containers.

ENV DOCKERIZE_VERSION v0.6.1
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

With this in my test container I can then utilize the dockerize command to wait for services. If I want to wait for an http service I might do something like the following.

dockerize -wait tcp://api-server:8080 -timeout 1m

When I want to wait on a database container such as mysql I use the following command.

dockerize -wait tcp://mysql:3306 -timeout 1m

Note the hostnames I use in the above commands are the names I specify when I setup the containers within the Docker Compose configuration files.

Subscribe to zsiegel.com

Don’t miss out on the latest articles. Sign up now to get exclusive members only content and early access to articles.
jamie@example.com
Subscribe