{"id":12463,"date":"2020-01-12T19:57:10","date_gmt":"2020-01-12T17:57:10","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?post_type=dt_lessons&#038;p=12463"},"modified":"2020-01-13T23:12:30","modified_gmt":"2020-01-13T21:12:30","slug":"docker-compose-wait-for-container-using-wait-tool","status":"publish","type":"dt_lessons","link":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/","title":{"rendered":"Docker Compose Wait for Container using Wait Tool"},"content":{"rendered":"<div id=\"rdoc\">\n<p>This article provides an example of a step-by-step reproducible guide to make <strong>docker-compose wait for container<\/strong> dependencies (example: <em>MySQL<\/em>, <em>Postgres<\/em>, <em>Redis<\/em>, <em>Mongodb<\/em>) using the <a href=\"https:\/\/github.com\/ufoscout\/docker-compose-wait\">docker-compose-wait tool<\/a> tool.<\/p>\n<p>The <strong>docker-compose-wait tool<\/strong> is a small command line utility to wait for other docker images to be started while using docker-compose. It permits to wait for a fixed amount of seconds and\/or to wait until a TCP port is open on a target image.<\/p>\n<p>Like for the <code>dockerize<\/code> tool, you need to add the docker-compose-wait tool in your application Dockerfile.<\/p>\n<p>Contents:<\/p>\n<div id=\"TOC\">\n<ul>\n<li><a href=\"#quick-start\">Quick start<\/a><\/li>\n<li><a href=\"#step-0-download-a-template\">Step 0: Download a template<\/a><\/li>\n<li><a href=\"#step-1-add-the-docker-compose-wait-tool-to-your-application-dockerfile\">Step 1: Add the docker-compose-wait tool to your application Dockerfile<\/a><\/li>\n<li><a href=\"#step-2-modify-your-docker-compose.yml-file\">Step 2: Modify your docker-compose.yml file<\/a><\/li>\n<li><a href=\"#step-3-building-and-running-your-app\">Step 3: Building and running your app<\/a><\/li>\n<li><a href=\"#step-4-stopping-containers-and-cleaning\">Step 4: Stopping containers and cleaning<\/a><\/li>\n<li><a href=\"#summary\">Summary<\/a><\/li>\n<li><a href=\"#references\">References<\/a><\/li>\n<\/ul>\n<\/div>\n<div id=\"quick-start\" class=\"section level2\">\n<h2>Quick start<\/h2>\n<pre class=\"bash\"><code># Download a template\r\ngit clone https:\/\/github.com\/kassambara\/docker-compose-wait-for-container.git\r\n\r\n# Build the demo application\r\ncd docker-compose-wait-for-container\/ex01-using-wait-tool\r\ndocker-compose build\r\n# Running your app\r\ndocker-compose run my_super_app\r\n\r\n# Stopping containers and cleaning\r\ndocker-compose down \r\nrm -rf mysql<\/code><\/pre>\n<\/div>\n<div id=\"step-0-download-a-template\" class=\"section level2\">\n<h2>Step 0: Download a template<\/h2>\n<pre class=\"bash\"><code># Download a template\r\ngit clone https:\/\/github.com\/kassambara\/docker-compose-wait-for-container.git\r\ncd docker-compose-wait-for-container\/ex02-using-dockerize-tool<\/code><\/pre>\n<p><strong>Project folder structure<\/strong>:<\/p>\n<pre><code>files\/docker-compose-wait-for-container\/ex01-using-wait-tool\r\n\u251c\u2500\u2500 docker-compose.yml\r\n\u2514\u2500\u2500 my_super_app\r\n    \u251c\u2500\u2500 Dockerfile\r\n    \u2514\u2500\u2500 sayhello<\/code><\/pre>\n<p>Essential project contents:<\/p>\n<ul>\n<li><code>docker-compose.yml<\/code> to run all container services<\/li>\n<li><code>my_super_app<\/code> scripts: template Dockerfile to build your application. Here, this demo app will ask your name and then congratulate you!<\/li>\n<\/ul>\n<\/div>\n<div id=\"step-1-add-the-docker-compose-wait-tool-to-your-application-dockerfile\" class=\"section level2\">\n<h2>Step 1: Add the docker-compose-wait tool to your application Dockerfile<\/h2>\n<p>Example of Dockerfile using the <code>alpine<\/code> image:<\/p>\n<pre class=\"dockerfile\"><code>FROM alpine:latest\r\n\r\n# Add hello scripts\r\nADD sayhello \/sayhello\r\nRUN chmod +x \/sayhello\r\n\r\n# Add docker-compose-wait tool -------------------\r\nENV WAIT_VERSION 2.7.2\r\nADD https:\/\/github.com\/ufoscout\/docker-compose-wait\/releases\/download\/$WAIT_VERSION\/wait \/wait\r\nRUN chmod +x \/wait\r\n\r\nCMD [\"\/sayhello\"]<\/code><\/pre>\n<\/div>\n<div id=\"step-2-modify-your-docker-compose.yml-file\" class=\"section level2\">\n<h2>Step 2: Modify your docker-compose.yml file<\/h2>\n<pre class=\"yaml\"><code>version: '3.6'\r\nservices:\r\n  mysql:\r\n    image: \"mysql:5.7\"\r\n    container_name: mysql\r\n    restart: always\r\n    volumes:\r\n      - .\/mysql:\/var\/lib\/mysql\r\n    environment:\r\n      - MYSQL_ROOT_PASSWORD=your_password\r\n      - MYSQL_USER=root\r\n      - MYSQL_PASSWORD=your_password\r\n      - MYSQL_DATABASE=wordpress\r\n    ports:\r\n      - \"3306:3306\"\r\n    expose:\r\n      - 3306\r\n\r\n  my_super_app:\r\n    build: .\/my_super_app\r\n    image: \"my_super_app:latest\"\r\n    container_name: my_supper_app\r\n    depends_on:\r\n      - mysql\r\n    command: sh -c \"\/wait &amp;&amp; \/sayhello\"\r\n    environment:\r\n      - WAIT_HOSTS=mysql:3306\r\n      - WAIT_HOSTS_TIMEOUT=300\r\n      - WAIT_SLEEP_INTERVAL=30\r\n      - WAIT_HOST_CONNECT_TIMEOUT=30<\/code><\/pre>\n<div class=\"success\">\n<ul>\n<li>The command <code>sh -c \u201c\/wait &amp;&amp; \/sayhello\u201d<\/code> will run the wait tool and then your application, here \/sayhello.<\/li>\n<li>When docker-compose is started (or Kubernetes or docker stack or whatever), your application will be started only when all the pairs host:port in the <code>WAIT_HOSTS<\/code> variable are available. The <code>WAIT_HOSTS<\/code> environment variable is not mandatory, if not declared, the script executes without waiting.<\/li>\n<li>To make your docker application container <strong>wait for multiple hosts<\/strong>, the environment variable can be specified as for example <code>WAIT_HOSTS=mysql:3306, nginx:80<\/code><\/li>\n<\/ul>\n<\/div>\n<p><strong>Additional configuration options<\/strong>. The behavior of the wait utility can be configured with the following environment variables:<\/p>\n<ul>\n<li><em>WAIT_HOSTS<\/em>: comma separated list of pairs host:port for which you want to wait.<\/li>\n<li><em>WAIT_HOSTS_TIMEOUT<\/em>: max number of seconds to wait for all the hosts to be available before failure. The default is 30 seconds.<\/li>\n<li><em>WAIT_HOST_CONNECT_TIMEOUT<\/em>: The timeout of a single TCP connection to a remote host before attempting a new connection. The default is 5 seconds.<\/li>\n<li><em>WAIT_BEFORE_HOSTS<\/em>: number of seconds to wait (sleep) before start checking for the hosts availability<\/li>\n<li><em>WAIT_AFTER_HOSTS<\/em>: number of seconds to wait (sleep) once all the hosts are available<\/li>\n<li><em>WAIT_SLEEP_INTERVAL<\/em>: number of seconds to sleep between retries. The default is 1 second.<\/li>\n<\/ul>\n<\/div>\n<div id=\"step-3-building-and-running-your-app\" class=\"section level2\">\n<h2>Step 3: Building and running your app<\/h2>\n<pre class=\"bash\"><code># Building your app\r\ncd docker-compose-wait-for-container\/ex01-using-wait-tool\r\ndocker-compose build\r\n# Running your app\r\ndocker-compose run my_super_app<\/code><\/pre>\n<p>Console log output:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/docker-compose-wait-for-container\/images\/docker-compose-wait-for-mysql-logs.png\" alt=\"Docker compose wait logs\" \/><\/p>\n<div class=\"success\">\n<p>After typing your name, you will see a congratulation message from my_super_app<\/p>\n<\/div>\n<\/div>\n<div id=\"step-4-stopping-containers-and-cleaning\" class=\"section level2\">\n<h2>Step 4: Stopping containers and cleaning<\/h2>\n<pre class=\"bash\"><code>docker-compose down \r\nrm -rf mysql<\/code><\/pre>\n<\/div>\n<div id=\"summary\" class=\"section level2\">\n<h2>Summary<\/h2>\n<p>This article describes how to make docker-compose wait for container dependencies using the docker-compose-wait tool.<\/p>\n<\/div>\n<div id=\"references\" class=\"section level2\">\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/ufoscout\/docker-compose-wait\">docker-compose-wait: A simple script to wait for other docker images to be started while using docker-compose<\/a><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 docker-compose-wait tool tool.<\/p>\n","protected":false},"author":1,"featured_media":8981,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","class_list":["post-12463","dt_lessons","type-dt_lessons","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Docker Compose Wait for Container using Wait Tool : Excellent Examples - Datanovia<\/title>\n<meta name=\"description\" content=\"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 docker-compose-wait tool tool.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Docker Compose Wait for Container using Wait Tool : Excellent Examples - Datanovia\" \/>\n<meta property=\"og:description\" content=\"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 docker-compose-wait tool tool.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:modified_time\" content=\"2020-01-13T21:12:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X45200983_746222529051027_3970259655229177856_n.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/\",\"name\":\"Docker Compose Wait for Container using Wait Tool : Excellent Examples - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X45200983_746222529051027_3970259655229177856_n.jpg\",\"datePublished\":\"2020-01-12T17:57:10+00:00\",\"dateModified\":\"2020-01-13T21:12:30+00:00\",\"description\":\"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 docker-compose-wait tool tool.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X45200983_746222529051027_3970259655229177856_n.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X45200983_746222529051027_3970259655229177856_n.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Lessons\",\"item\":\"https:\/\/www.datanovia.com\/en\/lessons\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Docker Compose Wait for Container using Wait Tool\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"name\":\"Datanovia\",\"description\":\"Data Mining and Statistics for Decision Support\",\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.datanovia.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\",\"name\":\"Datanovia\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"width\":98,\"height\":99,\"caption\":\"Datanovia\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Docker Compose Wait for Container using Wait Tool : Excellent Examples - Datanovia","description":"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 docker-compose-wait tool tool.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/","og_locale":"en_US","og_type":"article","og_title":"Docker Compose Wait for Container using Wait Tool : Excellent Examples - Datanovia","og_description":"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 docker-compose-wait tool tool.","og_url":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/","og_site_name":"Datanovia","article_modified_time":"2020-01-13T21:12:30+00:00","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X45200983_746222529051027_3970259655229177856_n.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/","url":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/","name":"Docker Compose Wait for Container using Wait Tool : Excellent Examples - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X45200983_746222529051027_3970259655229177856_n.jpg","datePublished":"2020-01-12T17:57:10+00:00","dateModified":"2020-01-13T21:12:30+00:00","description":"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 docker-compose-wait tool tool.","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X45200983_746222529051027_3970259655229177856_n.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X45200983_746222529051027_3970259655229177856_n.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/lessons\/docker-compose-wait-for-container-using-wait-tool\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"Lessons","item":"https:\/\/www.datanovia.com\/en\/lessons\/"},{"@type":"ListItem","position":3,"name":"Docker Compose Wait for Container using Wait Tool"}]},{"@type":"WebSite","@id":"https:\/\/www.datanovia.com\/en\/#website","url":"https:\/\/www.datanovia.com\/en\/","name":"Datanovia","description":"Data Mining and Statistics for Decision Support","publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.datanovia.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.datanovia.com\/en\/#organization","name":"Datanovia","url":"https:\/\/www.datanovia.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","width":98,"height":99,"caption":"Datanovia"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/"}}]}},"multi-rating":{"mr_rating_results":[]},"_links":{"self":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/12463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons"}],"about":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/types\/dt_lessons"}],"author":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/comments?post=12463"}],"version-history":[{"count":1,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/12463\/revisions"}],"predecessor-version":[{"id":12501,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/12463\/revisions\/12501"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/8981"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=12463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}