{"id":732,"date":"2018-09-05T05:34:55","date_gmt":"2018-09-05T05:34:55","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=732"},"modified":"2019-01-19T17:49:01","modified_gmt":"2019-01-19T17:49:01","slug":"docker-centos-with-apache","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=732","title":{"rendered":"Docker container: Build images &#8211; Dockerfile 1"},"content":{"rendered":"<p>This tutorial will guide the reader to create a Docker image with apache web server running on a CentOS operating system.<\/p>\n<p>If you are new to Dockers, please click <a href=\"http:\/\/shijuvarghese.com\/?p=722\" target=\"_blank\">here<\/a> to access the installation and basics document.<\/p>\n<p>Create a new folder named \/sandbox\/docker<br \/>\n<em><strong>[root@docker1]# mkdir \/sandbox\/docker<\/strong><\/em><\/p>\n<p>Go to the created folder \/sandbox\/docker<br \/>\n<em><strong>[root@docker1]# cd \/sandbox\/docker<\/strong><\/em><\/p>\n<p>Create a file named &#8220;<strong>.Dockerfile<\/strong>&#8221; with the following lines in italics:<\/p>\n<p>==== ===== ===<\/p>\n<p># FROM is the base image to be used<br \/>\nFROM centos<\/p>\n<p>#RUN to run a command before creating the final image<br \/>\nRUN yum -y install httpd<br \/>\nRUN echo &#8220;Welcome to my webpage&#8221; &gt; \/var\/www\/html\/index.html<\/p>\n<p># COPY to copy a file present in the localhost to the filesystem inside the new image<br \/>\nCOPY .\/shiju.txt \/home<\/p>\n<p>#Command to be run after deploying the comainter with the image<br \/>\nENTRYPOINT [&#8220;\/usr\/sbin\/httpd&#8221;, &#8220;-D&#8221;, &#8220;FOREGROUND&#8221;]<\/p>\n<p>==== ===== ===<\/p>\n<ul>\n<li>The\u00a0<strong>FROM<\/strong> parameter tells what should be the base image to be used<\/li>\n<li>The\u00a0<strong>RUN<\/strong>\u00a0parameter says what commands needs to be\u00a0run on the base image before we build the new image<\/li>\n<li>The\u00a0<strong>CMD<\/strong>\u00a0parameter says what command needs to be run when the\u00a0container\u00a0will be\u00a0deployed using the new image<\/li>\n<li>The\u00a0<strong>WORKDIR\u00a0<\/strong>is a parameter that can be used to mention the\u00a0<em>working directory<\/em> based on which the remaining command\/statements needs to be run<\/li>\n<\/ul>\n<p>Now let us build the initial build named &#8220;centos_1&#8221; using the &#8220;.Dockerfile&#8221; file:<br \/>\n<em><strong>[root@docker1 docker]# docker build -t centos_http -f .Dockerfile .<\/strong><\/em><\/p>\n<p>The\u00a0<span style=\"color: red;\"><strong>-t<\/strong><\/span> is the parameter to\u00a0<em>tag\u00a0<\/em>the image<\/p>\n<p>Now let us run the image, binding port 1337 to view the webpage<\/p>\n<p><em><strong>[root@docker1 docker]# docker run -it -p 1337:80 &#8211;name myContainer centos_http<\/strong><\/em><\/p>\n<p>You should be able to access the default apache web page by accessing &#8220;<strong>http:\/\/&lt;base host IP&gt;:1337<\/strong>&#8221; using a web browser.<\/p>\n<p>The switch <span style=\"color: red;\"><strong>-p<\/strong><\/span> enables all connections coming to TCP port <em>1337<\/em> of the system hosting the docker to be mapped to port <em>80<\/em> of the new container being deployed.<\/p>\n<p><strong><u>Tagging the image:<\/u><\/strong><\/p>\n<p>Another way of tagging the same build image was by running the command:<\/p>\n<p><em><strong>[root@docker1 docker]# docker build -t shiju_docker_id\/redis1:latest -f .Dockerfile3 .<br \/>\n<\/strong><\/em><\/p>\n<p>Here\u00a0<em><strong>latest<\/strong><\/em> is the version number, which is normally written as latest.<\/p>\n<p><em><strong>[root@docker1 docker]# docker ps -all<\/strong><\/em><\/p>\n<p>The way to deploy a container using the image is as follows.<\/p>\n<p><em><strong>\u00a0[root@docker1 docker]#\u00a0docker run shiju_docker_id\/redis1<\/strong><\/em><\/p>\n<p>Theoretically the\u00a0<em>version number\u00a0<\/em>is the actual\u00a0<em>tag<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This tutorial will guide the reader to create a Docker image with apache web server running on a CentOS operating system. If you are new <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=732\" title=\"Docker container: Build images &#8211; Dockerfile 1\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":730,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,25],"tags":[],"class_list":["post-732","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-docker"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=732"}],"version-history":[{"count":16,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/732\/revisions"}],"predecessor-version":[{"id":792,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/732\/revisions\/792"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/730"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=732"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}