Using Containerization to setup Webserver using Python programs
In this article, we are going to create a great setup where we will be launching a Docker container that would be capable to execute python programs and setup an Apache web server using some python script. Now, have you ever thought that what is the need of this docker container ? 🤔
Before finding this answer, we need to understand the actual meaning and use of Docker tool and the containerization concept.
What is Docker ?
Earlier whenever we have a requirement to launch an Operating System to perform some experimental tasks or some work, we need to install it as we usually do!!! That take 20–30 minutes (approx.) and resources as well. So in industry use cases there are many situations where we need install the Operating System for 5–10 times or may be more. Isn’t it a great loss of time ?? its obvious that we cannot compromise with the resources, but Time is the most important thing and if we are wasting time then we are wasting money !!!!
For this problem, a great person named “Soloman Hikes” created a great tool in the year 2013 that is capable of installing and preparing fully working Operating System instance to operate and execute some program. The wonder thing with this tool is 🤩🤩 — It will launch and setup everything within 1 sec or may be less 😁Isn’t it exciting !!
Hence, Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. It work on the concept of containerization.
We can launch as many OS instances as we want.
For the implementation, i have setup an RHEL8 Virtual Machine as my base OS that will provide Infrastructure and it will be called as Host Operating System(as depicted in the above picture). Moreover this, i have installed and configured Docker service to create/launch OS instances over the base OS.
Step 1: Check whether the docker is working or not ?
To check its status, we use the command “systemctl status docker”
Step 2: Pull the required image to create a docker container
As our requirement is to execute python program, so need to add/pull a image that can support python!! Here i am going to use the centos:7 image.
To pull a docker image, we use the command “docker pull centos:7”
This downloading may take some time, depends on your internet speed.
To check the available images, we use the command “docker images”.
Step 3: Launching an instance with this image
To launch an instance we use the docker run command.
Step 4: Setup and configure python in it
To setup python , first we need to install it using command “yum install python3 -y”
In my case it is already there!! After install , check its version
Have you noticed that, how wonderful it is !! Everything is configured within some seconds 🤩🤩
Step 5: Create and execute a python program
Here i am creating a python file with mycode.py name
It is mine python code, you can create your own customized code and have fun with python 😎!!
To execute the python file, we use the python3 command
See its wonderful!! It is having its own IP and own network configurations!!😎 Due to these features it is called as a most powerful tool.
Step 6: Exit this OS and check its status
To exit from this OS, we use the command “exit”
After exiting the OS, we again prompted to the base OS and the docker OS is shut down (it won’t be active) but this does not mean that we have lost the instance, it is there in the system but in shutdown/not-active state.
Lets check its configuration using “docker ps -a”
“docker ps” :- Command to get the list of all the instances that are active.
“docker ps -a” :- Command to get the list of all the instances either in active or inactive state.
Step 7: Setup webserver using a python program
Since we have stopped the instance, now wither we can restart the same or create a new one to setup webserver. To start the stopped instance, we use the command “docker exec -it container_name /bin/bash”
Now we will write a python script to setup and configure the webserver.
Now, try to access the web server from the base OS with the link provided in the output.
Hence, we have successfully configured the web server from the python script!! This is a fully automated program that we have created.
Note:- If you are using AWS to perform this practical, then it is good practice to close all the instances.
Hope this would be helpful to you, to perform such great implementations 😇😇
💫Keep Sharing, Keep learning💫
Thank You !!😇😇