{"id":258682,"date":"2021-05-25T21:00:15","date_gmt":"2021-05-25T18:00:15","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it\/"},"modified":"2021-05-25T21:00:15","modified_gmt":"2021-05-25T18:00:15","slug":"the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it\/","title":{"rendered":"#The Difference Between CMD and ENTRYPOINT in Docker Images \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_88 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-6ac184eecffb1\" 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-6ac184eecffb1\" 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\/the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it\/#What_Is_The_Entrypoint\" >What Is The Entrypoint?<\/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\/the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it\/#Adding_The_Command_CMD\" >Adding The Command (CMD)<\/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\/the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it\/#Entrypoint_Overrides\" >Entrypoint Overrides<\/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\/the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it\/#Which_One_To_Use\" >Which One To Use?<\/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\/the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it\/#Entrypoint_Modes_Shell_or_Exec\" >Entrypoint Modes: Shell or Exec<\/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\/the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it\/#Benefits_of_Dockers_Entrypoint_Approach\" >Benefits of Docker\u2019s Entrypoint Approach<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/the-difference-between-cmd-and-entrypoint-in-docker-images-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#The Difference Between CMD and ENTRYPOINT in Docker Images \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-10864\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Illustration showing the Docker logo\" width=\"1600\" height=\"900\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The <code>CMD<\/code> and <code>ENTRYPOINT<\/code> instructions are two commonly confused <code>Dockerfile<\/code> directives. Both have a role in determining the command which will run when the container starts.<\/p>\n<p>The <code>CMD<\/code> and <code>ENTRYPOINT<\/code> can be overridden individually within each image. Effective use of these directives makes your container easier to use by shortening the length of commands you supply.<\/p>\n<h2 id=\"what-is-the-entrypoint\"><span class=\"ez-toc-section\" id=\"What_Is_The_Entrypoint\"><\/span>What Is The Entrypoint?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We\u2019ll look at <code>ENTRYPOINT<\/code> first as it\u2019s processed before <code>CMD<\/code> when starting a new container. The image\u2019s entrypoint defines the process which will be run when the container starts.<\/p>\n<p>Docker defaults the entrypoint to <code>\/bin\/sh -c<\/code>. This means you\u2019ll end up in a shell session when you start the container. For many containers, it\u2019s more desirable to have a different process launch by default. You want headless services to start their workload straightaway.<\/p>\n<p>Setting the <code>ENTRYPOINT<\/code> directive in a <code>Dockerfile<\/code> instructs Docker to run a specific command when the container starts. It will become the foreground process, instead of the default shell session.<\/p>\n<pre>ENTRYPOINT [\"date\"]<\/pre>\n<p>A container created with this <code>Dockerfile<\/code> will run the <code>date<\/code> command. As <code>date<\/code> is not a long-living foreground process, the container will exit im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely afterwards.<\/p>\n<p>Entrypoints must be executable binaries or <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\">script<\/a>s. Your container won\u2019t start if you specify an invalid entrypoint. If you\u2019re using a custom script, make sure it\u2019s got the executable bit set. You can add execute permissions using <code>chmod +x my-script.sh<\/code>.<\/p>\n<h2 id=\"adding-the-command-cmd\"><span class=\"ez-toc-section\" id=\"Adding_The_Command_CMD\"><\/span>Adding The Command (CMD)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>CMD<\/code> instruction is something of a misnomer. It provides <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\">default arguments<\/a> for the command defined by <code>ENTRYPOINT<\/code>.<\/p>\n<pre>ENTRYPOINT [\"date\"]&#13;\nCMD [\"+%A\"]<\/pre>\n<p>This example results in the container running <code>date +%A<\/code>. The <code>+%A<\/code> argument to <code>date<\/code> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/date.1.html\">displays the current<\/a> day of the week (e.g.\u00a0<code>Monday<\/code>).<\/p>\n<p><code>CMD<\/code> is designed to be overridden. <code>docker run<\/code> lets you specify a different command for an individual container instance:<\/p>\n<pre>docker run my-image +%B<\/pre>\n<p>The default <code>CMD<\/code> will be overridden with <code>+%B<\/code>, causing the container to display the name of the current month. This works because the image\u2019s entrypoint remains intact. The <code>CMD<\/code> is always appended to the <code>ENTRYPOINT<\/code>, so the final command becomes <code>date +%B<\/code>.<\/p>\n<p>You should use <code>ENTRYPOINT<\/code> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\/#understand-how-cmd-and-entrypoint-interact\">to define your<\/a> container\u2019s primary executable. Use <code>CMD<\/code> to define default arguments for that executable. It will be overridden when the container is run with different arguments.<\/p>\n<h2 id=\"entrypoint-overrides\"><span class=\"ez-toc-section\" id=\"Entrypoint_Overrides\"><\/span>Entrypoint Overrides<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can force Docker to start an image using a custom entrypoint. Pass the <code>--entrypoint<\/code> flag to <code>docker run<\/code>:<\/p>\n<pre>docker run --entrypoint \/bin\/sh my-image<\/pre>\n<p>The entrypoint defined in the container\u2019s image will be ignored in favour of the command you\u2019ve specified. In our example, a shell session will be started, instead of the <code>date<\/code> command.<\/p>\n<p>Overriding entrypoints should be a rare occurrence. It can go against the image author\u2019s intentions. Setting a custom entrypoint can be useful though, particularly when debugging. If a container\u2019s misbehaving, overriding its entrypoint can grant you shell access you couldn\u2019t otherwise obtain.<\/p>\n<h2 id=\"which-one-to-use\"><span class=\"ez-toc-section\" id=\"Which_One_To_Use\"><\/span>Which One To Use?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you\u2019re an image author, you should use <code>ENTRYPOINT<\/code> when defining what your container will run. If you want to provide default arguments, but expect the user to override them, include <code>CMD<\/code> too.<\/p>\n<p>As an image user, you can normally stick to overriding <code>CMD<\/code>. <code>docker run<\/code> has transparent support for command overrides. Any arguments provided after the image name will be interpreted as the <code>CMD<\/code> string for the container.<\/p>\n<h2 id=\"entrypoint-modes-shell-or-exec\"><span class=\"ez-toc-section\" id=\"Entrypoint_Modes_Shell_or_Exec\"><\/span>Entrypoint Modes: Shell or Exec<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker actually supports two different forms of <code>ENTRYPOINT<\/code>: exec mode and shell mode. Exec mode is characterised by the use of an array construct to specify parameters. In shell mode, the command is specified as one string.<\/p>\n<pre># exec mode&#13;\nENTRYPOINT [\"binary\", \"--param\", \"--another-param\"]&#13;\n&#13;\n# shell mode&#13;\nENTRYPOINT binary --param --another-param<\/pre>\n<p>Using shell mode causes your binary to be executed as a subprocess of <code>\/bin\/sh -c<\/code>. This gives your entrypoint access to environment variables defined by the shell.<\/p>\n<p>Shell mode has tradeoffs though. You can\u2019t use <code>CMD<\/code> so users won\u2019t be able to issue overrides. Arguments given to <code>docker run<\/code> will be ignored; your container will always use the entrypoint as-is.<\/p>\n<p>Because your binary runs within a shell, Docker lifecycle commands like <code>docker stop<\/code> may work erratically or not at all. Docker will signal the <em>shell<\/em> to stop, instead of the process within. You can launch your process with <code>exec<\/code> to avoid this.<\/p>\n<pre>ENTRYPOINT exec binary --param --another-param<\/pre>\n<h2 id=\"benefits-of-dockers-entrypoint-approach\"><span class=\"ez-toc-section\" id=\"Benefits_of_Dockers_Entrypoint_Approach\"><\/span>Benefits of Docker\u2019s Entrypoint Approach<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Separating the entrypoint from its arguments helps you hide complexity in your containers. This is most beneficial when you\u2019re creating utility containers to encapsulate CLI programs.<\/p>\n<p>Set your CLI\u2019s binary as the image\u2019s entrypoint. This lets users interact without repeating the binary name in each command.<\/p>\n<p>Consider if we packaged the above <code>Dockerfile<\/code> as <code>date:latest<\/code>:<\/p>\n<pre># default entrypoint (\/bin\/sh -c)&#13;\ndocker run date:latest date +%A&#13;\n&#13;\n# with `date` as the entrypoint&#13;\ndocker run date:latest +%A`<\/pre>\n<p>Setting a custom entrypoint shortens commands and reduces repetition. The container becomes more specialised by invoking <code>date<\/code> automatically. This creates a friendlier interface for your users.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker\u2019s <code>ENTRYPOINT<\/code> and <code>CMD<\/code> instructions are a frequent source of confusion. Their naming masks their intended purposes.<\/p>\n<p>Use <code>ENTRYPOINT<\/code> to set the \u201ccommand\u201d that will execute when new containers start. You can define default arguments using <code>CMD<\/code>. <code>ENTRYPOINT<\/code> and <code>CMD<\/code> are combined together to produce the container\u2019s final command string.<\/p>\n<p>When you use <code>docker run<\/code>, Docker replaces the image\u2019s default <code>CMD<\/code> with the arguments you specify. If you need to override an image\u2019s entrypoint, use the <code>--entrypoint<\/code> flag.\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\/11154\/the-difference-between-cmd-and-entrypoint-in-docker-images\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#The Difference Between CMD and ENTRYPOINT in Docker Images \u2013 CloudSavvy IT&#8221; The CMD and ENTRYPOINT instructions are two commonly confused Dockerfile directives. Both have a role in determining the command which will run when the container starts. The CMD and ENTRYPOINT can be overridden individually within each image. Effective use of these directives makes&#8230;<\/p>\n","protected":false},"author":1,"featured_media":258683,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-258682","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\/258682","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=258682"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/258682\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/258683"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=258682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=258682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=258682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}