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


    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.


    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:


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 on 20 May 2019
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 164
Questions: 16
Given Best Solutions: 16 *


Nikhil Joshi

POC: Proof of Concept.

Like 0 People on 1 August 2019

Ok. I love that I learned a lot here, but if I make a python script that saves a file, where does it saves it? for printing I see the screen, but I don't know where it comes from, and saving is a different issue. 

Like 1 Person on 27 April 2020
Nikhil Joshi

Hi Gerry,
You may need to look into docker commands i.e. how to connect to container and run commands on container like copying/moving files. Another article which actually host the web app on docker might be helpful:


Copying files from docker:


Docker is not basically designed for copying files from container to host. docker is designed to host applications, and the applications should have interfaces to perform these kind of operations. Like an http endpoint which can list out saved files and could be downloaded on click.

Like 0 People on 28 April 2020
Grant Gualtieri

Hi Nikhil, I have created an ASP.NET core application to serve as an API for a backend service that I am developing. I need to run a python script as part of my service, and I am doing so by running a System.Diagnostics.Process in C#. This works on my machine, but now I need to dockerize the entire application and I was wondering if you would be able to provide any advice about how to acheive this. The script leverages third party libraries and needs python 3 to work so iron python is not really an option.

Like 0 People on 18 December 2020
Nikhil Joshi

Hey Grant,

If you are targeting linux environment then system diagnostic library might not work for you. You may google and try other options. If i get something i will try updating this article with more information soon.


Like 0 People on 18 December 2020

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

Existing User

Login via:

New User