Docker compose wait don’t run it now

One of the main problem you will face when you work with Docker is that you are not 100% sure that the service on the ImageX is ready for you to use it or even for ImageY to communicate with it.

For example, lets say that you have the ImageX which run MySQL/MariaDB (or any other DB), and ImageY which run PHP, no matter how you try you cant be sure that ImageX is ready to accept connections from ImageY and CRUD some data from it, so Docker docs stated that you should find some solution for that, and they do provide some simple instruction for it on Controlling startup order in Compose.

For me I like to use Dockerize and find it too much simple to deal with, so I’ll explain how do I build my images using it (not all just the one which I know they need to use it).

So the installation and the usage is simple, for the installation you should add the following to your Dockerfile:

The latest version from Dockeize is 0.3.0 at the day of writing this article, so remember to change the version based on what the developer has released, you can check the release list from github repo at this link. There is also a Docker image ( jwilder/dockerize ) which you can use, but be careful its based on alpine linux.

The usage within Docker compose is simple, all you have to do is understand what is the services that you are waiting for to be ready then execute the command you want, so the entrypoint in your docker-compose file should follow the following format

and yes as you are thinking dockerize can be in the entrypoint inside the Dockerfile no need for it to be in docker-compose file, but it might be tricky to deal with it, so I like to overwrite the entrypoint from Dockerfile within docker-compose just in case I need to change a parameter, this way I will not need to rebuild my image.

so an example of real usage for it like this :

What am telling dockerize is that I want to wait till the kong-database container to be ready before I can start kong cause if kong started before the database it will just fail and crash.

so as stated in dockerize documentation

Dockerize gives you the ability to wait for services on a specified protocol (tcp, tcp4, tcp6, http, https and unix) before starting your application.

so feel free to use any of the protocols you want.

You can optionally specify how long to wait for the services to become available by using the -timeout # argument (Default: 10 seconds). If the timeout is reached and the service is still not available, the process exits with status code 1. You may not need more than 10second for the other container to finish, but sometime ( especially if you are executing big SQL statements with too many records to import ), 10s may not be good for you, in one of my Docker images I had to change this to 80s, so play with it carefully.

The options which dockerize provide is great for you, but you will need to learn about them and how to use them so I do encourage you to read the documentation, and play with the options to find the best that suited your apps.

As always, you may find some lingual mistakes in my post, as english is not my mother language, so if you do and want to help me out, just let me know so I can fix it.

Filed under: docker, Linux, MySQL, PHP

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *

Name *
Email *
Website

12 + 19 =