Configuring (Apache)HTTPD Server & Python on Docker Container
Let’s first understand some of the basic terminologies that we can use while doing this task so that we can understand it better.
1)Apache(HTTPD) Web Server:
Apache HTTPD is one of the most used web servers on the Internet. Apache HTTP Server is a free software/open-source web server for Unix-like systems and other operating systems.
2) Web Server:
A web server is server software or hardware dedicated to running this software, that can satisfy client requests on the World Wide Web. A web server can, in general, contain one or more websites. A web server processes incoming network requests over HTTP and several other related protocols.
Docker is an open platform for developing, shipping, and running applications by using containers. Docker enables us to separate our applications from our infrastructure so we can deliver software quickly. With Docker, we can manage our infrastructure in the same ways as we manage our applications. So, by taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, we can significantly reduce the delay between writing code and running it in production.
or in a way, we can say that Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, it allows applications to use the same Linux kernel as the system that they’re running on and only requires applications to be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.
Containers offer a logical packaging mechanism in which applications can be abstracted from the environment in which they actually run. This decoupling allows container-based applications to be deployed easily and consistently, regardless of whether the target environment is a private data center, the public cloud, or even a developer’s personal laptop. Containerization provides a clean separation of concerns, as developers focus on their application logic and dependencies.
For those coming from virtualized environments, containers are often compared with virtual machines (VMs). We might already be familiar with VMs: a guest operating system such as Linux or Windows runs on top of a host operating system with virtualized access to the underlying hardware. Like virtual machines, containers allow us to package our application together with libraries and other dependencies, providing isolated environments for running our software services.
As we see that below in the image, the similarities end here as containers offer a far more lightweight unit as compare to VMs for developers and IT teams to work with, carrying a lot of benefits.
A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform. It includes the elements needed to run an application as a container — such as code, config files, environment variables, libraries, and run time.
It provides a convenient way to package up applications and pre-configured server environments, which we can use for our own private use or share publicly with other Docker users. If the image is deployed to a Docker environment it can then be executed as a Docker container. The docker run command will create a container from a given image.
Docker images are also reusable assets that can be deployed on any host.
Now, lets Start the Practical:
1)Configuring HTTPD Server on Docker Container
First of all, we need to make sure that Docker is installed on our system. For the following tutorial, we’ll assume that the Docker Community Edition (CE) is installed.
Docker CE is available for all major platforms including macOS, Windows, and Linux. The specific steps needed to install Docker CE on your system can be found at https://docs.docker.com/get-docker/.
Furthermore, you should make sure to create a free account at https://hub.docker.com, so that you can use this account to sign in to the Docker Desktop application.
Once Docker is installed and running on our system we’re able to start by entering the following command on the terminal:
✒️ Command to see the version of docker : docker version
✒️ Command to see the detail info. about the docker container : docker info
Pull the Docker Image from the Docker Hub
For the list of already existing Docker images go to hub.docker.com
For this, we will be using Docker Image of Centos, Version 7. You can also use Centos Docker Image.
Command to see the list of Docker Images present in your local machine : docker images
Now, we have to pull the image from the docker hub so that we can launch our container from that image. So for this, we use docker pull image_name:version
Start the docker service
Command use to start service : systemctl start docker
Command use to see status of our service : systemctl status docker
To make the service started permanent in host os i.e., every time we start the host os and our service is running so for this use the command: systemctl enable docker
Now, we have to launch an container from the above download image i.e centos:7 and for this we use the command : docker run -it centos:7
Now, to configure the webserver on top of the above launch container we have to first install httpd software so, for installing software in redhat there are many ways but the most common is by using the yum command.and in centos:7 yum command is preconfigured .
Step 1: Run command yum install httpd, for installing the httpd server.
But if any error comes while running the above command then run these commands that shown below image :
Now, to confirm the httpd server is successfully installed or not we run the command: rpm -q httpd.
Step 2: Now we need to start httpd services.
In normal Linux OS, we use the systemctl start httpd command to start the service but in container OS this command is not supported as so we can use, /usr/sbin/httpd.
Now, we can create one file that is index.html in directory /var/www/html.
Now, after saving the file we need to restart the httpd service using the command :/usr/sbin/httpd -k restart
Now, if we can open our browser and check our web page by using the URL:
172..17.0.2/index.html it showed our webpage successfully.
2) Setting Up Python Interpreter & running Python Code on Docker Container
Step 1: Run command yum install python3, for installing the python on top of the container.
Now, to confirm that python is successfully installed or not run the command python3.
Here, we can see that our Python code successfully run inside the container running the python3 interpreter.
I have successfully Configuring (Apache)HTTPD Server & Python on top of Docker Container.
In the upcoming days I am going to publish lots of articles on different automation Tools and other Technologies, So definitely follow me on Medium.
Here is my LinkedIn profile if u have any queries definitely comment below or DM me on Linkedin