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:
- the DB container is built
- then the app container is built
- Then my CMD on the app container is run (eg my crawler is started)
- 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/