Dockerizing a Python Application: A Step-by-Step Guide

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Dockerizing your Python application can provide numerous benefits, including consistent deployment environments, ease of scaling, and simplified dependency management. This guide will walk you through the process of creating a Docker image for your Python application.

Prerequisites

  • Python and pip installed on your machine
  • Docker installed on your machine
  • An existing Python application or create a new one
  • IDE (Visual Studio Code, PyCharm, etc.)

Step 1: Create a Python Application

If you don't have an existing Python application, you can create a simple one.

Open your terminal and run the following commands:

mkdir my-python-app cd my-python-app 

Create a file named app.py in the root directory of your project:

from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Docker!' if __name__ == '__main__': app.run(host='0.0.0.0', port=5000) 

This will create a simple Flask application that listens on port 5000.

Step 2: Create requirements.txt

Create a requirements.txt file in the root directory of your project to list the project dependencies.

flask 

You can generate this file by running:

pip freeze > requirements.txt 

Step 3: Create a Dockerfile

Create a file named Dockerfile in the root directory of your project. This file will contain instructions for building the Docker image.

# Use the official Python image from the Docker Hub FROM python:3.11-slim # Set environment variables ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 # Set the working directory WORKDIR /app # Install dependencies COPY requirements.txt /app/ RUN pip install -r requirements.txt # Copy the project files to the working directory COPY . /app/ # Expose port 5000 EXPOSE 5000 # Run the application CMD ["python", "app.py"] 

Explanation:

  • FROM python:3.11-slim: Use the official Python image as the base image.
  • ENV PYTHONDONTWRITEBYTECODE 1 and ENV PYTHONUNBUFFERED 1: Set environment variables for Python.
  • WORKDIR /app: Set the working directory inside the container to /app.
  • COPY requirements.txt /app/ and RUN pip install -r requirements.txt: Copy requirements.txt to the container and install the dependencies.
  • COPY . /app/: Copy the rest of the project files to the container.
  • EXPOSE 5000: Expose port 5000 to the host.
  • CMD ["python", "app.py"]: Run the application.

Step 4: Build the Docker Image

Run the following command to build the Docker image:

docker build -t my-python-app . 

Explanation:

  • docker build: The Docker command to build an image.
  • -t my-python-app: Tags the image with the name my-python-app.
  • .: Specifies the current directory as the build context.

Step 5: Run the Docker Container

Run the following command to start a Docker container from the image:

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

Explanation:

  • docker run: The Docker command to run a container.
  • -p 5000:5000: Maps port 5000 of the container to port 5000 on the host machine.
  • my-python-app: The name of the Docker image to run.

Step 6: Verify the Application

Open a web browser and navigate to http://localhost:5000. You should see the message "Hello, Docker!" from your Flask application.

Additional Docker Commands

6.1 List Docker Images

To list all Docker images on your system, run:

docker images 

6.2 List Running Containers

To list all running Docker containers, run:

docker ps 

6.3 Stop a Running Container

To stop a running Docker container, run:

docker stop <container_id> 

Replace <container_id> with the actual container ID obtained from the docker ps command.

6.4 Remove a Docker Container

To remove a Docker container, run:

docker rm <container_id> 

Replace <container_id> with the actual container ID.

6.5 Remove a Docker Image

To remove a Docker image, run:

docker rmi my-python-app 

Conclusion

In this guide, you have learned how to dockerize a Python application. We covered:

  • Creating a simple Python application with Flask.
  • Writing a Dockerfile to build and run the Python application.
  • Building the Docker image.
  • Running the Docker container.
  • Verifying the application.

By following these steps, you can easily package your Python application into a Docker container, providing a consistent and portable deployment environment.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare