Dockerfile Demystified: Turn Your App into a Container

In this guide, weโ€™ll strip away the complexity and walk you through the process of containerizing your Python application using a Dockerfile. Whether you're building a small script or a full-fledged web app, understanding how to package your code into a container is a must-have skill in todayโ€™s DevOps and cloud-native world.

If you're new to Docker or unsure how it fits into the bigger picture, I recommend checking out my previous post ๐Ÿ‘‰ Mastering Docker: A Beginner-Friendly Guide to Containers & Images to get comfortable with the basics.

Now letโ€™s dive in! ๐Ÿณ๐Ÿ”ฅ

๐Ÿ› ๏ธ Understanding docker build

The docker build command is how you turn your code and instructions into a Docker image. Think of it like baking a cake: you provide the recipe (your Dockerfile) and ingredients (your code), and Docker creates a finished image.

Syntax:

docker build -t my-python-app .
  • -t lets you tag your image with a name.

  • . tells Docker to look for a Dockerfile in the current directory.

Once built, this image contains everything your app needs to run, including Python, libraries, and your source code.

๐Ÿงพ What Is a Dockerfile?

A Dockerfile is simply a text file with a list of instructions that Docker uses to build your image. Here's a basic one for a Python app:

Example Dockerfile:

# Use official Python image
FROM python:3.9

# Set working directory
WORKDIR /app

# Copy the Python script
COPY app.py .

# Install dependencies (if any)
# RUN pip install -r requirements.txt

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

This file says:

  • Start with the Python 3.9 image.

  • Set /app as the working directory.

  • Copy app.py to the container.

  • (Optional) Install dependencies if you have a requirements.txt file.

  • When the container runs, start the app using python app.py.


๐Ÿณ Build and Run the Docker Image

Once your Dockerfile is ready, build your image:

docker build -t python-app .

Now run it:

docker run --rm python-app

This will start a container, run your app, then remove the container when it's done.


๐Ÿ“ฆ What is Docker Compose?

As your app grows, it may need multiple services (e.g., a Python backend and a database). Docker Compose lets you define and run multi-container applications using a simple docker-compose.yml file.

Example:

version: '3.8'
services:
  app:
    build: .
    ports:
      - "5000:5000"

To start everything:

docker-compose up

This tells Docker to:

  • Build your app using the Dockerfile

  • Map port 5000 on your machine to port 5000 inside the container

๐Ÿ™ Push to Docker Hub

Want to share your image?

Step 1: Tag the image

docker tag flask-app yourdockerhubusername/flask-app:latest

Step 2: Login to Docker Hub

docker login

Step 3: Push the image

docker push yourdockerhubusername/flask-app:latest

Now anyone can pull and run it with:

docker pull yourdockerhubusername/flask-app

โœ… What We've Learned So Far

  • What docker build does and how it works with a Dockerfile

  • How to write a Dockerfile for a Python app

  • How to build, run, and tag your own Docker image

  • Introduction to Docker Compose for multi-service apps

  • How to push your image to Docker Hub

๐ŸŽ Bonus: Free Sample Project to Practice

To put your new Docker skills to the test, Iโ€™ve created a beginner-friendly sample project thatโ€™s ready for you to explore, build, and run โ€” just like we discussed in this guide.

๐Ÿงฐ GitHub Repository:
๐Ÿ‘‰ docker-python-flask-starter

Follow for more and subscribe to our newsletter!

0
Subscribe to my newsletter

Read articles from Prianshu Mukherjee directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prianshu Mukherjee
Prianshu Mukherjee