Docker | EP02 – Connecting Your Dockerfile with Docker Compose



This content originally appeared on DEV Community and was authored by Booranasak Kanthong

In EP01, we learned how to build a Python web app with Flask and Dockerize it. Now, let’s level up by introducing Docker Compose — a tool that lets you define and run multi-container applications with ease.

Why Use Docker Compose?

Imagine you want to run:

  • Your Python app
  • Plus a Redis cache
  • Or a PostgreSQL database

Instead of typing docker run commands for each container (and remembering port mappings, volumes, etc), you can define everything in one simple file:
docker-compose.yml.

Project Structure

my-python-app/
├── app.py
├── requirements.txt
├── Dockerfile
└── docker-compose.yml

Step 1: The Same Flask App (No Changes Needed)

We’re still using the same app.py, requirements.txt, and Dockerfile from Episode01.

Your image will be built automatically by Docker Compose.

Step 2: Create docker-compose.yml

Here’s how to define your Flask app in Compose:

version: '3.8'

services:
  web:
    build: .
    image: my-python-app
    ports:
      - "5000:5000"

Explanation:

  • build: Build the image using your Dockerfile in this folder
  • image: Give the image a name (you’ll use this again)
  • ports: Maps port 5000 in the container to port 5000 on your machine

Step 3: Add a Second Service (e.g. Redis)

Let’s add a basic Redis container to help your app cache stuff (even if we don’t use it yet).

version: '3.8'

services:
  web:
    build: .
    image: my-python-app
    ports:
      - "5000:5000"
    depends_on:
      - redis

  redis:
    image: redis:alpine

depends_on tells Compose:

“Start Redis before starting the web app.”

Even if you’re not using Redis in code yet, this shows how easy it is to link containers together!

Step 4: Start Everything

In your project directory, run:

docker-compose up

Docker will:

  • Build your my-python-app image
  • Start your web app on port 5000
  • Pull and run a Redis container

or

docker-compose up -d
  • Everything above, but runs in detached mode, This means Docker will run containers in the background, and you’ll get your terminal prompt back.

Now visit: http://localhost:5000

Summary

  • Docker Compose helps you manage multiple containers.
  • You no longer need to run docker build or docker run manually.
  • Your whole environment lives in one file: docker-compose.yml.


This content originally appeared on DEV Community and was authored by Booranasak Kanthong