Docker has revolutionized containerization, providing powerful tools for building and managing containerized applications. Two core concepts in Docker are Dockerfile and Docker Compose. While both are essential, they serve different purposes. A Dockerfile is used to define a single container image, whereas Docker Compose simplifies managing multi-container applications.
In this tutorial, you will learn:
- The fundamental differences between Dockerfile and Docker Compose
- When to use Dockerfile and when to use Docker Compose
- Hands-on examples of both approaches

| Category | Requirements, Conventions or Software Version Used |
|---|---|
| System | Linux/macOS/Windows with Docker installed |
| Software | Docker, Docker Compose |
| Other | Basic knowledge of Docker |
| Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Dockerfile vs. Docker Compose
A Dockerfile is a script containing a set of instructions to build a Docker image. It specifies the base image, dependencies, configurations, and commands required to set up the environment.
Docker Compose, on the other hand, is a tool that defines and runs multi-container Docker applications using a docker-compose.yml file. It helps orchestrate multiple containers with networking, volumes, and environment variables.
Note: The following examples are not fully functional applications. They serve as a basic skeleton to aid in understanding Dockerfile and Docker Compose concepts.
Example 1: Creating a Simple Web Application Using Dockerfile
In this example, we create a Python-based Flask web application using a Dockerfile.
- Dockerfile for Flask Application: Below is a simple Dockerfile for a Python Flask app.
# Use an official Python runtime as base image FROM python:3.9 # Set the working directory WORKDIR /app # Copy application files COPY . . # Install dependencies RUN pip install -r requirements.txt # Define the command to run the app CMD ["python", "app.py"]
This Dockerfile pulls a Python image, sets up the environment, copies application files, installs dependencies, and runs the application.
Example 2: Using Docker Compose to Manage Multiple Containers
Now, let’s see how Docker Compose can be used to manage a Flask application with a PostgreSQL database.
- docker-compose.yml for Flask and PostgreSQL: Below is a Docker Compose configuration for a Flask app and a PostgreSQL database.
version: '3.8' services: web: build: . ports: - "5000:5000" depends_on: - db db: image: postgres:13 environment: POSTGRES_USER: user POSTGRES_PASSWORD: password POSTGRES_DB: mydatabaseThis setup defines two services:
- web: This is the Flask application service. The
build: .directive tells Docker Compose to build the image using theDockerfilepresent in the same directory. This means it follows all the steps defined in the Dockerfile, including setting up the environment, installing dependencies, and defining how the application should run. - db: This is the PostgreSQL database service. Instead of building a custom image, it pulls an official PostgreSQL image from Docker Hub. The environment variables configure the database user, password, and database name.
The
depends_ondirective ensures that the web service starts only after the database container is up. - web: This is the Flask application service. The
Conclusion
Understanding the difference between Dockerfile and Docker Compose is crucial for efficient containerized application deployment. Use Dockerfile when you need to define and build an image, and use Docker Compose to orchestrate multiple containers with networking and dependencies.