{"id":208,"date":"2020-04-03T10:51:01","date_gmt":"2020-04-03T10:51:01","guid":{"rendered":"http:\/\/shresthabrijan.com.np\/?p=208"},"modified":"2020-04-03T11:22:36","modified_gmt":"2020-04-03T11:22:36","slug":"lets-talk-about-dockerfile","status":"publish","type":"post","link":"https:\/\/shresthabrijan.com.np\/?p=208","title":{"rendered":"Let&#8217;s talk about Dockerfile"},"content":{"rendered":"\n<p>Docker provides a way to build an image by reading a set of instructions. Dockerfile is the file containing such instructions which is read by <em><strong>docker build<\/strong> <\/em>command to generate an image.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$docker build &lt;context&gt;\n\nThe command, docker build, is passed with either of two context, PATH or URL. \nPath is the location in local filesystem whereas URL is git repo location. \nThis makes the docker build pass the content of whole context to docker daemon recursively.\n\nUsage:\n$docker build -f \/path\/to\/Dockerfile \/project\n$docker build -t newproject .\n\nNote:\nEach line of instruction inside Dockerfile is executed independently and if required separate image is generated for each command simultaneously until the final image is created\nDocker excludes files and dirs listed in .dockerignore file while sending the context.<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Inside the Dockerfile<\/h5>\n\n\n\n<p>Instructions inside dockerfile are usually entered in following format:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Comment\nINSTRUCTION arguments<\/pre>\n\n\n\n<h6 class=\"wp-block-heading\"><strong> Common Instructions <\/strong><\/h6>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>FROM<\/strong><br><br>Every dockerfile must begin with a FROM instruction which defines the parent image with which the subsequent image is to be built. However, it may be preceded by  parser directive, comment and\/or global scope ARGs<br><br>Usage:<br>FROM [&#8211;platform=&lt;platform&gt;] &lt;image&gt;[:&lt;tag&gt;] [AS &lt;name&gt;]<br><br>&#8211;platform flag can be used to specify platform of an image<br>Specifying image name helps inf future reference of image built in the stage<br>E.g. FROM &#8211;platform=linux\/amd64 Ubuntu:18.04 AS newimage<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>RUN<\/strong><br><br>RUN instruction is used to execute command and commit the results on top of current image as a result it can be used in next set of instructions. <br><br>Usage:<br>RUN &lt;command&gt; <br>This will execute the command using shell which which by default is&nbsp;\/bin\/sh -c&nbsp;on Linux or&nbsp;cmd \/S \/C&nbsp;on Windows<br>e.g. RUN echo Hello!<br><br>RUN [&#8220;executable&#8221;, &#8220;param1&#8221;, &#8220;param2&#8221;]<br>To use other than shell to run commands, this format is used<br>e.g. RUN [&#8220;D:\\script.bat&#8221;, &#8220;arg&#8221;]<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>CMD<\/strong><br><br>This instruction is use to define defaults for an executing container as a result there can be only one of it. Multiple declaration of CMD in a dockerfile will automatically use the last CMD as default. <br><br>Usage:<br>CMD [&#8220;executable&#8221;,&#8221;param1&#8243;,&#8221;param2&#8243;]<br>This is the exec form for running CMD. <br>e.g. CMD [&#8220;\/bin\/ls&#8221;, &#8220;-l&#8221;]<br><br>CMD [&#8220;param1&#8243;,&#8221;param2&#8221;]<br>This is used to pass arguments to ENTRYPOINT. <br><br>CMD command param1 param2<br>This form is used to execute command in a shell.<br>e.g. CMD echo &#8220;Hello World!&#8221;<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>ENTRYPOINT<\/strong><br><br>Defining ENTRYPOINT allows to configure a container to run a command on start of container similar to CMD but the major difference is arguments passed while running container is appended to ENTRYPOINT command while the CMD command is completely replaced by them.<br>i.e. For <code><span class=\"has-inline-color has-very-dark-gray-color\"><em>$docker run &lt;image&gt; arg1<\/em><\/span><\/code>&nbsp;, arg1 will be appended on ENTRYPOINT whereas it will replace CMD.<br><br>Usage:<br>ENTRYPOINT [&#8220;executable&#8221;, &#8220;param1&#8221;, &#8220;param2&#8221;]<br>This is termed exec form and is parsed as JSON array.  It can use CMD to set extra parameters which can be changed by user while running a container.<br>e.g.<br>ENTRYPOINT [&#8220;\/bin\/ls&#8221;, &#8220;-l&#8221;]<br>CMD [&#8220;-a&#8221;]<br><br>ENTRYPOINT command param1 param2<br>This is termed as shell form and executes the command using shell. To ensure proper exit, command should be preceded by exec.<br>e.g. ENTRYPOINT exec ls -l <\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>EXPOSE<\/strong><br><br>The EXPOSE instruction defines a port that the container listens on. By default, it assumes tcp port unless defined specifically.<br>e.g. EXPOSE 80\/tcp or EXPOSE 1234\/udp<br><br>Note:<br>The ports defined by EXPOSE are not published by default. User has to manually define ports using -p flag during docker run or explicitly publish all exposed ports using -P. So, this is more like a documentation for maintainers.<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>ENV<\/strong><br><br>As the name suggests, this instruction sets environment variable which remains persistent during container creation.<br><br>Usage:<br>ENV &lt;key&gt; &lt;value&gt;<br>ENV &lt;key&gt;=&lt;value&gt;<br><br>e.g. ENV name Fname Lname<br>        ENV name=Fname\\ Lname address=&#8221;Home Town&#8221;<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\">   <strong>ADD<\/strong><br><br>This instruction copies files and dirs from given source in context to the image. The source can be local filesystem or remote url as well.<br><br>Usage:<br>ADD &lt;src&gt; &lt;dest&gt;<br>e.g. ADD testdir \/project<br>        ADD https:\/\/example.com\/repo \/project<br><br>For linux containers, ADD supports [&#8211;chown=&lt;user&gt;:&lt;group&gt;] parameter for hadnling user and group administration<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>COPY<\/strong><br><br>Copy does the same job as ADD but does not supports URL copying.<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>VOLUME<\/strong><br><br>VOLUME instruction creates mount point in the image with the name defined in parameter which can be referred by other containers. <br><br>Usage:<br>VOLUME [&#8220;\/data&#8221;]<br>e.g. VOLUME \/newvol<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>WORKDIR<\/strong><br><br>This instruction sets working directory which is used by other instructions. A container created using the image will start in the location defined in WORKDIR. <br><br>Usage:<br>WORKDIR \/path\/to\/workdir<\/p>\n\n\n\n<p class=\"has-background has-cyan-bluish-gray-background-color\"><strong>USER<\/strong><br><br>USER instruction helps to define user and optionally group which will be used to run commands during image creation.<br><br>Usage:<br>USER &lt;user&gt;[:&lt;group&gt;] or<br>            &lt;uid&gt;[:&lt;gid&gt;]<br>e.g. USER foo<\/p>\n\n\n\n<p>For more details on the Dockerfile, visit the docker <a href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\/#dockerfile-reference\">documentation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker provides a way to build an image by reading a set of instructions. Dockerfile is the file containing such instructions which is read by docker build command to generate an image. $docker build &lt;context&gt; The command, docker build, is passed with either of two context, PATH or URL. Path is the location in local [&hellip;]<\/p>\n<div class=\"read-more\"><a href=\"https:\/\/shresthabrijan.com.np\/?p=208\" class=\"btn btn-primary btn-lg\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-208","post","type-post","status-publish","format-standard","hentry","category-system-administration"],"_links":{"self":[{"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=208"}],"version-history":[{"count":14,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":222,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions\/222"}],"wp:attachment":[{"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shresthabrijan.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}