Docker Compose Wait for Dependencies

Docker Compose Wait for Container using Dockerize Tool

This article provides an example of a step-by-step reproducible guide to make docker-compose wait for container dependencies (example: MySQL, Postgres, Redis, Mongodb) using the dockerize tool.

The dockerize tool gives you the ability to wait for services on a specified protocol (file, tcp, tcp4, tcp6, http, https and unix) before starting your application:

dockerize -wait tcp://db:5432 -wait http://web:80 -wait file:///tmp/generated-file

Important arguments:

  • timeout. You can optionally specify how long to wait for the services to become available by using the -timeout # argument (Default: 10 seconds). If the timeout is reached and the service is still not available, the process exits with status code 1.
  • wait-retry-interval. dockerize sleeping time before checking whether the dependencies are ready
dockerize -wait tcp://db:5432 -wait http://web:80 -timeout 10s -wait-retry-interval 3s


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/ex02-using-dockerize-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-dockerize-tool

Project folder structure:

files/docker-compose-wait-for-container/ex02-using-dockerize-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 dockerize 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 dockerize tool -------------------
RUN apk add --no-cache openssl
ENV DOCKERIZE_VERSION v0.6.1
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz

CMD ["/sayhello"]

Use this for Ubuntu image:

# Add dockerize tool -------------------
RUN apt-get update && apt-get install -y wget
ENV DOCKERIZE_VERSION v0.6.1
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

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 "dockerize -wait tcp://mysql:3306 -timeout 300s -wait-retry-interval 30s /sayhello"

In essence, Dockerize is a wrapper. dockerize our_normal_command just calls our command. But optionally, we can add parameters to delay execution, perform file templating or redirect output from files to STDOUT/STDERR. Very common and useful operations in a Docker world.

Examples of optional dockerize configurations:

# redirect files to stdout and stderr
dockerize \
  -stdout info.log \
  -stdout perf.log \
  ...

# wait for 2 services with 10s timeout
dockerize \
  -wait tcp://db:5432 \
  -wait http://web:80 \
  -timeout 10s \
  ...

# template option
dockerize \
  -template nginx.tmpl:nginx.conf \
  ...

Step 3: Building and running your app

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

Console log output:

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 container dependencies using the dockerize tool.



Version: Français

(Next Lesson) Docker Compose Wait for Container using Wait Tool
Back to Docker Compose Wait for Dependencies

No Comments

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