Docker Compose Wait for Dependencies

Docker Compose Wait for MySQL Container to be Ready

This article provides an example for making docker-compose wait for MySQL container to be ready before starting a dependent docker application container. We’ll use the docker-compose-wait tool tool, which is a small command line utility allowing to wait for a fixed amount of seconds and/or to wait until a TCP port is open on a target image.

You need to add the docker-compose-wait tool in your application Dockerfile.



Contents:

Quick start

# Download a template
git clone https://github.com/kassambara/docker-compose-wait-for-container.git

# Build the demo application
cd docker-compose-wait-for-container/ex01-using-wait-tool
docker-compose build
# Running your app
docker-compose run my_super_app

# Stopping containers and cleaning
docker-compose down 
rm -rf mysql

Step 0: Download a template

# Download a template
git clone https://github.com/kassambara/docker-compose-wait-for-container.git
cd docker-compose-wait-for-container/ex02-using-wait-tool

Project folder structure:

files/docker-compose-wait-for-container/ex01-using-wait-tool
├── docker-compose.yml
└── my_super_app
    ├── Dockerfile
    └── sayhello

Essential project contents:

  • docker-compose.yml to run all container services
  • my_super_app scripts: template Dockerfile to build your application. Here, this demo app will ask your name and then congratulate you!

Step 1: Add the docker-compose-wait tool to your application Dockerfile

Example of Dockerfile using the alpine image:

FROM alpine:latest

# Add hello scripts
ADD sayhello /sayhello
RUN chmod +x /sayhello

# Add docker-compose-wait tool -------------------
ENV WAIT_VERSION 2.7.2
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait
RUN chmod +x /wait

CMD ["/sayhello"]

Step 2: Modify your docker-compose.yml file

version: '3.6'
services:
  mysql:
    image: "mysql:5.7"
    container_name: mysql
    restart: always
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=your_password
      - MYSQL_USER=root
      - MYSQL_PASSWORD=your_password
      - MYSQL_DATABASE=wordpress
    ports:
      - "3306:3306"
    expose:
      - 3306

  my_super_app:
    build: ./my_super_app
    image: "my_super_app:latest"
    container_name: my_supper_app
    depends_on:
      - mysql
    command: sh -c "/wait && /sayhello"
    environment:
      - WAIT_HOSTS=mysql:3306
      - WAIT_HOSTS_TIMEOUT=300
      - WAIT_SLEEP_INTERVAL=30
      - WAIT_HOST_CONNECT_TIMEOUT=30
  • The command sh -c “/wait && /sayhello” will run the wait tool and then your application, here /sayhello.
  • To make your docker application container wait for multiple hosts, the environment variable can be specified as for example WAIT_HOSTS=mysql:3306, nginx:80

Additional configuration options. The behavior of the wait utility can be configured with the following environment variables:

  • WAIT_HOSTS: comma separated list of pairs host:port for which you want to wait.
  • WAIT_HOSTS_TIMEOUT: max number of seconds to wait for all the hosts to be available before failure. The default is 30 seconds.
  • WAIT_HOST_CONNECT_TIMEOUT: The timeout of a single TCP connection to a remote host before attempting a new connection. The default is 5 seconds.
  • WAIT_BEFORE_HOSTS: number of seconds to wait (sleep) before start checking for the hosts availability
  • WAIT_AFTER_HOSTS: number of seconds to wait (sleep) once all the hosts are available
  • WAIT_SLEEP_INTERVAL: number of seconds to sleep between retries. The default is 1 second.

Step 3: Building and running your app

# Building your app
cd docker-compose-wait-for-container/ex01-using-wait-tool
docker-compose build
# Running your app
docker-compose run my_super_app

Console log output looks like this (MySQL example):

Docker compose wait logs

After typing your name, you will see a congratulation message from my_super_app

Step 4: Stopping containers and cleaning

docker-compose down 
rm -rf mysql

Summary

This article describes how to make docker-compose wait for MySQL container using the docker-compose-wait tool.



Version: Français

Docker Compose Wait for Container using Wait Tool (Prev Lesson)
(Next Lesson) Docker Compose Wait for MongoDB Container to be Ready
Back to Docker Compose Wait for Dependencies

Comment ( 1 )

  • RICARDO RAUL JACINTO MONTES

    Sirs, thanks for your site and your scripts.
    I am having a weird problem I am using your tools to ensure sequence creation of containers. It works well in Ubuntu 18:04 but fails in 16:04.

    Failure is detected when a message sh 1: wait not found I have traced this information and it seems to me that is related to the download of wait.
    I assume that based on if there’s wait command which should be located on /wait.

    Sorry to botter, but any help will be greatly appretiated. Thanks in advance. Obviously I can sen files needed.

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments