How to setup delay for docker containers with docker-compose [duplicate]

  docker, docker-compose, docker-swarm, dockerfile

When I run 3 images through the following docker-compose file.

docker-compose.yml

version: "3.8"

services:
  consumer:
    image: consumer
    container_name: docker-consumer
    ports:
    - "9098:9098"
    networks:
      - producer-consumer
    depends_on:
      - producer
  producer:
      image: producer
      container_name: docker-producer
      ports:
      - "9090:9090"
      networks:
        - producer-consumer
      depends_on:
        - mysql
  mysql:
    image: mysql:8
    container_name: mysql-db
    ports:
    - 3306:3306
    networks:
      - producer-consumer
    environment:
      - MYSQL_ROOT_PASSWORD=14292
      - MYSQL_DATABASE=HR
      - MYSQL_USER=root

networks:
  producer-consumer:

I have to set up a delay for producer and consumer containers until the MySQL container is created. when I run the above file then MySQL connection is going to be failed with the connection URL.

"jdbc:mysql://mysql-db:3306/HR?createDatabaseIfNotExists=true&autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false"

I am using container_name instead of using localhost and during the creation of the producer container it is trying to use localhost to connect and it will be failed.

can I run the container sequentially in the following way?

1.mysql-db

2.docker-producer

3.docker-consumer

Source: Docker Questions

LEAVE A COMMENT