Containerization and WebServer

Containerization and WebServer

Kshitiz Saini

Published on Nov 24, 2020

5 min read

Subscribe to my newsletter and never miss my upcoming articles

What is WebServer?

WebServer is a computer that stores webserver software and a website's component files. (for example: HTML, images, scripte etc..) WebServer is a server side software used to satisfy client's request over World Wide Web. Web Server works over protocols like HTTP and processes the incoming request.

If you are not familiar with Docker and have not installed Docker in your device, you can go through my article mentioning installation Containerization and Docker and Getting familiar with Docker.

Setting Apache Server in CentOS container

Running a CentOS container

Running a CentOS container

  1. First we need to pull CentOS image from DockerHub which can be done using the command,
    docker pull centos
  2. Further we need to start the Docker container using the run command,
    docker run -it --name web-server centos
  3. Now we need to install some of the packages in the CentOS container which can be done using the command given
    dnf install ncurses psmisc vim net-tools httpd -y

    ncurses: Installs the clear command

    psmisc: Installs the killall command

    vim: Installs the text editor

    net-tools: Installs the ifconfig and netstat command

    httpd: Installs the Apache Web Server

Now our container is completely setup and ready to use for further configuration.

Configuring the WebServer

Configuring the WebServer

  1. For configuring a WebServer first we need to start the services of the Apache WebServer, but there is a twist in Docker Containers.

    We are unable to use systemctl command in Docker as the containers don't contain PID-1.


    The above command will enable the webservices and further the services can be verified using the netstat command given below as systemctl doesn't work.

    netstat -tnlp
  2. Now we need to put the WebServer files on our Apache WebServer document root which is present at /var/www/html
  3. Now we can access the files on the IP of the Webserver

    If we are using the default 80 port we need not to specify the port number as browser itself use the 80 port.

Making the WebServer permanent

The WebServer will stop when we stop the Docker Container and will not start again if we need to add the services to the .bashrc file to start the services at boot time. We also need to remove the files present on the /var/run/httpd folder as the pid is stored in the specified folder for the current process of httpd running and when we stop the container and start again the processid is not removed.

echo "rm -rf /var/run/httpd/*" >> .bashrc
echo "/usr/sbin/httpd" >> .bashrc

We are updating the .bashrc file as it is the script that gets executed when a user logs in. The file itself contains a series of configurations for the terminal session.

Creating a Docker Image from the container

Creating a Docker Image from the container

  1. Check the docker container info using the docker ps command.
    docker ps
  2. Use the docker commit command to create the image of the docker container using the syntax,

    Following command will create a docker image from the given container.

  3. Check if the image is present using the docker images command.
    docker images

Now the WebServer has been configured on the CentOS container and can be used using the new Docker Image.

Share this