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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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