How to Setup a Simple Apache Web Server in a Docker Container

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.

Installing Docker on CentOS

Run the following command to download and install the package.

curl -fsSL https://get.docker.com | sh

Next, start the docker service and check its status, by running the following command.

systemctl start docker

systemctl status docker

Run the below commands to view the list of available commands and to get help.

Also, will print all available options

docker COMMAND --help

docker ps --help

docker run --help

Setting Up an Apache Container

We will create an instance of Apache 2.4 container and name it as may-apache-app. In this example, our website files and contents are stored in /home/user/website/. We will map the website directory to /usr/local/apache2/htdocs/ on the container.

docker run -dit --name my-apache-app -p 8080:80 -v /home/user/website/:/usr/local/apache2/htdocs/ httpd:2.4

Note that the requests are made to port 8080 will be redirected to port 80 from the above command.

Run the command to view if the container is running or not

docker ps

Output:

[root@vps ~]# docker ps
CONTAINER ID   IMAGE       COMMAND              CREATED          STATUS          PORTS                                   NAMES
b1f57b026341   httpd:2.4   "httpd-foreground"   13 seconds ago   Up 12 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   my-apache-app
[root@vps ~]#

Now let’s create a web page named docker.html inside /home/user/website directory.

vi /home/user/website/docker.html

Add the following content to the file.

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>Learn Docker at crowncloud.com</title>
</head>
<body>
        <h1>This is testing docker with crowncloud</h1>   
</body>
</html>

Enter it into your browser's address bar and it should be presented with the page we created previously.

image

You can stop the container by running the following command.

docker stop my-apache-app

Run the following command to remove the container

docker rm my-apache-app

To finish cleaning up, you may want to delete the image that was used in the container.

docker image remove httpd:2.4