{"id":825,"date":"2023-08-13T22:19:25","date_gmt":"2023-08-13T16:49:25","guid":{"rendered":"https:\/\/www.openwriteup.com\/?page_id=825"},"modified":"2024-04-02T16:41:04","modified_gmt":"2024-04-02T11:11:04","slug":"dockerfile","status":"publish","type":"page","link":"https:\/\/www.openwriteup.com\/?page_id=825","title":{"rendered":"Dockerfile"},"content":{"rendered":"<p>This labs is about creating image from dockerfile.<\/p>\n<p><strong>Youtube Link:<\/strong> <span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/www.youtube.com\/watch?v=HW2Ky13HLCI\">Navigation for lab<\/a><\/span><\/p>\n<pre>mkdir doclab\r\ncd doclab<\/pre>\n<pre><strong>Create a dockerfile using vi editor<\/strong>\r\nFROM debian:stable\r\nLABEL authors=\"amit\"\r\nRUN apt-get update &amp;&amp; apt-get install -y --force-yes apache2\r\nEXPOSE 80<\/pre>\n<pre>- Save the dockerfile\r\n- docker build -t mydeb .<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/dockerbuild.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-826\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/dockerbuild-300x99.jpg\" alt=\"\" width=\"300\" height=\"99\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/dockerbuild-300x99.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/dockerbuild-700x232.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/dockerbuild-768x255.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/dockerbuild.jpg 1119w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<ol>\n<li><code>FROM debian:stable<\/code>: This sets the base image for your Docker image as the official Debian stable image.<\/li>\n<li><code>LABEL authors=\"amit\"<\/code>: This adds a label to your image indicating the author as &#8220;amit&#8221;. Labels are metadata that can be used to provide information about the image.<\/li>\n<li><code>RUN apt-get update &amp;&amp; apt-get install -y --force-yes apache2<\/code>: This command updates the package list and then installs the Apache2 web server package. The <code>-y<\/code> flag automatically confirms any prompts during the installation process, and <code>--force-yes<\/code> is used to force installation without confirmation, which can be risky and is generally not recommended.<\/li>\n<li><code>EXPOSE 80<\/code>: This instruction indicates that the container will listen on port 80. It&#8217;s a declaration of the intended network ports that the container will listen on when running.<\/li>\n<\/ol>\n<pre>docker images\r\ndocker inspect &lt;imageid&gt;<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/inspect.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-827\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/inspect-300x102.jpg\" alt=\"\" width=\"300\" height=\"102\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/inspect-300x102.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/inspect-700x238.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/inspect-768x261.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/inspect.jpg 952w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>COPY and ADD example<\/p>\n<p>Create a docker file dockerfileadd<\/p>\n<pre>touch file.txt\r\nvi dockerfileadd and copy below code, save it\r\n\r\nFROM ubuntu:latest\r\n# Set a working director inside container\r\nWORKDIR \/app\r\n#Copy file from current directory\r\nCOPY file.txt \/app\/\r\n#ADD a directory from the host\r\n#ADD \/home\/lab-user\/amit \/app\/\r\nADD https:\/\/github.com\/amitopenwriteup\/my_proj\/blob\/master\/k8s-setup.txt \/app\/\r\n#Till this point copy it in docker file\r\n\r\n#below command run in the shell\r\n\r\ndocker build -t myweb2 -f dockerfileadd .\r\n\r\ndocker images\r\n\r\ndocker run\u00a0 -it &lt;imageid,from image command&gt; \/bin\/bash\r\nexit<\/pre>\n<p><strong>RUN,CMD and ENTRYPOINT\u00a0<\/strong><\/p>\n<p>Lab1<\/p>\n<pre>1)mkdir test\r\ncd test\r\nvi date.sh\u00a0\r\n\r\n\/\/Copy the below code in date.sh\r\n\r\n#!\/bin\/sh\r\necho `date` $@ &gt;&gt; log.txt;\r\ncat log.txt;\r\n#save date.sh script<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-828\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date1-300x79.jpg\" alt=\"\" width=\"300\" height=\"79\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date1-300x79.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date1.jpg 700w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre>chmod 777 date.sh<\/pre>\n<p>Create a docker file named mydockerfile and copy the below line<\/p>\n<pre>FROM alpine\r\nADD date.sh \/\r\nRUN [\"\/date.sh\", \"image created\"]<\/pre>\n<pre>docker build -t myimage -f mydockerfile .<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-829\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage-300x77.jpg\" alt=\"\" width=\"300\" height=\"77\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage-300x77.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage.jpg 666w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre>docker run myimage cat log.txt\r\nCheck the time<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-830\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date2-300x50.jpg\" alt=\"\" width=\"300\" height=\"50\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date2-300x50.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date2.jpg 668w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>If we run the container several times, we&#8217;ll see that the date in our<br \/>\nlog file doesn&#8217;t change. This makes sense because the run step<br \/>\nexecutes at image build time, not at the container runtime.<\/p>\n<p><strong>Lab2 Create a new docker file named mydockerfile1<\/strong><\/p>\n<pre>FROM alpine\r\nADD date.sh \/\r\nRUN [\"\/date.sh\", \"image created\"]\r\nCMD [\"\/date.sh\", \"container started\"]<\/pre>\n<pre>docker build -t myimage1 -f mydockerfile1 .\r\n\r\ndocker run myimage1<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-831\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage2-300x88.jpg\" alt=\"\" width=\"300\" height=\"88\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage2-300x88.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage2.jpg 584w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>If we run this multiple times, we&#8217;ll see that the image created entry<br \/>\nstays the same. But the container started entry updates with every<br \/>\nrun. This shows how cmd indeed executes every time the container<br \/>\nstarts.<\/p>\n<p><strong>Lab3: create a new dockerfile mydockerfile2 Lets add two cmd commands.<\/strong><\/p>\n<pre>FROM alpine\r\nADD date.sh \/\r\nRUN [\"\/date.sh\", \"image created\"]\r\nCMD [\"\/date.sh\", \"container started\"]\r\nCMD [\"\/date.sh\", \"container running\"]<\/pre>\n<pre>docker build -t myimage2 -f mydockerfile2 .\r\n\r\ndocker run myimage2<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-832\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage3-300x76.jpg\" alt=\"\" width=\"300\" height=\"76\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage3-300x76.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/myimage3.jpg 606w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Lab4 : ENTRYPOINT<\/strong><\/p>\n<p>Create a dockerfile name mydockerfile3<\/p>\n<pre>FROM alpine\r\nADD date.sh \/\r\nRUN [\"\/date.sh\", \"image created\"]\r\nENTRYPOINT [\"\/date.sh\"]<\/pre>\n<pre>docker build -t myimage3 -f mydockerfile3 .\r\n\r\ndocker run myimage3 welcome to container<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-833\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date3-300x49.jpg\" alt=\"\" width=\"300\" height=\"49\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date3-300x49.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/date3.jpg 648w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This labs is about creating image from dockerfile. Youtube Link: Navigation for lab mkdir doclab cd doclab Create a dockerfile using vi editor FROM debian:stable LABEL authors=&#8221;amit&#8221; RUN apt-get update &amp;&amp; apt-get install -y &#8211;force-yes apache2 EXPOSE 80 &#8211; Save the dockerfile &#8211; docker build -t mydeb . FROM debian:stable: This sets the base image [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-825","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=825"}],"version-history":[{"count":10,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/825\/revisions"}],"predecessor-version":[{"id":1412,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/825\/revisions\/1412"}],"wp:attachment":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}