This article provides an example for making docker-compose wait for Postgres or Postgresql 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.
# 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
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.ymlto run all container services
my_super_appscripts: 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
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: postgres: image: "postgres:9.4" hostname: postgres ports: - "5432:5432" my_super_app: build: ./my_super_app image: "my_super_app:latest" container_name: my_supper_app depends_on: - postgres command: sh -c "/wait && /sayhello" environment: - WAIT_HOSTS=postgres:5432 - 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
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):
After typing your name, you will see a congratulation message from my_super_app
Step 4: Stopping containers and cleaning
This article describes how to make docker-compose wait for Postgres or Postgresql container using the docker-compose-wait 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
- 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
- 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
Amazing Selling Machine
- Free Training - How to Build a 7-Figure Amazon FBA Business You Can Run 100% From Home and Build Your Dream Life! by ASM
Books - Data Science
- 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)
- 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
Hilariously even after talking to the server this only checks it is available, not ready… Reporting this about Postgres with NodeJS