Skip to main content

Command Palette

Search for a command to run...

How to Write a Dockerfile for a Python Application (Step-by-Step Guide for Beginners)

Updated
3 min read
How to Write a Dockerfile for a Python Application (Step-by-Step Guide for Beginners)
M

👋 Hi there! I'm a DevOps enthusiast with a deep passion for all things Cloud Native. I thrive on learning and exploring new technologies, always eager to expand my knowledge and skills. Let's connect, collaborate, and grow together as we navigate the ever-evolving tech landscape! SKILLS: 🔹 Languages & Runtimes: Python, Shell Scripting, YAML 🔹 Cloud Technologies: AWS, Microsoft Azure, GCP 🔹 Infrastructure Tools: Docker, Terraform, AWS CloudFormation 🔹 Other Tools: Linux, Git and GitHub, Jenkins, Docker, Kubernetes, Ansible, Prometheus, Grafana

Want to containerize your Python application using Docker?

In this blog, you’ll learn how to write a simple Dockerfile for a Python app and understand each instruction step-by-step.

Dockerfile for a Python application

Let’s create a simple Dockerfile for a Python application. This example assumes you have a Python script named app.py and a requirements.txt file containing the dependencies for your application.

  1. Open a terminal.

  2. Navigate to the directory where you want to create or edit the Dockerfile.

  3. Type vi Dockerfile and press Enter. This will open the vi editor with a new file named Dockerfile.

  4. Press i to enter insert mode. You can now start typing your Dockerfile contents.

  5. Once you’re done editing, press Esc to exit insert mode.

  6. Type :wq and press Enter to save the changes and exit vi. If you want to exit without saving, type :q! and press Ente

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 8080 available to the world outside this container
EXPOSE 8080

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

In this Dockerfile:

  • We’re using the official Python Docker image with version 3.9 (specifically, the slim variant, which is smaller).

  • We set the working directory inside the container to /app.

  • We copy the current directory (where your app.py and requirements.txt files should reside) into the container at /app.

  • We install the Python dependencies specified in requirements.txt.

  • We expose port 8080 to allow communication with the container.

  • We set an environment variable NAME to "World" (you can change this as needed).

  • Finally, we specify that the command to run when the container starts is python app.py.

To build an image using this Dockerfile, navigate to the directory containing the Dockerfile and run:

docker build -t my-python-app .

Replace my-python-app with the desired name for your Docker image.

After building the image, you can run a container from it using:

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

This command runs a container based on your Docker image, forwarding port 8080 from the container to port 8080 on your host machine. Adjust the port mapping as needed based on your application’s requirements.


💡 Want to learn more about Docker?

👉 Read: Creating a Dockerfile

🔗 Read now: meghasharma.hashnode.dev/dockerfile-for-a-nodejs-application
📌 Follow me for more DevOps and Docker tutorials
☕ Support me:

More from this blog

Megha Sharma's Blog

87 posts