Tutorial 5: Running a python file using Docker

Gaurav Patil
3 min readSep 25, 2022
Photo by Rubaitul Azad on Unsplash

Missed previous tutorial ? : click here.

We will cover following topics in following tutorial:

Creating a Dockerfile

Creating a sample.py file using VS code

Building a Docker image

Checking the built image

Creating a Docker container from image

Checking the built container

Creating a Dockerfile :

Create a file named “Dockerfile” (Remember uppercase ‘D’ ) using VS code.

Write following code in a Dockerfile :

FROM pythonRUN mkdir /appWORKDIR /appCOPY sample.py ./CMD [“python”, “sample.py”]

The Docker uses Linux-based commands in a Dockerfile.

FROM python

This command sets the Base Image for subsequent instructions. ‘python’ is a base image in our Dockerfile.

A base image is a starting point for the image that we finally want to create. The choice of base image depends on the requirement of the code.

RUN mkdir /app 

mkdir is Linux-based command which creates a folder named “app”.

WORKDIR /app

WORKDIR sets the working directory in the Docker. So this means folder currently we are in.

COPY sample.py ./

This command copies sample.py file from our local system(your PC) to current path in a Docker image. ./ means working directory we have set above.

CMD [“python”, “sample.py”]

Above command is used to run the python file sample.py copied above.

Creating a sample.py file using VS code :

Write simple print statement in the python file.

print("Hello from python file")

Building a Docker image :

docker build -t <docker_image_name>:<image_version> <path_of_the_Dockerfile>

Run following command to build the Docker image :

docker build -t docker1_image:v1 .

-t means “tag” — to give the image name

docker_image1 is the name of Docker image we are creating

v1 is the version of Docker image we are creating

. is for current path where Dockerfile is. Instead of dot, you can give the path of Dockerfile as well.

You can see following output in the command prompt :

Checking the built image :

We can check the list of all the images we have built using following command :

docker images

You can see following output in the command prompt :

Creating a Docker container from image

To create Docker container from this image we need to run the image, using following command :

docker run --name docker1_container docker1_image:v1

You can see following output in the command prompt :

Output reads as “Hello from python file” which is due to CMD command in a Docker image that is used to run the sample.py file.

Checking the built container :

We can check the list of all the containers we have built using following command :

docker ps -a

We can check the list of all the images we have built using following command :

Congratulations !!!

In this tutorial you learnt to run the python file using Docker. But how to create or edit such python file while Docker container is running and execute there only ? This can be done by running Docker image in an interactive mode.

Take a deep breath and move to the next tutorial.

--

--

Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.