If you have ever used Docker Compose to run multi-container applications, there is a good chance that you have run into the following situation. Service A depends on service B, but service B takes a a while to start up and be ready. Because of this, you must add some extra “wait for service B” logic into service A’s startup procedure.
For example consider this docker-compose file:
What Docker Compose guarantees is that dependency services are started in the same sense that your desktop computer is started when you press the power button–it still takes a while for the computer to get through its startup screens before you actually get a login screen and the computer is “ready for use.”
In our case here, Docker Compose only guarantees that the web service had its “power button pressed,” but not that the web server is actually ready to start accepting connections; it still has to go through its regular startup procedure and do a few things before it’s actually “ready for use.”
Because both services start at the same time, it is possible that the spring-boot attempt to initiate a connection to the web service before the docker-mysql service is ready to accept connections.
Create a file called “wait-for-it.sh”, the original script is here vishnubob’s page:
Now we can edit the original docker-compose.yml file, in this way: