I’m trying to set up my Docker Compose project to work with an already running MySQL container that’s not part of the same compose file. The problem is I can’t figure out how to make my services wait for this external database to be ready before starting up.
I know we can use the depends_on option for services within the same compose file, but that doesn’t seem to work for external containers. Is there a way to achieve this kind of dependency?
I’ve looked around for solutions, but haven’t found anything that really works for my situation. Has anyone dealt with this before? What’s the best way to handle external dependencies in Docker Compose?
Here’s a basic example of what I’m trying to do:
version: '3'
services:
myapp:
image: myapp:latest
# How can I make this wait for the external MySQL?
environment:
- DB_HOST=external-mysql
- DB_PORT=3306
Any help or suggestions would be really appreciated!
I’ve encountered this issue before and found a workaround using a custom health check script. You can create a simple shell script that attempts to connect to your external MySQL database and exits with a non-zero status if it fails. Then, use this script as a healthcheck in your Docker Compose file.
Here’s an example of how you might modify your compose file:
This setup introduces a db-healthcheck service that checks the external MySQL’s availability. Your main service then depends on this healthcheck service, effectively waiting for the external database to be ready.
hey, i’ve dealt with this before. you could try using a wait-for-it script. it’s pretty simple to set up. just add it to your dockerfile and use it in the command section of your compose file. something like: