Configuring ApacheWebserver and Setting up Python Interpreter on Docker Container

Image for post
Image for post

So what is Docker?

So what challenges faced by the industry before docker?

So how docker become the saviour?

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:-

vi /etc/yum.repos.d/yash.repo

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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

systemctl status docker

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

Launch/run new docker container: To launch the docker container, we use the below command

docker run -it — name my_os centos:latest

Image for post
Image for post

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

Image for post
Image for post

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:-

→ 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

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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

/usr/sbin/httpd

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

We can see our Webserver is successfully configured inside a docker container.

Setting up a python interpreter and running Python Code on Docker Container

docker ps -a

Image for post
Image for post

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

Image for post
Image for post

→ 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

Image for post
Image for post

I have written a python code inside yash.py file

Image for post
Image for post

→ For executing this code we will use the command

python3 yash.py

Image for post
Image for post

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:

Thanks for reading this article! Leave a comment below if you have any questions.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store