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
- First we need to pull CentOS image from DockerHub which can be done using the command,
docker pull centos
- Further we need to start the Docker container using the run command,
docker run -it --name web-server centos
- 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
- 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
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
- Now we need to put the WebServer files on our Apache WebServer document root which is present at /var/www/html
/var/www/html
- Now we can access the files on the IP of the Webserver
http://IP_ADDRESS/FILE:PORT_NO
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
- Check the docker container info using the docker ps command.
docker ps
- Use the docker commit command to create the image of the docker container using the syntax,
docker commit CONTAINER_NAME IMAGE_NAME:VERSION
Following command will create a docker image from the given container.
- 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.
Top comments (2)
Have you considered building your webserver from a Dockerfile?
Yes, we can also do that, next blog in my series about DockerFile only