Configuring the Webserver and Setting up Python environment on the Docker
By: Sachin Kumar
Tags: Web Server, Docker, Python, Containers, Apache

Designed by Sachin Kumar

Configuring the Webserver and Setting up Python environment on the Docker

Let’s see how we can configure the Webserver and setting up the Python Environment on the top of Docker Container.

Before starting the configuration I think you should know a little bit about Docker. So let’s start with the introduction of Docker without any delay.

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

Installation of Docker

Hopefully, now you have a clear idea of why docker is used by us. So let’s see the installation of docker.

Note: Here I am installing the Docker on the top of AWS Redhat Instance But you can use any operating system with the same approach.

Login with root power using sudo su command and go inside the /etc/yum.repos.d/ directory using the below-mentioned command.

cd /etc/yum.repos.d/

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

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

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

Now you can see Docker has installed on the top of AWS Redhat Instance. So, start the docker services using the below command.

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 in the above screenshot 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

Now Docker installation has done. So you can use all the docker services as you want.

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 services. 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.

The architecture of WebServer on the Docker Container

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.

docker pull imagename:imageversion

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

So launch the docker container using the below command.

docker run -it --name containername imagesname:imageversion

Now container has been launched and also you can see in the above screenshot before launching the container you are seeing root@ip-172-31–41–26 but after running the above command root@3de7caacc4d0 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

Make a webpage according to your comfort.

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.

curl http://containerip/webpage.html

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

You can also check all the images whatever you have inside your docker setup using the below command.

docker images

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@ip-172-31–41–26 but after running the above command root@1a90da6ce750 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 test.py file as mentioned below.

Now run the code using python3 filename.py

So you can see test.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 and AWS instance as well. So hopefully you have really enjoyed this article.

I tried to explain as much as possible. Hope You learned Something from here. Feel free to check out my LinkedIn profile mentioned below and obviously feel free to comment. I write Cloud Computing, Machine Learning, Bigdata, DevOps and Web, etc. blogs so feel free to follow me on Medium.

Thanks, Everyone for reading. That’s all… Signing Off… 😊