1

I have a docker-compose file as shown below, that has 2 containers. One is a MySQL DB and the other is a Python crawler app that reads/writes to the DB. When I do docker-compose up, I see:

  1. the DB container is built
  2. then the app container is built
  3. Then my CMD on the app container is run (eg my crawler is started)
  4. Then my database is initialized in the DB container based on the environment variables in the docker-compose file.

My question is why is my crawler script running before my database is created in the DB container? How can I ensure that the database is already created before my crawler script is run?

version: '3.7' 
services: 
    db:
        image: mysql:8
        restart: always
        environment: 
            MYSQL_DATABASE: my-database-name
            MYSQL_USER: root
            MYSQL_ROOT_PASSWORD: password
            MYSQL_PASSWORD: password
        ports:
            - "3308:3306"
        command: --default-authentication-plugin=mysql_native_password
    app:
        build:
            context: ./
            dockerfile: Dockerfile-crawler-dev
        depends_on: 
            - db
        environment: 
            MYSQL_DATABASE: my-database-name
            MYSQL_USER: root
            MYSQL_ROOT_PASSWORD: password
            MYSQL_PASSWORD: password
            MYSQL_HOST: db
            MYSQL_PORT: 3306
        volumes:
            - ./:/crawler/
Wise Shepherd
  • 2,228
  • 4
  • 31
  • 43

1 Answers1

3

The best option use to check only from your code. Also if you can use compose 2, but read documentation: "There are several things to be aware of when using depends_on:

depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.

Version 3 no longer supports the condition form of depends_on.

The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file."

Master
  • 101
  • 2