Configuring Apache WebServer on Docker Container.
Agenda of this article:
🔅Configuring HTTPD Server on Docker Container.
To follow the entire process, I have installed docker-ce (community edition) on the top of RHEL8.
And now, let’s carry out the practical…
I am going to use the centos:latest image to launch a container.
Here, I have launched a container using the above command. In this command, -i and -t are used for interactive and terminal. -p is used to expose the container to the outside world (also known as PAT).
I have bind the port 80 of the container with port 8080 of my Base OS i.e., RHEL8.
As you can see below that the HTTPD i.e., the apache web server is not installed in this container. And we have to install it.
Here, since I have used the centos:7 image to launch the container, the yum will be preconfigured in this image. All we need to do is to install the package.
The package has been installed . we can check as shown in image below..
Hence, the httpd package has been installed successfully. Let us move towards the next step, which is: configuring the webserver inside the container.
In the default document root of the httpd i.e. /var/www/html, I have configured the webpage to be deployed. And that is it. As of now, we do not need to configure anything.
The next step will be starting web service inside container.
In this step, we have to start the httpd service. In RHEL8, we use <systemctl start httpd> command to start the web services. But in the docker container, systemctl command won’t work. So, how do we start the service now….?
When we run the <systemctl start httpd> command, behind the scene, this command executes the above file to start the service: </usr/sbin/httpd>. And here, instead of systemctl command, we will directly execute this file to start the httpd service…
And hence, the webservice will be started.
we can also check the webservice using terminal as shown below…
Now, from here, the major part begins. The webservice has been started but it is not permanently enabled. In case, if the container stops or restarts, the werbservice will again become inactive as shown in image below…
Now, we have to start the Httpd service manually again..!
Docker is a tool that is used in DevOps and automation is one of the features of DevOps. And here, we do not want any manual process.
So, in order to permanently enable the webservice, we can write the command to execute the </usr/sbin/httpd> file in the </root/.bashrc> file. This file runs as soon as the container starts and hence, the webservice will become active again..!
But here also, we have one problem. In base OS i.e. RHEL8, we use <systemctl start> command to start the httpd service. This command executes the httpd file due to which the httpd process starts and a PID (process ID) is assigned to this process. This ID is stored in a file called <httpd.pid>.
When we stop the webservice using <systemctl stop> command, this command kills the httpd service and it also deletes the PID as well as other process related files.
But when we talk about the same inside the docker container, we directly executed the </usr/sbin/httpd> file in order to start the service. And since, the process started so, it also got a PID stored in </var/run/httpd/httpd.pid> file.
When we exit the container, the httpd service gets killed. But here, the PID and other process related files do not get deleted. So, next time when we start the httpd service, the PID file will be already there and the system will think that the process is already running and hence, the process will not start.
So, we will be deleting all the process related files at first before starting the service and to automate everything, we will write the commands to do so in the </root/.bashrc> file.
Now, if we stop and start the docker container the web-service will restart as the container will restarts as shown in image below…!
And that was all on the WEBSERVER CONFIGURATION INSIDE DOCKER.
After successfully configuring the webserver inside the docker container, you can also create your own image of webserver using “docker commit” command and then contribute it to other people by pushing it to the DOCKER HUB.