{"id":11807,"date":"2019-03-17T11:46:19","date_gmt":"2019-03-17T11:46:19","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11807"},"modified":"2019-03-17T11:46:19","modified_gmt":"2019-03-17T11:46:19","slug":"how-to-setup-a-simple-apache-web-server-in-a-docker-container","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/how-to-setup-a-simple-apache-web-server-in-a-docker-container\/","title":{"rendered":"How to Setup a Simple Apache Web Server in a Docker Container"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/nickjanetakis.com\/assets\/blog\/cards\/get-to-know-dockers-ecosystem-a7dd5ab02fcef5e87b85ef233c599715c8e65646f37a99a17d21a0f8064cc475.jpg\" alt=\"Image result for docker photo\" \/><\/p>\n<p>If you are a Linux system administrator who provides support for developers, chances are you\u2019ve heard of\u00a0<strong>Docker<\/strong>. If not, this software solution will make your life easier beginning today by helping you reduce operating costs and accelerate deployments \u2013 among other benefits.<\/p>\n<p>But it\u2019s not magic.\u00a0<strong>Docker<\/strong>\u00a0as a platform leverages containers \u2013 packages of an application along with all the tools it needs to run to eliminate differences between environments.<\/p>\n<p>In other words, containerized software will operate and can be managed consistently regardless of where it is installed. Additionally, containers are much easier to set up, start, stop, and maintain than good old virtual machines. If you\u2019re interested in knowing more about the differences between these two technologies, the\u00a0<a href=\"https:\/\/www.docker.com\/what-container#\/virtual_machines\" target=\"_blank\" rel=\"nofollow noopener\">official Docker website<\/a>\u00a0provides a great explanation.<\/p>\n<p>To illustrate, in this article we will explain how to install\u00a0<strong>Docker<\/strong>\u00a0on\u00a0<strong>CentOS 7<\/strong>\u00a0and\u00a0<strong>Ubuntu 16.04<\/strong>, and spin up an Apache\u00a0<strong>2.4<\/strong>\u00a0container from\u00a0<strong>Docker Hub<\/strong>.<\/p>\n<p>We will then use it to serve a simple web page from our home directory \u2013 all without the need to install a web server on our host.<\/p>\n<h3>Installing Docker on CentOS and Ubuntu<\/h3>\n<p>To begin, let\u2019s install\u00a0<strong>Docker<\/strong>\u00a0using the following command. This will download and run a shell script that will add the Docker repository to our system and install the package.<\/p>\n<pre># curl -fsSL https:\/\/get.docker.com | sh\r\n<\/pre>\n<p>Next, use\u00a0<strong>systemctl<\/strong>\u00a0command to start the main\u00a0<strong>Docker<\/strong>\u00a0service and check its status.<\/p>\n<pre># systemctl start docker\r\n# systemctl status docker\r\n<\/pre>\n<p>At this point we can simply execute.<\/p>\n<pre># docker\r\n<\/pre>\n<p>to view the list of available commands or to get help.<\/p>\n<pre># docker COMMAND --help\r\n# docker ps --help\r\n<\/pre>\n<p>will tell us how to list containers present on our system, whereas<\/p>\n<pre># docker run --help\r\n<\/pre>\n<p>will print all the options that we can use to manipulate a container.<\/p>\n<h3>Setting Up an Apache Container<\/h3>\n<p>One of the amazing things about the\u00a0<strong>Docker<\/strong>\u00a0ecosystem is that there are tens of standard containers that you can easily download and use. In the following example we will instantiate an\u00a0<strong>Apache 2.4<\/strong>\u00a0container named\u00a0<strong>tecmint-web<\/strong>, detached from the current terminal. We will use an image called\u00a0<strong>httpd:2.4<\/strong>\u00a0from\u00a0<strong>Docker Hub<\/strong>.<\/p>\n<p>Our plan is to have requests made to our public IP address on port\u00a0<strong>8080<\/strong>\u00a0be redirected to port\u00a0<strong>80<\/strong>\u00a0on the container. Also, instead of serving content from the container itself, we will serve a simple web page from\u00a0<strong>\/home\/user\/website<\/strong>.<\/p>\n<p>We do this by mapping\u00a0<strong>\/home\/user\/website\/<\/strong>\u00a0on the\u00a0<strong>\/usr\/local\/apache2\/htdocs\/<\/strong>\u00a0on the container. Note that you will need to use\u00a0<strong>sudo<\/strong>\u00a0or log in as\u00a0<strong>root<\/strong>\u00a0to proceed, and do not omit the forward slashes at the end of each directory.<\/p>\n<pre># sudo docker run -dit --name tecmint-web -p 8080:80 -v \/home\/user\/website\/:\/usr\/local\/apache2\/htdocs\/ httpd:2.4\r\n<\/pre>\n<p>At this point our\u00a0<strong>Apache<\/strong>\u00a0container should be up and running.<\/p>\n<pre>$ sudo docker ps\r\n<\/pre>\n<div id=\"attachment_29858\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/Check-Apache-Docker-Container.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-29858\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/Check-Apache-Docker-Container.png\" sizes=\"auto, (max-width: 1895px) 100vw, 1895px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/Check-Apache-Docker-Container.png 1895w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/Check-Apache-Docker-Container-768x105.png 768w\" alt=\"Check Apache Docker Container\" width=\"1895\" height=\"258\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check Apache Docker Container<\/p>\n<\/div>\n<p>Now let\u2019s create a simple web page named\u00a0<strong>docker.html<\/strong>\u00a0inside\u00a0<strong>\/home\/user\/website directory<\/strong>.<\/p>\n<pre># vi \/home\/user\/website\/docker.html<\/pre>\n<pre><\/pre>\n<p>Add the following sample HTML content to file.<\/p>\n<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;title&gt;Learn Docker at Tecmint.com&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;h1&gt;Learn Docker With Us&lt;\/h1&gt;   \r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Next, point your browser to\u00a0<strong>AAA.BBB.CCC.DDD:8080\/docker.html<\/strong>\u00a0(where\u00a0<strong>AAA.BBB.CCC.DDD<\/strong>\u00a0is your host\u2019s public IP address). You should be presented with the page we created previously.<\/p>\n<div id=\"attachment_29859\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/Check-Apache-Page.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-29859\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/Check-Apache-Page.png\" alt=\"Check Apache Page\" width=\"628\" height=\"202\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check Apache Page<\/p>\n<\/div>\n<p>If you wish, you can now stop the container.<\/p>\n<pre>$ sudo docker stop tecmint-web\r\n<\/pre>\n<p>and remove it:<\/p>\n<pre>$ sudo docker rm tecmint-web\r\n<\/pre>\n<p>To finish cleaning up, you may want to delete the image that was used in the container (omit this step if you\u2019re planning on creating other\u00a0<strong>Apache 2.4<\/strong>\u00a0containers soon).<\/p>\n<pre>$ sudo docker image remove httpd:2.4\r\n<\/pre>\n<p>Note that in all the above steps we never had to install the web server on our host.<\/p>\n<h5>Summary<\/h5>\n<p>In this article we explained how to install Docker and manipulate a container. Unfortunately, these are just the basics \u2013 there are entire courses, books, and certification exams that cover Dockers (and containers in general) more in depth.<\/p>\n<p>If you want to learn more about Docker, we have already covered a 3-article series, that explains how to install Docker, run applications into containers and automatically build docker images with dockerfile.<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/install-docker-and-learn-containers-in-centos-rhel-7-6\/\" target=\"_blank\" rel=\"noopener\">Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 7\/6<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/install-run-and-delete-applications-inside-docker-containers\/\" target=\"_blank\" rel=\"noopener\">How to Deploy and Run Applications into Docker Containers on CentOS\/RHEL 7\/6<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/build-and-configure-docker-container-images-with-dockerfile\/\" target=\"_blank\" rel=\"noopener\">Automatically Build and Configure Docker Images with Dockerfile on CentOS\/RHEL 7\/6<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/remove-docker-images-containers-and-volumes\/\" target=\"_blank\" rel=\"noopener\">How to Remove Docker Images, Containers and Volumes<\/a><\/li>\n<\/ol>\n<p>Consider this as your starting point and let us know if you have any questions or comments \u2013 we look forward to hearing from you!<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/install-apache-web-server-in-a-docker-container\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are a Linux system administrator who provides support for developers, chances are you\u2019ve heard of\u00a0Docker. If not, this software solution will make your life easier beginning today by helping you reduce operating costs and accelerate deployments \u2013 among other benefits. But it\u2019s not magic.\u00a0Docker\u00a0as a platform leverages containers \u2013 packages of an application &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/how-to-setup-a-simple-apache-web-server-in-a-docker-container\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Setup a Simple Apache Web Server in a Docker Container&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11807","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11807"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11807\/revisions"}],"predecessor-version":[{"id":11808,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11807\/revisions\/11808"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}