Search

16 March, 2018

How to use Docker with Python projects



Docker is the modern way to share ready made machines , so other can use it without the usual hassles of configuration and environment setup. I wanted to try it myself first hand, to see what the fuss is about.  I want to share what I found, so you start your project right now.

Before we start, for the absolute beginners who ABSOLUTELY NO IDEA about DOCKER, let me try to explain what it is:

It is a CONTAINER which CONTAINS ALL that you NEED to run your project. 

Dockers are the new generation Virtual Machines which totally remove the need to install another files on a system just to run or test something . They are just CONTAINERS with all the things necessary for our app to work. That's it .


Advantages:

1) User need not worry about , what he needs to install for the project.
2) User doesn't need to know the steps for building the project.
3) User doesn't need to worry about the correct versions and their respective correct libraries that need to be installed.
4) It can easily be shared.
5) Helps deliver project without much effort.

DOCKER INSTALLATION:

I am assuming that you have already installed Docker. If not , you can easily install it from here.
I am going to cover for windows only . My system is a Windows 10 professional (You need windows 10 minimum). Its fairly straight forward for windows . Download the file "Docker for Windows Installer.exe" from the official website. The size is approx 130 MB.

When the installation is finished, in order to check if docker is available, launch your command prompt and type 'docker' and you should see a list of commands. That's great. We are ready.

Note : In order for Docker to run , you must enable 'Virtualization'. You can do that from you BIOS.
Also you need to ensure that the checkbox 'Hyper-V' is check in list of features (Go to Control Panel > Programs > Programs & Features > Turn windows features On and Off. )

THE IDEA:

The basic idea, is to
  • Create a Docker file
  • Write down  everything you need, inside it . 
  • Then build an IMAGE from it. 
  • Run that image. See your program run.

Equivalent steps would be :
  •  "docker build"  - Builds an image
  •  "docker run" - Creates a container and runs it.

Tools for Monitoring:
  • "docker logs" - To see logs
  • "docker ps" - To see what's running.

Let's start with a HELLO World equivalent project that takes a single python file and runs it successfully.

I have a Python file with the following code.

import requests

r = requests.get('http://www.google.com/search?=Docker')
print r.status_code


That's it. Nothing fancy.

Creating our Docker Project

  • Lets create a folder first. I have named mine 'Docker_Prac'. 
  • Create the above python file too in this folder. I have saved the file as ''sample_python.py"
  • Create a file named 'Dockerfile' (no extension).Edit that using Notepad++ (any text editor). Add the following lines.

FROM python:2.7
ADD sample_python.py /
RUN pip install requests
CMD [ "python", "./sample_python.py" ]

Now open command prompt and build your docker image by using the command.

docker build -t 


Cool. Our image is ready to used. Let's run it.

docker run

Admin@DESKTOP-58V2B17 MINGW64 ~/Desktop/Pytrash/Docker_Prac
$ docker run python-requests-proj
200

There's our expected result.

You can see what docker images you have by using

    docker ps -a


$ docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                     PORTS               NAMES
dc1817ec4c22        python-requests-proj   "python ./sample_p..."   6 minutes ago       Exited (0) 6 minutes ago                       gracious_hamilton

There you go . Your first docker project with python is ready.

How do we share this container with others ? 


For this , we need to visit the website https://hub.docker.com and create an account . A free account lets you create one repository. That enough for practice. 

After an account is created , you can go ahead and create a repo. 



No comments:

Post a Comment