{"id":17732,"date":"2015-03-02T18:56:33","date_gmt":"2015-03-02T13:26:33","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17732"},"modified":"2016-08-30T11:06:41","modified_gmt":"2016-08-30T05:36:41","slug":"docker-scenario-installing-and-using-apache-web-server-in-docker-container","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/docker-scenario-installing-and-using-apache-web-server-in-docker-container\/","title":{"rendered":"DOCKER (Scenario: Installing and using Apache web server in docker container)"},"content":{"rendered":"<h3 style=\"text-align: justify; color: #ff9900;\"><\/h3>\n<h3 style=\"text-align: justify; color: #ff9900;\">Introduction<\/h3>\n<p><a title=\"docker devOps\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">Docker<\/a> is an open source platform which can be used to run\/test applications in an isolated environment on the host system. Docker could be defined as an <a title=\"aws automation\" href=\"http:\/\/www.tothenew.com\/devops-aws\">implementation of container<\/a> using the concept of images.<\/p>\n<p>Unlike VM which is a complete OS intalled on host system managed by hypervisors and needs dedicated resources from the host machine, docker uses Linux Containers which directly interacts with the linux kernel like any other process on linux to request resources.<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">Scenario<\/h3>\n<p>Install apache inside a docker container on ec2 instance running Ubuntu (ami-ca381398) and access it using the hosts public IP.<\/p>\n<p><strong>Installing Docker:<\/strong> Run the following commands on terminal to install the docker.<\/p>\n<p>[js]sudo apt-get update<br \/>\nsudo apt-get install docker.io<br \/>\nsource \/etc\/bash_completion.d\/docker.io #to enable tab-completion of Docker commands in BASH[\/js]<\/p>\n<p>Run following command to verify the successful installation of docker<\/p>\n<p>[js]sudo docker info<br \/>\n[\/js]<\/p>\n<p>We would see the following output on terminal which would list the version of docker installed on the system.<\/p>\n<p>[js]Client version: 1.0.1<br \/>\nClient API version: 1.12<br \/>\nGo version (client): go1.2.1<br \/>\nGit commit (client): 990021a<br \/>\nServer version: 1.0.1<br \/>\nServer API version: 1.12<br \/>\nGo version (server): go1.2.1<br \/>\nGit commit (server): 990021a<br \/>\n[\/js]<\/p>\n<p><strong>Fowarding Traffic:<\/strong> We need to foward all traffic to docker which we be required as we are running apache web server inside docker container.Enable forwarding with UFW(Uncomplicated Firewall):<\/p>\n<p>[js]sudo vi \/etc\/default\/ufw<br \/>\n[\/js]<\/p>\n<p>Replace \u201cDEFAULT_FORWARD_POLICY=&#8221;DROP&#8221;\u201d with \u201cDEFAULT_FORWARD_POLICY=&#8221;ACCEPT&#8221; and save the file.<\/p>\n<p><strong>Search Docker Images:<\/strong> We would need to download Ubuntu image on host Ubuntu system on which we would install Apache web server. Varoius customize docker images are available on Docker repository which could be searched using the following command. This command would search and list all images having Ubuntu string in their name.<\/p>\n<p>[js]sudo docker search ubuntu<br \/>\n[\/js]<\/p>\n<p><strong>Download Ubuntu Image:<\/strong> This command would download Ubuntu version 14.04 image.<\/p>\n<p>[js]sudo docker pull ubuntu:14.04<br \/>\n[\/js]<\/p>\n<p>We can get list of all available images on our local system using:<\/p>\n<p>[js]sudo docker images<br \/>\nREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE<br \/>\nubuntu 14.04 2d24f826cb16 8 days ago 192.7 MB<br \/>\n[\/js]<\/p>\n<p>In the above output, we can see the image id (2d24f826cb16) which is unique for every image. Just to compare, the size of this image is 192.7MB which is very small than than it&#8217;s VM image.<\/p>\n<p>Now ,we have Ubuntu image which we would use to run container inside it and install Apache inside this container.<\/p>\n<p><strong>Example:<\/strong> We would take a simple example to run an application inside container which would print \u201chello\u201d.<\/p>\n<p>[js]sudo docker run 2d24f826cb16 echo &quot;hello&quot;<br \/>\n[\/js]<\/p>\n<p>docker run: run applications inside containers.<\/p>\n<p>2d24f826cb16: The image which we want to use to run container inside. We have used image id of the downloaded Ubuntu. We can either use image id or image name i.e. ubuntu:14.04.<br \/>\necho &#8220;hello&#8221;: the command we want to run inside the container.<\/p>\n<p>Docker container would be active until the command we ran is active. We can check the status of the exited\/running containers using:<\/p>\n<p>[js]docker ps -a<br \/>\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES<br \/>\n6cd7aacb64fb ubuntu:14.04 echo hello 9 minutes ago Exited (0) 1 minutes ago backstabbing_sinoussi<br \/>\n[\/js]<\/p>\n<p>The above output says container with id 6cd7aacb64fb has been exited a minute ago. Like images, containers do have unique container id and name (backstabbing_sinoussi in this case). These name are automatically generated by docker. We can provide custom name using &#8220;\u2013name&#8221; option n the docker run command.<\/p>\n<p>In our case we need a container where we can install Apache web server and configure it the way we want. To achieve this we need an interactive container.<\/p>\n<p>[js]sudo docker run -t -i ubuntu:14.04 \/bin\/bash<br \/>\n[\/js]<\/p>\n<p>This command would get us an interactive container.<\/p>\n<p>-t : assigns a terminal inside the container.<br \/>\n-i : create a interactive connection with container by grabbing STDIN.<br \/>\n\/bin\/bash : this command would launch bash shell inside container.<\/p>\n<p>Now we are inside container. Type exit to come out of the container.<\/p>\n<p>Before moving ahead, we would need the 80 port of host machine to be mapped with the 80 port of our container since we would be using host&#8217;s public ip and 80 port to take in requests and host would forward these requests to the Apache web server running at port 80 of container.<\/p>\n<p>This commands would create container name \u201cWebServer\u201d and map the ports.<\/p>\n<p>[js]sudo docker run &#8211;name WebServer -p 80:80 -t -i ubuntu:14.04 \/bin\/bash<br \/>\n[\/js]<\/p>\n<p>-p: maps the host&#8217;s 80 port with 80 port of container<\/p>\n<p>Now we are inside the container. We would need to install Apache inside it and start it.<\/p>\n<p>[js]apt-get update<br \/>\napt-get install apache2<br \/>\nservice apache2 start<br \/>\n[\/js]<\/p>\n<p>Now try to access the webserver using the public ip of host system in web browser.<\/p>\n<p>Please verify 80 port is open on the security group of the ec2 instance of host system.<\/p>\n<p>Now, the web server should work fine.<\/p>\n<p>Press Ctrl+p and Ctrl+q to come out of the container. This would keep the container running behind. We can verify it using<\/p>\n<p>[js]sudo docker ps -a<br \/>\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES<br \/>\n8931afa5aaa6 ubuntu:14.04 \/bin\/bash 9 minutes ago Up 9 minutes 0.0.0.0:80 80\/tcp WebServer<br \/>\n[\/js]<\/p>\n<p>The status WebServer container is \u201cUp\u201d and is running from last 9 minutes.<\/p>\n<p>If we again need to make some changes inside this container, we need to attach to this container using docker attach command.<\/p>\n<p>[js]sudo docker attach WebServer<br \/>\n[\/js]<\/p>\n<p>We can save this &#8220;Webserver&#8221; Container as image using docker commit command.<\/p>\n<p>[js]sudo docker commit -m &quot;Apache web server&quot; -a &quot;Navjot Singh&quot; 8931afa5aaa6 ubuntu:Apache<br \/>\n[\/js]<\/p>\n<p>-m : for description of image<\/p>\n<p>-a : Name of the Author<br \/>\n8931afa5aaa6 : container&#8217;s ip whose image we are creating.<br \/>\nubuntu:Apache : specified the image name as \u201cubuntu\u201d and tag as \u201cApache\u201d<\/p>\n<p>We can see this image using:<\/p>\n<p>[js]sudo docker images<br \/>\nREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE<br \/>\nubuntu Apache dcc12acbb267 14 seconds ago 227.6 MB<br \/>\nubuntu 14.04 2d24f826cb16 8 days ago 192.7 MB<br \/>\n[\/js]<\/p>\n<p>We would continue with some more scenarios in the next blog which would cover the creating image using dockerfile and allowing communication between two docker containers.<\/p>\n<p>Check out our blog \u00a0<a href=\"http:\/\/www.tothenew.com\/blog\/amazon-ec2-container-service-ecs-docker-container-managment\/\">Amazon EC2 Container Service (ECS) \u2013 Docker Container Management<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Docker is an open source platform which can be used to run\/test applications in an isolated environment on the host system. Docker could be defined as an implementation of container using the concept of images. Unlike VM which is a complete OS intalled on host system managed by hypervisors and needs dedicated resources from [&hellip;]<\/p>\n","protected":false},"author":154,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[1174],"tags":[3866,1883],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17732"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/154"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17732"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17732\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}