{"id":829,"date":"2019-02-16T15:01:25","date_gmt":"2019-02-16T15:01:25","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=829"},"modified":"2024-07-17T10:25:59","modified_gmt":"2024-07-17T10:25:59","slug":"docker-attaching-a-volume-to-a-container","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=829","title":{"rendered":"Docker: Attaching an external volume to a container"},"content":{"rendered":"<p>By default, when we deploy a container using an image, the container is supposed to have all files required to run, and do its job. Let us take an example of a basic appache server running on CentOS.<\/p>\n<p>The process that can be used to bring up this container can be to create a Dockerfile that will create container with base CentOS and install httpd server.\u00a0 The content of the file can be as below:<\/p>\n<p>==== ===<\/p>\n<p><strong>[root@gw20-lap-doc1 attach_ext_vol]# cat Dockerfile<\/strong><br \/>\nFROM centos<br \/>\nRUN yum -y install httpd<br \/>\nENTRYPOINT [&#8220;\/usr\/sbin\/httpd&#8221;, &#8220;-D&#8221;, &#8220;FOREGROUND&#8221;]<\/p>\n<p>==== ===<\/p>\n<p>Let us assume the name of the image that is build from this Dockerfile is\u00a0<strong>myhttpd<\/strong>.<\/p>\n<p>Running the following command will bring the container:<\/p>\n<p><strong>[root@gw20-lap-doc1 sandbox]# docker run -p 80:80 myhttpd<\/strong><\/p>\n<p>You may access the web page by accessing the url of the node hosting the container. However, you will notice that the webpage displayed is the default Appache server&#8217;s page.<\/p>\n<p><span style=\"color: #800000;\"><em><strong>What can be dome to display is a customized web page that displays &#8220;Hello to my page&#8221; ?<\/strong><\/em><\/span><\/p>\n<p>You will require an\u00a0<em>index.html<\/em> file in <em>\/var\/www\/html<\/em> folder in the container.<\/p>\n<p>One way is to create an\u00a0<em>index.html<\/em> file in the node hosting the container and copying it when building the image, so that it adds the file when the container comes up, or the other way is to ensure the container can refer to an\u00a0<em>index.html<\/em> file that is\u00a0in the\u00a0node hosting the container.<\/p>\n<p>&nbsp;<\/p>\n<p>Once you have the <em>index.html<\/em> file\u00a0in the\u00a0node hosting the container, start the image\u00a0<em>myhttpd<\/em> by running the following command:<\/p>\n<p><strong>[root@gw20-lap-doc1 sandbox]# docker run -v $(pwd):\/var\/www\/html\/ -p 80:80 myhttpd<\/strong><\/p>\n<p>Or<\/p>\n<p><strong>[root@gw20-lap-doc1 sandbox]# docker run -v \u00a0-v \/Sandbox\/html:\/var\/www\/html\/ -p 80:80 myhttpd<\/strong><\/p>\n<p>When running this command, if the server in the container searches for any file in\u00a0<em>\/var\/www\/html<\/em> , it will refer to a file with the exact name it is searching for, in the directory from where the\u00a0<em>run<\/em> command is executed.<\/p>\n<p><span style=\"color: #800000;\"><strong>A twist !!<\/strong><\/span><\/p>\n<p>Imagine a scenario, where you have a new docker image that had three folders in\u00a0<em>\/var\/www\/html,\u00a0<\/em>and the same three folders in the node hosting the container too.<\/p>\n<ul>\n<li>dir1<\/li>\n<li>dir2<\/li>\n<li>dir3<\/li>\n<\/ul>\n<p>Now you wanted to start the container using the image. However, you want the container to refer <strong>dir1<\/strong> and <strong>dir2<\/strong> from the local server, and use the <strong>dir3<\/strong> from the copy in the container itself. Here you are planning to change contents of\u00a0<strong>dir1<\/strong>\u00a0and\u00a0<strong>dir2<\/strong>\u00a0hosted in the local server often, and the updated files should be used by the container. The command to use is:<\/p>\n<p><strong>[root@gw20-lap-doc1 sandbox]# docker run\u00a0-v \/var\/www\/html\/dir3 -v $(pwd):\/var\/www\/html\/ -p 80:80 myhttpd<\/strong><\/p>\n<p>How to run it using Docker Compose file ?<\/p>\n<p>The below\u00a0docker-compose.yml can be used to run the same container with the attachments<\/p>\n<p><strong>[root@gw20-lap-doc1 sandbox]# cat docker-compose.yml<\/strong><br \/>\nversion: &#8216;3&#8217;<br \/>\nservices:<br \/>\nmyhttpdserver:<br \/>\nbuild: .<br \/>\nports:<br \/>\n&#8211; &#8220;80:80&#8221;<br \/>\nvolumes:<br \/>\n&#8211; .:\/var\/www\/html<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>By default, when we deploy a container using an image, the container is supposed to have all files required to run, and do its job. <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=829\" title=\"Docker: Attaching an external volume to a container\">[&#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-829","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\/829","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=829"}],"version-history":[{"count":10,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/829\/revisions"}],"predecessor-version":[{"id":1621,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/829\/revisions\/1621"}],"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=829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=829"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}