Docker Apache Web Server



What is Docker?

Nov 27, 2020 Configure Docker. Start and enable Docker services. Pull the httpd server image from the Docker Hub. Run the httpd container and expose it to the public. Copy the html code in /var/www/html directory and start the Webserver.

How To Start Apache Web Server In Docker Container \ Bind Mount & Persistent Volume \ Docker Compose

  1. In practice, MPMs extend the modular functionality of Apache by allowing you to decide how to configure the web server to bind to network ports on the machine, accept requests from clients, and use children processes (and threads, alternatively) to handle such requests.
  2. Deploy A Static Website with Docker The static websites are the HTML pages, directly severed by any web servers. It may also include some other static assets like css, js and images. In this tutorial you will learn, how to deploy a static website on Docker container.
  3. Configure Docker. Start and enable Docker services. Pull the httpd server image from the Docker Hub. Run the httpd container and expose it to the public. Copy the html code in /var/www/html directory and start the Webserver.

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

What is a container?

A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.
By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.

Apace Webserver:- Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web.

LET’S GET STARTED…………………………….

There are many ways to create a webserver in Docker but we follow then a simple one.

STEP 1:- CREATE A DIRECTORY WEBSERVER

STEP 2:- CREATE AN INDEX.HTML IN WEBSERVER DIRECTORY.

Paste the below code in index.html

At Bangmetric we help you to get the most out of your investments in the latest cutting-edge platforms that allows our customers to harness the power of automation and drive true digital transformation. Bangmetric is founded by a group of experts with years of experience of implementing, optimizing and managing several cloud platforms to a wide variety of clients, ranging from SME’s to global co-operations. Bangmetric are proud of our unique customer-centric approach and deliver exceptional ROI with rapid time to value. Bangmetric’s technical consultants can help you make your IT a proactive business partner.

save and exit

STEP 3:- CREATE A FILE NAME DOCKERFILE IN WEBSERVER DIRECTORY WITH BELOW INSTRUCTIONS (THE NAME DOCKERFILE IS HARDCODED DON’T CHANGE IT)

Dockerfile Commands

  • Maintainer:- This command is used to give information about the author or manager who is managing this image MAINTAINER Bangmetric pvt. ltd.
  • From:- FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with FROM instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.
  • Run:- Before building an image if want some configuration that needs to be present in the image. Inside the image we need to install Apache web server image the command to install that image is “”RUN yum -y install httpd“.
  • Copy:- This command is used to copy a file from host os to docker container
    “”COPY index.html /var/www/html“.
  • EXPOSE:– This command is used to specify the port number in which the container is running its process. Anybody can come from outside and connect to this port. Apache webserver is launched at port 80 by default that is why we need to expose container at port 80. “EXPOSE 80
  • CMD:- To run a command as soon as container is launched. CMD command is different from RUN because RUN is used at the time of building an image and CMD used to run command when container is started.
  • /usr/sbin/httpd:- This command is used to start the web server
  • -D FOREGROUND:- This is not a docker command this is http server argument which is used to run webserver in background. If we do not use this argument the server will start and then it will stop.
  • CMD [“/usr/sbin/httpd”,” -D”,” FOREGROUND”]

STEP 4:- BUILD THE DOCKER IMAGE WITH DOCKER BUILD

Note:- -t option is to tag or name the image and . is used because you are in a directory where Dockerfile is present

STEP 5:- NOW CHECK THE IMAGE WITH DOCKER IMAGES

STEP 6:- NOW RUN THE DOCKER IMAGE

-dit means run container in background -p define the port

STEP 7:- CHECK THE RESULT ON WEBBROWSER <IP:PORT>

CONGRATULATIONS YOU FOLLOW ALL THE STEPS AND SETUP APACHE WEBSERVER IN DOCKER

  • AWS, Docker

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 the host machine, docker uses Linux Containers which directly interacts with the linux kernel like any other process on linux to request resources.

Scenario

Install apache inside a docker container on ec2 instance running Ubuntu (ami-ca381398) and access it using the hosts public IP.

Installing Docker: Run the following commands on terminal to install the docker.

Run following command to verify the successful installation of docker

We would see the following output on terminal which would list the version of docker installed on the system.

Fowarding Traffic: 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):

Replace “DEFAULT_FORWARD_POLICY=”DROP”” with “DEFAULT_FORWARD_POLICY=”ACCEPT” and save the file.

Search Docker Images: 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.

Download Ubuntu Image: This command would download Ubuntu version 14.04 image.

We can get list of all available images on our local system using:

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’s VM image.

Now ,we have Ubuntu image which we would use to run container inside it and install Apache inside this container.

Example: We would take a simple example to run an application inside container which would print “hello”.

docker run: run applications inside containers.

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.
echo “hello”: the command we want to run inside the container.

Server

Docker container would be active until the command we ran is active. We can check the status of the exited/running containers using:

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 “–name” option n the docker run command.

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.

This command would get us an interactive container.

-t : assigns a terminal inside the container.
-i : create a interactive connection with container by grabbing STDIN.
/bin/bash : this command would launch bash shell inside container.

Now we are inside container. Type exit to come out of the container.

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’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.

This commands would create container name “WebServer” and map the ports.

-p: maps the host’s 80 port with 80 port of container

Now we are inside the container. We would need to install Apache inside it and start it.

Now try to access the webserver using the public ip of host system in web browser.

Please verify 80 port is open on the security group of the ec2 instance of host system.

Docker Apache Web Server Tutorial

Now, the web server should work fine.

Press Ctrl+p and Ctrl+q to come out of the container. This would keep the container running behind. We can verify it using

The status WebServer container is “Up” and is running from last 9 minutes.

If we again need to make some changes inside this container, we need to attach to this container using docker attach command.

We can save this “Webserver” Container as image using docker commit command.

-m : for description of image

-a : Name of the Author
8931afa5aaa6 : container’s ip whose image we are creating.
ubuntu:Apache : specified the image name as “ubuntu” and tag as “Apache”

We can see this image using:

Apache Web Server Docker File

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.

Monitoring Apache Web Server Running On Docker

Check out our blog Amazon EC2 Container Service (ECS) – Docker Container Management