DEV Community

Cover image for Containerization and WebServer
Kshitiz Saini
Kshitiz Saini

Posted on

Containerization and WebServer

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
Enter fullscreen mode Exit fullscreen mode
  1. Further we need to start the Docker container using the run command,
docker run -it --name web-server centos
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

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.
/usr/sbin/httpd
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Now we need to put the WebServer files on our Apache WebServer document root which is present at /var/www/html
/var/www/html
Enter fullscreen mode Exit fullscreen mode
  1. Now we can access the files on the IP of the Webserver
http://IP_ADDRESS/FILE:PORT_NO
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Use the docker commit command to create the image of the docker container using the syntax,
docker commit CONTAINER_NAME IMAGE_NAME:VERSION
Enter fullscreen mode Exit fullscreen mode

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

  1. Check if the image is present using the docker images command.
docker images
Enter fullscreen mode Exit fullscreen mode

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

Top comments (2)

Collapse
 
richytong profile image
Richard Tong • Edited

Have you considered building your webserver from a Dockerfile?

Collapse
 
kshitizsaini113 profile image
Kshitiz Saini

Yes, we can also do that, next blog in my series about DockerFile only