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 servicesmy_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:
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.
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
- Course: Machine Learning: Master the Fundamentals by Stanford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
Trending Courses
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Amazon FBA
Amazing Selling Machine
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Version: Français
No Comments