Watch all our Tutorials and Training Videos for Free on our Youtube Channel, Get Online Web Tools for Free on

Search Suggest

Dockerize Python Application

Dockerize Python Application, run python on docker, dockerize python app, build python on docker, docker python best practices, python docker,
Dockerize Python Application

As applications become more complex, it can be challenging to ensure they run consistently across different environments. This is where Docker comes in. Docker is an open-source containerization platform that enables developers to package and run their applications in isolated containers. In this article, we will explore how to Dockerize a Python application.


Before we begin, ensure that you have the following installed:

  • Docker
  • Python

Create a Python Application

We will start by creating a simple Python application. In your terminal or command prompt, create a new directory and navigate into it. Then, create a new file called Copy and paste the following code into

from flask import Flask
app = Flask(__name__)

def hello_world():
return 'Hello, World!'

This creates a Flask application with a single route that returns "Hello, World!".

Create a Dockerfile

To Dockerize the application, we need to create a Dockerfile. A Dockerfile is a text file that contains instructions on how to build a Docker image.

Create a new file in the same directory as called Dockerfile. Copy and paste the following code into Dockerfile.

FROM python:3.9
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", ""]

This Dockerfile starts with a base image that contains Python 3.9. It then sets the working directory to /app, copies requirements.txt into the container, installs the requirements using pip, copies the current directory into the container, and sets the command to run

Create a requirements.txt File

We also need to create a requirements.txt file to specify the dependencies for our application. Create a new file called requirements.txt in the same directory as Copy and paste the following code into requirements.txt.


This file specifies that we need Flask version 2.0.1.

Build the Docker Image

Now that we have our application code and Dockerfile, we can build the Docker image. In your terminal or command prompt, navigate to the directory containing, Dockerfile, and requirements.txt. Run the following command to build the Docker image.

docker build -t my-python-app .

This command tells Docker to build a new image with the tag my-python-app using the current directory (.) as the build context.

Run the Docker Container

Now that we have built our Docker image, we can run the container. In your terminal or command prompt, run the following command to start a new container.

docker run -p 5000:5000 my-python-app

This command starts a new container using the my-python-app image and maps port 5000 on the host to port 5000 in the container.

Test the Application

Open your web browser and navigate to http://localhost:5000. You should see the message "Hello, World!" displayed on the page.

Congratulations, you have Dockerized your Python application!

Docker provides a simple and consistent way to package and run applications. In this article, we have explored how to Dockerize a Python application using Dockerfiles and Docker images. By following these steps, you can ensure that your Python application runs consistently across different environments.

Related Searches and Questions asked:

  • How to Install Elasticsearch with Docker
  • Dockerize Java Application
  • How to Deploy a Python Application on Docker
  • Dockerize Springboot Application
  • That's it for this post. Keep practicing and have fun. Leave your comments if any.