Saturday, January 9, 2016

Docker – Apache HTTPD with Docker

As we already discussed that docker containers are mainly used as a application package containers. These are designed to package and run a single service. In this article we will see how we can run a single HTTP server inside a docker container

The outline for the steps to follow is
1) Download a latest Copy of the Ubuntu image
2) install and Configure apache2 on them
3) Commit the Image
4) Start the Apache server on foreground with a Port to access
5) Access the apache server.

1) Download the latest ubuntu using the “docker run” command

[root@vx111a work]# docker run ubuntu /bin/echo Hello World
Unable to find image 'ubuntu:latest' locally
Trying to pull repository ... latest: Pulling from library/ubuntu
9377ad319b00: Pull complete
a82f81f25750: Pull complete
b207c06aba70: Pull complete
d55e68e6cc9c: Pull complete
Digest: sha256:a2b67b6107aa640044c25a03b9e06e2a2d48c95be6ac17fb1a387e75eebafd7c
Status: Downloaded newer image for

Hello World

The above command not only downloads the ubuntu latest image and it also make sure to run the echo command once the image is downloaded successfully.

Once the Image is downloaded, start the image using,

docker run -t -i /bin/bash
keep running and in other console run the below commands

This will start the Container and take you to the bash prompt in the container.

2) Run the installation of apache commands as
Apt-get update
Apt-get install apache2

This will download the packages and it will ask if we want to raise the container size. Enter yes and it will take some time to download the packages.

Install the w3m packages to as this is needed for the apache to run successfully. Make sure the apache2ctl command Is available at /usr/sbin/apache2ctl.

3) The next step is commit the image so that we can use that multiple times. Now make sure the above ubuntu container is running by checking the “docker ps” command.

Once we got the ubuntu container running, take the container ID and commit the image as

[root@vx111a work]# docker commit 9b708c78e088 apache2/test

Now we have committed the ubuntu image which has apache2 installed as apache/test.
We can check this using the “docker images” command

4) Start the Container with apache on a Port

We need to run the apache process in the foreground, since our container will stop when the process specified in the docker run command stops. We can do this with a flag -D when starting the apache2 process. The default apache install will be running on port 80. To give our container access to traffic over port 80, we use the -p flag and specify the port on the host that maps to the port inside the container. In our case we want 80 for each, so we include -p 8888:8888 in our command as

[root@vx111a work]# docker run -d -p 8888:8888 apache2/test /usr/sbin/apache2ctl -d FOREGROUND

5) Test the apache by entering the http://localhost:8888 in the host browser. We can see the apache Home page.

This is how we can run apache Httpd Server in a Docker Container. More to Come ,happy learning J

No comments :

Post a Comment