How to wait for database migrations to complete before starting tests in docker-compose.yml

Viewed 2036

In a docker-compose.yml file, I have 3 services. The first service is used to spin up a MySQL database. The second service is used to run several database migrations to create tables and seed data in the MySQL database created in the first service. The third service is used to run some node.js unit tests which need the MySQL database. The problem is that I need the unit tests to start only after database migrations are complete. But there is no way for me to know that.

I have tried using the depends_on configuration option in the unit tests service to make it depend on the database migration service, but as per Docker documentation, depends_on only guaranties that unit tests service starts after database migration service has started.

1 Answers

The depends_on option allows you to control startup order only.
In order to check for readiness, you'll have to check it programmaticaly. You can either write a wrapper script that checks that your migration is done, or you can use existing tools such as wait-for-it

You can find more info on the official documentation

Related