Docker integration with Python: A newbie’s project
Python whose syntax and libraries made everyone a programmer or a coder, just import the library, a little bit code, and a clean & clear output. “CODER”.
But what happens when python is integrated with scripting languages like HTML, JavaScript, PHP, and so on. Sounds awesome right…
Everyone mostly heard about Flask and Django, but for integration, I used CGI (common gateway interface).
What is CGI?
The common gateway interface (CGI) is a standard way for a Web server to pass a Web user’s request to an application program and to receive data back to forward to the user or in laymen term it allows to use a web page such as HTML in front-end and programming language like python, java in the back-end part.
For more: CGI and a common example are creating a Facebook account where the backend is a database to store your information.
Now comes to my project, what I’ve made? “WebApp for python interpreter”
An interpreter (not live), a program that interprets as many lines you write on the given box/area must have a hundred lines of code. It sounds cool but in actual instead of hundred lines, you can barely count the lines of code on your finger.
Now its time to present the actual code, and for this project, I used Redhat Linux. Steps are as follow:
Step1: Installed a web server because I have to provide service to the client on the web. In my case, I used the Apache webserver(httpd).
Step 2: In Redhat Linux for CGI the default DocumentRoot is /var/www/cgi-bin/ but can be changed by editing the configuration file. All CGI programs must reside in this directory.
Step3: The code- imported CGI and subprocess, “print(“content-type:text/html”)” tells that the content that is going to be written in HTML and “print()” just after that. For more- Python-CGI
In the above figure, the FieldStorage() function provides space to store the data coming from the web, and getvalue(“name”) to get the data from the web. I created another file named ‘ty.py’ to store the data in a file and interpret this file using a python3 interpreter and stored the output into the ‘op’ variable. Print the variable. These lines which are in white colour are the main code of the interpreter and the rest are HTML and CSS. It depends on you how creative you want your page.
Step4: Open the web page- “this is my webpage”.
This part is completed, now Docker comes into play. But a question arises “why docker?”.
Answer:- For hosting a webpage webserver is needed and webserver is a program. To use a program an operating system is needed. But think what happens when your server gets crash or operating system hangs or may get corrupt, or the webpage gets overloaded. And it isn’t good to use an O.S. for a particular program it is just a waste of CPU. “Docker does this part for you, it will install an operating system load it, load the web server in just 1 second”. Yes, you read right in one second.
Basic Commands for Docker : systemctl start docker (to start docker service)
docker images (list of docker images and if you don’t have one, click here to download). Docker uses different images not the normal one that is installed normally.
docker run –i –t — name “anyname” imagename: version
For example docker run -i -t — name webserver centos:7
For more commands, click here
You can create your own docker images by two ways:
First, run an image it will provide an operating system called container and install the program/software/packages you want. Then use a command docker commit ‘container name’ ‘your own image name’: version
For example, I launched container from centos:7 and install webserver inside the container then use the command: ‘docker commit webserver htmlhost:1’
Second, I used dockerfile to create own image :
Then run the command: docker build -t(tag) “your image name”: version “path where dockerfile resides”. And if you are in that folder where the dockerfile reside just use ‘.’ (dot) in the place of path
For example: docker build -t htmlhost:2 /root/Desktop/
And for my project i used a Dockerfile:
docker build -t python:36 .
That's it my whole web server is deployed in an image and I can launch multiple replicas of my web page within a second. And an attack happens on the web page, a hacker doesn't find anything because the container has only web page nothing else. This is called isolation. But every container has a different IP Address so, I will use my base os ip and redirect the port to container’s port.
For example, web server works by default on port number 80 so what i do
docker run -d -t -i -p 80:80 python:36
This is my project, a newbie’s project.
Give a try and smash the code as the Iron man do.