What is Docker and how to run a python script on Docker container

Introduction to Docker, Benefits of Docker, Running Docker on windows and executing a python script in a container


Category: Docker Tags: Windows, Container, Python, Docker

python script on Docker container Code Files

Introduction

    Today I was searching for virtualization techniques and I learned about Docker. As I was digging into, it seemed more interesting. After a little research, I ended up installing Docker in my windows machine and a little POC. So, let’s start quickly.

What is Docker?

    Docker is a software platform which can be used to create containers and a container is very much similar to a virtual machine but a container does not create whole virtual operating system rather uses same kernel (and may be other components) of operating system to run the application on which Docker container is running on. The best part, you can create as many containers as you want based on your need and capacity of host machine. 

Why Docker is gaining popularity and who can benefit from it?

  1. Docker is popular in cloud based applications where large applications can run on containers and can be scaled on runtime.
  2. Useful for developers and operations team (DevOps) where a programmer can be more worried about coding instead of system setup and operations team to simplify deployment process.
  3. With Docker one can create custom images of system and code files and the same image can be deployed in different environments like dev, test and production which solves problem of inconsistent behavior of application in different environments.
  4. Some cloud based applications like Kubernetes can use Docker images to scale system based on the need.
  5. There are many prebuild Docker images available publicly, based on need developer can use one and deploy his application on it.

The POC

    In this POC I will create a python container and run python script on it. Let’s do this:

Step 1: Install Docker on your windows machine

link is given below for windows:

    https://docs.docker.com/docker-for-windows/install/

Step 2: Create a folder in local directory

Like I have created a folder “docker” in C drive.

Step 3: Create python script

Create a file "helloworld.py" and paste code below:

print("hello world")

Step 4: Create a Docker file

Create a file named “DockerFile” without any extension and paste lines below:

#publicly available docker image "python" on docker hub will be pulled

FROM python

#creating directory helloworld in container (linux machine)

RUN mkdir c:\home\helloworld

#copying helloworld.py from local directory to container's helloworld folder

COPY helloworld.py /home/helloworld/helloworld.py

#running helloworld.py in container

CMD python /home/helloworld/helloworld.py

Above description of each line is given in comments starting with #. You can search any images publicly available in Docker hub using command: docker search *SearchText*. In my case I ran command in CMD: docker search *python*

I got search results like below and used python image:

DockerHub Search Command
Fig 1: DockerHub Search Command

Step 5: Building the Image using DockerFile

First target the docker directory we created in Setp 2 using command:

cd c:\docker

Once we are inside directory run the command:

docker build -t helloworldapp .

docker build is command to build the docker file available in directory, -t is to tag the name "helloworldapp" to the container.

Now it will perform operation as written in dockerfile, like pulling python image, copying the script file and running the script. And operation will look like:

Building Docker Image using DockerFile
Fig 2: Building Docker Image using DockerFile

If Image of python is available in local system, it will not pull it again and use the same.

Step 6: Run the container

Use the command:

docker run helloworldapp

And Output:

Running Docker container
Fig 3: Running Docker container

 

And that’s it. It is that much simple. Don’t forget to write your thoughts in comment section.


Like 0 People
Last modified about 10 hours ago
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 132
Questions: 9
Given Best Solutions: 9 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x