How to wait for another docker container startup

There is a popular situation when you have an application in one docker container and that application uses mysql (or any other database, rabbitmq etc) from another container. And you need to start your app when all other containers are up and running and listening on the concrete port.

The most straightforward solution is using netcat utility which allows reading from and writing to network connections using TCP and UDP protocol. Here is an example:

# note, netcat utility should be installed in docker container
while ! nc -z DB 3306; do sleep 3; done
# DB is available here, so we can start our applicaiton
# java -jar /app.jar

Where ‘-z’ option specifies that nc should just scan for listening daemons, without sending any data to them

container-docker-blue-whale

Is a docker container up and running?

Leave a comment