Image for post
Image for post
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.

Sachin Kumar
Nov 1, 2020 · 7 min read

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

Image for post
Image for post

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 sucommand and go inside the /etc/yum.repos.d/ directory using the below-mentioned command.

cd /etc/yum.repos.d/
Image for post
Image for post

After going inside the yum.repos.d directory, create a repo file for docker software. So use any editor vimor vito create the file.

vi docker.repo
Image for post
Image for post

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

Image for post
Image for post

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

yum install docker-ce --nobest -y
Image for post
Image for post

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

rpm -q docker-ce
Image for post
Image for post

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
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 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 dockerso 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
Image for post
Image for post

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.

Image for post
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
Image for post
Image for post

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
Image for post
Image for post

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–26but after running the above command root@3de7caacc4d0has 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
Image for post
Image for post

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

cd /var/www/html
Image for post
Image for post

Make a webpage according to your comfort.

Image for post
Image for post

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.

Image for post
Image for post

So resolving this problem run the below command.

/usr/sbin/httpd
Image for post
Image for post

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

curl http://containerip/webpage.html
Image for post
Image for post

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
Image for post
Image for post

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

docker images
Image for post
Image for post

Now launch the docker container using the below command.

Note: Here containernameyou can give according to comfort.

docker run -it --name containername imagesname:imageversion
Image for post
Image for post

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–26but after running the above command root@1a90da6ce750has taken place.

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

dnf install python3 -y
Image for post
Image for post

Check the Python has installed or not using python3 -V

Image for post
Image for post

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

Image for post
Image for post

So I have written a small code inside my test.py file as mentioned below.

Image for post
Image for post

Now run the code using python3 filename.py

Image for post
Image for post

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.

Image for post
Image for post

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… 😊

Hackcoderr’s Hub

Upgrade your knowledge with Hackcoderr

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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