Configuring ApacheWebserver and Setting up Python Interpreter on Docker Container

So what is Docker?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.
So what challenges faced by the industry before docker?
Using different Operating system for performing task increases the security but everything comes with a cost let say if in a company they want to launch a new OS it requires at least 20–30 min and not the only time it also requires at least of 1 Gb RAM, so this a waste of time and resources which is very difficult for any company to bear this cost.
So how docker become the saviour?
Docker is a great tool that uses Containerization Technology, designed to create, deploy, and run containers and container-based applications very quickly. Docker can install our operating system in just 1 second that requires only 20–30 Mb of RAM space which helps the industry as their resources and time won’t get wasted.
So In this article
→ First we will see how to install and run docker services on RedHat OS
→ Second we will configure apache webserver on the docker container
→ And at last we will setup python interpretor and run Python Code on Docker Container
Installing and Running Docker services:-
→ Configuring yum repository: To install anything on the os we first have to configure yum repository so I have created my own yum repository by the name of yash.repo
vi /etc/yum.repos.d/yash.repo

we have configured yum repository which contains docker software
→ Installing docker software: we will use docker community edition as it is open source and for installing we will use the command
yum install docker-ce — nobest -y

This will install our docker software with all dependencies.
→ Starting docker service: for using any service we have to start it and the command to start docker service is
systemctl start docker

After starting the service just check the docker status that it is running or not, using the below-mentioned command
systemctl status docker

Now you can see that Docker has a running state now. But when you will shut down your system and restart again. At that time, you have to restart the docker services again using systemctl start docker. So if you want that you have not to do the same thing again & again then run the below command for enabling docker services permanently.
systemctl enable docker
→ Downloading OS images: Docker requires OS images to launch the docker container. When we download OS image, it by default download image from the docker hub.
To pull the docker images from the Docker-Hub we use the command
docker pull centos

we have downloaded centos image and if don't mention the version name then docker automatically downloads the latest version of the OS.
We can see all the available images using the command
docker images

→Launch/run new docker container: To launch the docker container, we use the below command
docker run -it — name my_os centos:latest

Now container has been successfully launched and also you can see before launching the container “ root@localhost” is our base OS shell but after running the above command we have landed in “root@83b72d4e6d48” container shell
We can also verify the id of the container from Base OS shell using the command
docker ps

We can see the id of the container is “83b72d4e6d48” which is the same as the shell in which we landed after launching new OS using docker.
Configure Apache WebServer on the Docker Container:-
This is the 3 step process to configure any server
→ Step 1 Installing web server: for installing any software we have to configure yum repository but in docker container yum repository is already is configured so we will just have to install webserver using yum
yum install httpd -y

→ Step 2 Configuring Webserver file: Every server has their specific document root from where they read webpages, Similarly in apache web-server, the document root is “/var/www/html/” So we will put our webpage in this “/var/www/html/” directory.
I create a file yash.html inside “/var/www/html” directory
vim /var/www/html/yash.html

→ Step 3 Starting the Service: In docker container, if you want to start the httpd services by systemctl, then it will fail because the container doesn’t provide the “systemctl” command inside the docker container.

Every command run some other command behind the scene so when we start service using systemctl it also runs another command
/usr/sbin/httpd

We want IP of the container to access this webpage using a browser so we use ifconfig command but in the container, we need to first install ifconfig command and the software which provides ifconfig command is net-tools so we will install net-tools software using yum
yum install net-tools -y
Now our ifconfig command will run

Our docker container Ip address is “172.17.0.2” using this IP in the browser with the name of our webpage to access the webpage hosted using the webserver

We can see our Webserver is successfully configured inside a docker container.
Setting up a python interpreter and running Python Code on Docker Container
→ First, we have to start the docker container if it is stopped and for this, we need to know the container id or Container name which we will get using the command
docker ps -a

As we can see my container name is “my_os” so we will use it to start docker container and the command to start container is
docker start -i my_os
We have used “-i” also as it will attach the shell of the container with our base OS so by this we don't have to use another command for attaching container shell with Base OS shell

→ To run python code inside docker container we need to first download and install python interpreter inside a docker container and for this, we will use yum and in the docker container, we get pre-configured yum repository so just need to install python
yum install python3 -y

I have written a python code inside yash.py file

→ For executing this code we will use the command
python3 yash.py

Our code is working fine, So in this way, we can set our python environment to run any python code inside the docker container.
Conclusion:
In this article, we have seen how we can configure the Webserver and set up the Python Interpreter on the top of Docker as well as how we can install the docker software on our local server. So hopefully you have really enjoyed this article.
Thanks for reading this article! Leave a comment below if you have any questions.