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"]
|
Command | Description |
---|
FROM | Specifies the base image (e.g., ubuntu:20.04 , node:16 ) |
WORKDIR | Sets the working directory inside the container |
COPY | Copies files from the local system to the container |
RUN | Executes commands during the image build process (e.g., installing dependencies) |
CMD | Specifies the default command to run when the container starts |
ENTRYPOINT | Similar to CMD , but allows additional command-line arguments |
EXPOSE | Declares the port the container will use |
ENV | Defines 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
Property | Description |
---|
version | Specifies the Docker Compose file version (e.g., "3.8" ) |
services | Defines the containers that will be run |
build | Uses the Dockerfile to build an image |
image | Specifies the Docker image to use |
ports | Maps ports between the host and container (host:container ) |
depends_on | Ensures services start in a specific order |
environment | Defines environment variables |
volumes | Creates persistent storage |
3. Differences Between Dockerfile and docker-compose.yml
Feature | Dockerfile | docker-compose.yml |
---|
Purpose | Defines how to build a container image | Manages multiple containerized services |
Usage | docker build , docker run | docker compose up -d , docker compose down |
Functionality | Installs dependencies, sets up environment | Defines 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
Start the containers using Docker Compose
Check running containers
Stop and remove containers