2

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!

theJediCode
  • 112
  • 2
  • 12
  • 2
    You could create an `entrypoint.sh` for your `flyway` service and use this technique here: https://stackoverflow.com/a/29793382/9835872 – ruohola Aug 27 '21 at 19:36
  • 2
    Does this answer your question? [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) – ruohola Aug 27 '21 at 19:37
  • Thanks both of you. Those were very helpful links and helped me debug as to what was going wrong. I went ahead without overwriting the endpoint. However, the real problem turned out to be something else. It was the timeout. Increasing it to 150000 allowed the tests to run fine. – theJediCode Aug 31 '21 at 18:29

1 Answers1

0

After a long session of logging and debugging, the issue seems to be the timeout. The 120000ms I specified turned out to be insufficient for the MySQL container to fire up. Increasing the timeout to say 150000ms does the trick.

theJediCode
  • 112
  • 2
  • 12