In this article we will see how to convert a simple python application to a containerized (Docker) one
Go to the profile of vignesh ravichandran
Sep 23, 2017
TL;DR -> Containerize a python app. Push the image and execute from Google Cloud.
On a leisurely Saturday afternoon, I thought its nice to play with containers.
I was fascinated with the idea of containerizing an app and running it across different environments. Its easier (for me) to understand something when I do, instead of just reading.
python ≥2.7 (and pip)
An account in docker hub and google cloud. (Both have free tier $)
This is the simple python program which I wanted to containerize.
city = “Los Angeles”
print “I am from %s” %city
r = requests.get(‘http://www.google.com')
Install the package requests using pip to execute this program locally.
Next, I wrote a Dockerfile which basically bundles base image, commands to execute and install other packages.
Then build a docker image by executing docker build -t . You can verify the image by running docker images. (note down the image id) To test the image is working as expected docker run
Didn’t want to stop there. How cool it will be to run the image in a different server!!?.
Alright, you need to push the docker image which you build locally to the docker hub, so that you can pull and run the image from different servers.
Preparation to push the docker image.
docker tag <imageid><dockerHubUserName>/<imageName:latest>
docker login ( provide dockerHubUserName/Password)
docker push <dockerHubUserName>/<imageName>
Successfully, you have pushed a docker image to the docker hub !!.
To really appreciate the use of containerization, you need to run this image from a different environment. Hmm!!? The easiest way is if you have a VM, you can use that (or a different computer). If you don’t have both, then you can spin up a VM from Google Cloud. (or from any other provider).
Note : There are OS images which includes Docker. So, you don’t even have to install Docker in the VM.
Boot Disk : Container-Optimized OS 61–9765.66.0 stable