Member-only story

Dockerfile for a Python application

Meghasharmaa
4 min readFeb 24, 2024

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…

--

--

Meghasharmaa
Meghasharmaa

Written by Meghasharmaa

DevOps Architect | AWS | Docker | Kubernetes | Terraform | Google Cloud | Python

Responses (1)