gomisha
gomisha

Reputation: 2922

Wait for mysql service to be ready in docker compose before creating other services

I'm trying to use wait-for-it in my docker-compose.yaml to wait for mysql to be ready before creating services that are dependent on it. This is my docker-compose.yaml:

version: '3.5'

services:
  mysql:
    image: mysql:5.6
    ports:
      - "3307:3306"
    networks:
      - integration-tests
    environment:
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=root
      - MYSQL_ROOT_PASSWORD=mypassword
    entrypoint: ./wait-for-it.sh mysql:3306
networks:
  integration-tests:
    name: integration-tests

I get this error when trying to run this with docker-compose:

Starting integration-tests_mysql_1 ... error

ERROR: for integration-tests_mysql_1 Cannot start service mysql: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"./wait-for-it.sh\": stat ./wait-for-it.sh: no such file or directory": unknown

ERROR: for mysql Cannot start service mysql: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"./wait-for-it.sh\": stat ./wait-for-it.sh: no such file or directory": unknown ERROR: Encountered errors while bringing up the project.

The wait-for-it.sh script is there on the same level as my docker-compose.yaml file so I don't understand why it's not being found.

Upvotes: 0

Views: 7460

Answers (2)

Mr3381
Mr3381

Reputation: 387

You can control services startup order by using the docker depends_on option.

Upvotes: 1

twoTimesAgnew
twoTimesAgnew

Reputation: 1506

Your issue here is that you're trying to execute something that is not part of your image. You're telling docker to create a container from mysql:5.6, which doesn't contain wait-for-it.sh, and then you're telling it to start the container by launching wait-for-it.sh.

I would recommend that you create your own image containing the following:

#Dockerfile
FROM mysql:5.6

COPY wait-for-it.sh /wait-for-it.sh
RUN chmod +x /wait-for-it.sh

Then you would replace mysql:5.6 with your image and you should be able to execute wait-for-it.sh. I would also execute it through a command instead of an entrypoint as such:

#docker-compose.yml
...
mysql:
  image: yourmysql:5.6
  command:  bash -c "/wait-for-it.sh -t 0 mysql:3306"
...

Where -t 0 will wait for mysql without a timeout.

Upvotes: 5

Related Questions