Open In App

How to Run a Python Script using Docker?

Last Updated : 25 Jan, 2024
Like Article

Docker helps you to run your python application very smoothly in different environments with out worrying about underlying platforms. Once you build an image using dockerfile you can run that image where ever you want to run. Docker image will help you to package all the dependencies required for the application which will used for further while running the container.

What is Docker?

Docker is a set of platforms as a service (PaaS) products that use the Operating system level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than a virtual machine. To know more about docker refer to Introduction to Docker.

Key Terminologies

Docker mainly depends on the following three components as mentioned below.:

  1. Dockerfile: Dockerfile is an source code of an docker image. It consists of set instructions to build an docker image.
  2. Image: The instructions which are given in the dockerfile are called layers in the docker image. Docker image consists of several layers according to the no.of instructions you have given.
  3. Container: Docker image helps to build docker containers which are light weight and easy to portable from one platform to another platform. Docker container is an running instance of docker image.

Docker components

Dockerfile Instruction

There is an Docker syntax that you need to follow while writting the dockerfile as shown below:


Most frequently used instructions in dockerfile are as follows:

  1. FROM
  2. RUN
  3. ADD
  4. ENV
  6. CMD

This are the some of the instructions which are regularly used in dockerfile to know more about the syntax of dockerfile refer to What is Dockerfile Syntax?.

Docker Commands

Docker commands make your work easier which will help you to manage the docker containers like removing containers, running docker image as an container,and managing the containers and so on some of the most commonly used docker commands are.

  1. docker run
  2. docker ps
  3. docker stop
  4. docker rm
  5. docker images
  6. docker pull

To know more about docker commands refer to the Docker – Instruction Commands.

Installing Docker

To run docker container first you need to install docker in your local machine you can install docker in different operating systems

Creating Dockerfile For Python Application

Step 1: Creating the Files and Folders

We will be creating a Folder docker_2 at the desktop location in our PC . Inside the Folder another Folder called docker_assignment is created. Then two files dockerfile and are created in this folder.



Step 2: Creating the Dockerfile

Inside the dockerfile we will start by first taking the python base image from docker hub. A tag latest is used to get the latest official python image. It is very important to set your working directory inside your container. I have chosen /usr/src/app. All commands will be executed here as well as the images will be copied here only.

I have then copied the file from my pc to the container current working directory(./ or /usr/src/app)  by using the COPY command. 

#Deriving the latest base image
FROM python:latest

#Labels as key value pair
LABEL Maintainer="roushan.me17"

# Any working directory can be chosen as per choice like '/' or '/home' etc
# i have chosen /usr/app/src
WORKDIR /usr/app/src

#to COPY the remote file at working directory in container
# Now the structure looks like this '/usr/app/src/'

#CMD instruction should be used to run the software
#contained by your image, along with any arguments.

CMD [ "python", "./"]

Build The Docker Image For Python Application

Step 1: Building the Docker Container

After you have created both the Python script and the Dockerfile, you can now use the Docker build command to build your Docker Image.

Here -t is for adding tags so as to identify your image easily.

docker image build -t python:0.0.1 /home/roushan/Desktop/docker_2/docker_assignment

Building docker image

Step 2: Verify the Image Build

After you have built your Docker Image, you can list all the Images to check whether your image has been successfully built or not.

docker images

You will find your Image name listed here and with the tag name, you can easily find it.

Docker images

Run The Docker Container For Python Docker Image

Step 1: Running the Docker Container

Now, you can use the Docker run command to run your Docker Container.

docker run python:0.0.1

Running Docker container

After running the Docker Container, you will see the output printed after adding the two numbers.


To conclude, in this article, we saw how to build a simple addition Python script and run it inside the Docker Container.

FAQs On Running Python Application Using Docker

1. Can I Run An Application On Docker?

Yes you can run you application by using docker by using docker image and docker run command.

2.Can Docker Run A GUI?

We can easily run the most common GUI applications without getting into trouble inside a Docker Container.

3. Is Docker Only For Web Apps?

You can run any type of application as an container using docker. It is well suited for micro service applications

Previous Article
Next Article

Similar Reads

Run Python script from Node.js using child process spawn() method
Node.js is one of the most adopted web development technologies but it lacks support for machine learning, deep learning and artificial intelligence libraries. Luckily, Python supports all these and many more other features. Django Framework for Python can utilize this functionality of Python and can provide support for building new age web applica
3 min read
Run Python Script using PythonShell from Node.js
Nowadays Node.js is the most attractive technology in the field of backend development for developers around the globe. And if someone wishes to use something like Web Scraping using python modules or run some python scripts having some machine learning algorithms, then one need to know how to integrate these two. We are going to get some data of a
3 min read
Docker - Using Public Repositories To Host Docker Images
Pre-requisites: DockerDocker is a software platform for creating isolated virtualized environments for building, deploying, and testing applications with ease. In this tutorial, we will learn how to host public repositories on docker hub which is a hosted repository service provided by Docker for finding and sharing container images. Just like GitH
3 min read
Run One Python Script From Another in Python
In Python, we can run one file from another using the import statement for integrating functions or modules, exec() function for dynamic code execution, subprocess module for running a script as a separate process, or os.system() function for executing a command to run another Python file within the same process. In this article, we will explore al
3 min read
How to Run Another Python script with Arguments in Python
Running a Python script from another script and passing arguments allows you to modularize code and enhance reusability. This process involves using a subprocess or os module to execute the external script, and passing arguments can be achieved by appending them to the command line. In this article, we will explore different approaches to Running a
3 min read
Difference between RUN vs CMD vs ENTRYPOINT Docker Commands
Commands such as CMD, RUN and ENTRYPOINT are interchangeably used when you are writing a dockerfile to create the Docker Image. However, if you have just started using Docker or you don't have enough hands-on experience working with these commands, then these commands might cause a lot of confusion for you. In this article, we are going to discuss
3 min read
Docker Compose Tool To Run aMulti Container Applications
The article talks about how to run multi-container applications using a single command. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can configure a file (YAML file) to configure your docker containers. Then Once you configured the Yaml file with a single command, you create and start all
8 min read
How to Run GUI Based Applications inside Docker?
A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can also save that environment for future use also as
7 min read
Run python script from anywhere in linux
In Linux, there is a way to execute python files from anywhere. This can be done by typing several commands in the terminal. Prerequisite: Basic Shell Commands in Linux Basics of python Steps: At first, open the terminal and go to the home directory. To go the home directory type the following command. cd ~ Create a folder and a python script insid
2 min read
How to run Python script directly in Kivy file?
Kivy is a platform-independent GUI tool in Python. It can run on Android, IOS, Linux and Windows, etc. This is the only GUI library from python which can independently run on the android device even we can use it on Raspberry pi also. It is an open-source Python library for the rapid development of applications that make use of innovative user inte
3 min read
Practice Tags :