{"id":272044,"date":"2021-06-11T15:00:01","date_gmt":"2021-06-11T12:00:01","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-and-why-to-add-health-checks-to-your-docker-containers-cloudsavvy-it\/"},"modified":"2021-06-11T15:00:01","modified_gmt":"2021-06-11T12:00:01","slug":"how-and-why-to-add-health-checks-to-your-docker-containers-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-and-why-to-add-health-checks-to-your-docker-containers-cloudsavvy-it\/","title":{"rendered":"#How (and Why) to Add Health Checks to Your Docker Containers \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3f9b3f76b45\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3f9b3f76b45\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-and-why-to-add-health-checks-to-your-docker-containers-cloudsavvy-it\/#How_Health_Checks_Work\" >How Health Checks Work<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-and-why-to-add-health-checks-to-your-docker-containers-cloudsavvy-it\/#Writing_Health_Check_Commands\" >Writing Health Check Commands<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-and-why-to-add-health-checks-to-your-docker-containers-cloudsavvy-it\/#Customizing_Health_Checks\" >Customizing Health Checks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-and-why-to-add-health-checks-to-your-docker-containers-cloudsavvy-it\/#Command_Format\" >Command Format<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-and-why-to-add-health-checks-to-your-docker-containers-cloudsavvy-it\/#What_About_Docker_Compose\" >What About Docker Compose?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-and-why-to-add-health-checks-to-your-docker-containers-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How (and Why) to Add Health Checks to Your Docker Containers \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-11186\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/38a27d3e.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Blue Docker logo on a purple background\" width=\"1602\" height=\"902\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You\u2019ve built your Docker image, pushed it to your registry and started a new container in production. Everything\u2019s working as you head home for the day but you\u2019re met with outage reports when you come back next morning. Your container\u2019s still running but it\u2019s not serving requests.<\/p>\n<p>This scenario might be uncomfortably familiar to operations teams that work with Docker. Here\u2019s how to use Docker\u2019s health check feature to get accurate data on the availability of your services.<\/p>\n<h2 id=\"how-health-checks-work\"><span class=\"ez-toc-section\" id=\"How_Health_Checks_Work\"><\/span>How Health Checks Work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\/#healthcheck\">Health checks allow<\/a> a container to expose its workload\u2019s availability. This stands apart from whether the container is <em>running<\/em>. If your database goes down, your API server won\u2019t be able to handle requests, even though its Docker container is still running.<\/p>\n<p>This makes for unhelpful experiences during troubleshooting. A simple <code>docker ps<\/code> would report the container as available. Adding a health check extends the <code>docker ps<\/code> output to include the container\u2019s true state.<\/p>\n<p>You configure container health checks in your <code>Dockerfile<\/code>. This accepts a command which the Docker daemon will execute every 30 seconds. Docker uses the command\u2019s exit code to determine your container\u2019s healthiness:<\/p>\n<ul>\n<li><strong><code>0<\/code><\/strong> \u2013 The container is healthy and working normally.<\/li>\n<li><strong><code>1<\/code><\/strong> \u2013 The container is unhealthy; the workload may not be functioning.<\/li>\n<li><strong><code>2<\/code><\/strong> \u2013 This status code is reserved by Docker and should not be used.<\/li>\n<\/ul>\n<p>When <code>HEALTHCHECK<\/code> is present in a Dockerfile, you\u2019ll see the container\u2019s healthiness in the <code>STATUS<\/code> column when you run <code>docker ps<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11921\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/06\/d424e24b-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"947\" height=\"129\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Healthiness isn\u2019t checked straightaway when containers are created. The status will show as <code>starting<\/code> before the first check runs. This gives the container time to execute any startup tasks. A container with a passing health check will show as <code>healthy<\/code>; an unhealthy container displays <code>unhealthy<\/code>.<\/p>\n<h2 id=\"writing-health-check-commands\"><span class=\"ez-toc-section\" id=\"Writing_Health_Check_Commands\"><\/span>Writing Health Check Commands<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Container health checks are configured with the <code>HEALTHCHECK<\/code> instruction in your <code>Dockerfile<\/code>. You should use a health check command that\u2019s <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ropriate for your container. For web servers, <code>curl<\/code> gives you an easy way to perform a basic readiness check. Ping a known endpoint that should be available whenever your service is up.<\/p>\n<pre>FROM nginx:latest&#13;\nHEALTHCHECK CMD curl --fail http:\/\/localhost\/api\/healthcheck || exit 1<\/pre>\n<p>This example would mark the container as unhealthy if your server\u2019s <code>\/api\/healthcheck<\/code> endpoint issued an error status.<\/p>\n<p>You can use <code>docker inspect<\/code> to see the output from health check commands. This is helpful when you\u2019re debugging your <code>HEALTHCHECK<\/code> instruction.<\/p>\n<pre>docker inspect --format=\"{{json .State.Health}}\" my-container<\/pre>\n<p>Replace <code>my-container<\/code> with the ID or name of the container you want to inspect. These details are displayed in <code>docker ps<\/code> output.<\/p>\n<h2 id=\"customizing-health-checks\"><span class=\"ez-toc-section\" id=\"Customizing_Health_Checks\"><\/span>Customizing Health Checks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker lets you customize the frequency of health checks. You can also alter the criteria that marks a container as unhealthy.<\/p>\n<p>There are four options available:<\/p>\n<ul>\n<li><strong><code>--interval<\/code><\/strong> \u2013 Set the time between health checks. This lets you override the default value of 30 seconds. Use a higher interval to increase the time between checks. This helps if you have a low-priority service where regular health checks might impact performance. Use a more regular frequency for critical services.<\/li>\n<li><strong><code>--start-period<\/code><\/strong> \u2013 Set the duration after a container starts when health checks should be ignored. The command will still be run but an unhealthy status won\u2019t be counted. This gives containers time to complete startup procedures.<\/li>\n<li><strong><code>--retries<\/code><\/strong> \u2013 This lets you require multiple successive failures before a container is marked as <code>unhealthy<\/code>. It defaults to 3. If a health check fails but the subsequent one passes, the container will not transition to <code>unhealthy<\/code>. It will become unhealthy after three consecutive failed checks.<\/li>\n<li><strong><code>--timeout<\/code><\/strong> \u2013 Set the timeout for health check commands. Docker will treat the check as failed if the command doesn\u2019t exit within this time frame.<\/li>\n<\/ul>\n<p>Options are passed as flags to the <code>HEALTHCHECK<\/code> instruction. Supply them before the health check command:<\/p>\n<pre>HEALTHCHECK --interval=60s --retries=5 CMD curl --fail http:\/\/localhost || exit 1<\/pre>\n<p>This configuration instructs Docker to run <code>curl<\/code> every 60 seconds. The container will be marked as unhealthy if five consecutive tests have a non-zero exit code.<\/p>\n<h2 id=\"command-format\"><span class=\"ez-toc-section\" id=\"Command_Format\"><\/span>Command Format<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>HEALTHCHECK<\/code> command syntax supports either a plain <code>CMD<\/code> or an entrypoint-style <code>exec<\/code> array. You can also pass <code>NONE<\/code> instead of <code>CMD<\/code> to disable health checks:<\/p>\n<pre>HEALTHCHECK NONE<\/pre>\n<p>This lets you inhibit your base image\u2019s health checks. Each <code>HEALTHCHECK<\/code> statement overrides any previous instructions in your image\u2019s layers.<\/p>\n<h2 id=\"what-about-docker-compose\"><span class=\"ez-toc-section\" id=\"What_About_Docker_Compose\"><\/span>What About Docker Compose?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker Compose supports health check definitions too. Add a <code>healthcheck<\/code> section to your service:.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co3\">version<\/span><span class=\"sy2\">: <\/span><span class=\"st0\">\"3\"<\/span><span class=\"co4\">\nservices<\/span>:<span class=\"co4\">\n  app<\/span>:<span class=\"co3\">\n    image<\/span><span class=\"sy2\">: <\/span>nginx:latest<span class=\"co4\">\n    ports<\/span><span class=\"sy2\">:\n<\/span>      - 80:80<span class=\"co4\">\n    healthcheck<\/span>:<span class=\"co3\">\n      test<\/span><span class=\"sy2\">: <\/span>curl --fail http:\/\/localhost || exit 1<span class=\"co3\">\n      interval<\/span><span class=\"sy2\">: <\/span>10s<span class=\"co3\">\n      retries<\/span><span class=\"sy2\">: <\/span>5<span class=\"co3\">\n      start_period<\/span><span class=\"sy2\">: <\/span>5s<span class=\"co3\">\n      timeout<\/span><span class=\"sy2\">: <\/span>10s<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>test<\/code> key defines the command to run. The other keys map to the parameters accepted by the Dockerfile <code>HEALTHCHECK<\/code> instruction.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Setting a <code>HEALTHCHECK<\/code> instruction makes it easier to diagnose a misbehaving container. You can track the readiness of your workload independently of the container\u2019s \u201crunning\u201d state.<\/p>\n<p>Health checks are compatible with any command that issues a <code>0<\/code> or <code>1<\/code> exit code. You can use common commands like <code>curl<\/code> and <code>ping<\/code> to inspect web services and databases. For more advanced control, write a dedicated script and include it in your images.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/11915\/how-and-why-to-add-health-checks-to-your-docker-containers\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How (and Why) to Add Health Checks to Your Docker Containers \u2013 CloudSavvy IT&#8221; You\u2019ve built your Docker image, pushed it to your registry and started a new container in production. Everything\u2019s working as you head home for the day but you\u2019re met with outage reports when you come back next morning. Your container\u2019s still&#8230;<\/p>\n","protected":false},"author":1,"featured_media":272045,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/38a27d3e.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-272044","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/272044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=272044"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/272044\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/272045"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=272044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=272044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=272044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}