Configuring the Webserver and Setting up Python Environment on top of Docker.
By: Satwika Gunishetty
Tags: Networking, Docker, Vimal Daga, Righteducation, Makingindiafutureready

Before starting the configuration. I think you should know a little bit about Docker and its uses.

Introduction to 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.

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.

Installation of Docker

Now let us start with how to install docker on RedHat 8 Linux

In this practical, I am using putty program to connect redhat8 Linux which was already in my local system.

Here I am logging in with the root user. Because the root user has full power to access the Linux.

Now to Install docker-engine in our VM.

Go inside the /etc/yum.repos.d/ directory using the below-mentioned command.

cd /etc/yum.repos.d/

2. After going inside the yum.repos.d directory, create a repo file for docker software. So use any editor vim or vi to create the file.

vi docker.repo

3. Now write the below-mentioned script inside the docker.repo file to install the Docker software.

[docker]

baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/

gpgcheck=0

4. After saving the docker.repo file and run the below-mentioned command to install the docker software.

yum install docker-ce --nobest -y

So docker software has installed or not you can check with help of the below command.

rpm -q docker-ce

You can also check whether Docker is installed by seeing the yum repolist and if the docker repository status is the same as the below figure. It is perfectly installed.

yum repolist

5. Now let us start the docker services using the below-mentioned command.

systemctl start docker

To enable docker permanently which means whenever you restart the os. Again to start docker services automatically. Use the below-mentioned command.

systemctl enable docker

To check the docker status

systemctl status docker

Configuration of the Webserver

Before setting up a server you must know the real meaning or definition of a server. So, the Server is a program that provides the client with any kind of service. For example, a web server provides our websites, a database server provides us data. This means every server has work to do and for every different work we want them to do, we have to choose different servers. So let’s see which steps we have to do to configure the WebServer. Also, web server architecture will be solved here as the below mentioned.

3 Steps to Configure a WebServer

Install the Server Program

Configuration of Server

Start the server

So let’s see step by step how to configure the server on the docker. But before configuring, pull the docker image from DockerHub using the below-mentioned command.

Note: You can pull any image from the docker hub which you want using the same below command. If we don’t give any version it will automatically downloads the latest version of os image.

docker pull imagename:imageversion

Note: docker images the command is used to see the docker images whatever your docker has.

Docker Network Architecture and Usage of Patting for launching Webserver

Before launching the webserver we should know about Patting and why we should use Patting for launching webserver in docker. And the Network Architecture of Docker Engine

When we launch containers with docker it behind the scenes assign to one lab known as Network. with one switch, one router, NIC to a container with SNAT enabled which is based on the facility of SDN (Software Defined Network). This complete setup for containers within the network is known as Docker Host Or wherever the docker container is launched and used, those devices are known as Docker host.

In Docker Network DNAT is not by-default enabled means anyone from outside cannot ping to the containers inside it. To connect to docker from outside we require the concept of Port Mapping that can be achieved by PATing .The combination of the switch along with the router is considered as the bridge adapter.

PAT stands for Port Address Translation could be used to associate a device within the network with the outside world(Internet). Request here is redirected to the device within the network using Port Number. PAT’ing is used to share a single IP public address between multiple clients who need to use the Internet publicly. It is an extension of network address translation (NAT).

We use the following command to enable the PAT’ing

docker run -it --name w1 -p <portno>:<your desired port> centos:latest

Let's Now Launch the Webserver. In this practical, I am setting 2222 port to use port 80 services of the docker host.

Now container has been launched and also you can see in the above screenshot before launching the container you are seeing root@localhost but after running the above command root@b2239ee5a830 has taken place.

Now the container is ready for the configuration of the Webserver So install the webserver using the below-mentioned command.

yum install httpd -y

After installing the Webserver go inside the /var/www/html directory and make a webpage as the below mentioned.

cd /var/www/html/ //change directory to document root

vim index.html // to write a simple html code

After making the webpage you have to start the webserver but when you will start it in a regular way then you will face this type of problem. So resolving this problem run the below command.

/usr/sbin/httpd

Copy your container IP and try to access the webpage through your local server using the below-mentioned command.

Now let us check the same webserver in docker host whether it is working

REDHAT8 Linux OS-Docker Host

Now Letus check the same Web server in the windows host

As you observed the docker container IP(172.17.0.2) cannot be pingable to windows host due to docker host has disabled the DNAT rule. To solve this problem we are using the concept of PATTING (Port Adress Translation). Where docker host Ip (192.168.0.114:2222)will go to Windows host to ping the docker container by assigning a unique port number to each container. 2222 is a port number that was assigned to the docker container while launching to get port 80 services of the docker host.

We are able to access the webpage now. So Webserver has configured.

Setting up the Python Environment

let’s see how we can set up the python environment as well as how python works on the docker container. but before setting up the python environment you have to launch a docker container first. So let’s start.

First of all, you have to pull the image from the docker hub using the below-mentioned command.

docker pull imagename:imageversion

Now launch the docker container using the below command.

Note: Here containername you can give according to comfort.

docker run -it --name containername imagesname:imageversion

Now container has launched and also you can see in the above screenshot before launching the container you are seeing root@localhost but after running the above command root@43cc0fa4625f has taken place.

So now install python in the container using the below command.

dnf install python3 -y

Check the Python has installed or not using python3 -V

Make workspace for the python as mentioned in the below screenshot and write any python code.

So I have written a small code inside my t.py file as mentioned below. Run the code using python3 filename.py

So you can see the t.py file has successfully run.

Conclusion

In this article, we have seen how we can configure the Webserver and set up the Python environment 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.

www.linkedin.com/in/satwika-gunishetty-b50b19196

Thanks, Everyone for reading. That’s all…