Post

Dockerfile vs docker compose

1. Dockerfile

A Dockerfile is a script that contains a set of instructions to create a Docker image. It defines the environment in which an application will run inside a container. The image built from the Dockerfile can then be used to create and run containers..

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1. Specify the base image
FROM python:3.9

# 2. Set the working directory
WORKDIR /app

# 3. Copy the dependencies file to the container
COPY requirements.txt .

# 4. Install dependencies
RUN pip install -r requirements.txt

# 5. Copy application source code
COPY . .

# 6. Specify the command to run the application
CMD ["python", "app.py"]
CommandDescription
FROMSpecifies the base image (e.g., ubuntu:20.04, node:16)
WORKDIRSets the working directory inside the container
COPYCopies files from the local system to the container
RUNExecutes commands during the image build process (e.g., installing dependencies)
CMDSpecifies the default command to run when the container starts
ENTRYPOINTSimilar to CMD, but allows additional command-line arguments
EXPOSEDeclares the port the container will use
ENVDefines environment variables

2. docker-compose.yml

A docker-compose.yml file is used to define and manage multiple containers as part of a single application. With docker-compose up, you can start multiple services at once.

Example docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
version: "3.8"

services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - db
    environment:
      - DATABASE_URL=mysql://user:password@db:3306/mydb

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydb
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data:

Key Concepts of docker-compose.yml

PropertyDescription
versionSpecifies the Docker Compose file version (e.g., "3.8")
servicesDefines the containers that will be run
buildUses the Dockerfile to build an image
imageSpecifies the Docker image to use
portsMaps ports between the host and container (host:container)
depends_onEnsures services start in a specific order
environmentDefines environment variables
volumesCreates persistent storage

3. Differences Between Dockerfile and docker-compose.yml

FeatureDockerfiledocker-compose.yml
PurposeDefines how to build a container imageManages multiple containerized services
Usagedocker build, docker rundocker compose up -d, docker compose down
FunctionalityInstalls dependencies, sets up environmentDefines services, networks, and dependencies

4. Example Usage

1) Create a Dockerfile

1
2
3
4
5
6
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

2) Create a docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
version: "3.8"

services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - db
    environment:
      - DATABASE_URL=mysql://user:password@db:3306/mydb

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydb
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    ports:
      - "3306:3306"

3) Running the Containers

  1. Start the containers using Docker Compose

    1
    
     docker-compose up -d
    
  2. Check running containers

    1
    
     docker-compose p
    
  3. Stop and remove containers

    1
    
     docker-compose down
    
This post is licensed under CC BY 4.0 by the author.