I am using flyway with a mysql db. I am using them as services in a docker-compose and then using this in testcontainer's DockerComposeEnvironment. The problem is that even though I have added the depends_on
key in 'flyway' service for the MySQL 'db', testcontainer starts flyway first and this results in the migrations not being applied.
I want to know if there is a method by which we can make flyway container run only when the db container is ready?
I am using this piece of code to initialize my test docker-compose environment (test_mysql
and flyway_c
are the names of the containers of the services):
const environment = await new DockerComposeEnvironment(composeFilePath, composeFile)
.withWaitStrategy('test_mysql', Wait.forLogMessage(`ready for connections. Bind-address: '::' port:`))
.withWaitStrategy('flyway_c', Wait.forLogMessage(`Successfully applied`))
.withStartupTimeout(120000)
.up();
This is my docker-compose file:
version: '3'
services:
db:
image: mysql:8.0.26
environment:
- MYSQL_DATABASE=test_db
- MYSQL_ROOT_PASSWORD=pass
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=mysql_native_password
ports:
- '3306:3306'
- '33060:33060'
expose:
- '3306'
container_name: test_mysql
flyway:
image: flyway/flyway
command: -url=jdbc:mysql://db -schemas=test_db -user=root -password=pass -connectRetries=100 migrate
volumes:
- .:/flyway/sql
depends_on:
- db
container_name: flyway_c
Any help would be appreciated. Thanks!
EDIT:
This problem is not caused by the docker-compose. In other words, the service flyway
does wait for db
if they are run as docker-compose up
but they fail when running inside the testcontainers npm library. So, Docker Compose wait for container X before starting Y doesn't really resolve this.
I have added the answer which I used to resolve this for me. Thanks!