2024-03-26 14:20:03 +00:00
|
|
|
---
|
|
|
|
id: fw56
|
|
|
|
title: Docker compose and Dockerfile difference
|
2024-06-16 18:15:03 +01:00
|
|
|
tags:
|
|
|
|
- docker
|
2024-03-26 14:20:03 +00:00
|
|
|
created: Tuesday, March 26, 2024
|
|
|
|
---
|
|
|
|
|
|
|
|
# Docker compose and Dockerfile difference
|
|
|
|
|
|
|
|
### Key Differences
|
|
|
|
|
|
|
|
- **Scope**: A `Dockerfile` is for building a single Docker image, while
|
|
|
|
`docker-compose.yml` is for orchestrating multiple containers (that might be
|
|
|
|
built from `Dockerfiles`) to work together as a unified application.
|
|
|
|
- **Use Case**: A `Dockerfile` is necessary when you need to create a custom
|
|
|
|
image. In contrast, `docker-compose.yml` is used when you want to deploy and
|
|
|
|
manage an application that consists of multiple containers.
|
|
|
|
- **Command**: You build an image from a `Dockerfile` using `docker build`. You
|
|
|
|
start the application defined by `docker-compose.yml` using
|
|
|
|
`docker-compose up`.
|
|
|
|
|
|
|
|
### Dockerfile
|
|
|
|
|
|
|
|
- **Purpose**: A `Dockerfile` is a text document that contains all the commands
|
|
|
|
a user could call on the command line to assemble an image. It automates the
|
|
|
|
process of creating Docker images by specifying a sequence of steps and
|
|
|
|
configurations such as setting environment variables, installing software, and
|
|
|
|
copying files from the local directory into the image.
|
|
|
|
- **Functionality**: It is essentially a blueprint for building Docker images.
|
|
|
|
You define the base image, software packages, scripts, files, environment
|
|
|
|
variables, and other components you need in your Docker image.
|
|
|
|
- **Usage**: You use a `Dockerfile` when you need to create a custom Docker
|
|
|
|
image based on your application's specific dependencies and runtime
|
|
|
|
environment. After defining your `Dockerfile`, you use the `docker build`
|
|
|
|
command to create an image.
|
|
|
|
|
|
|
|
Example structure of a Dockerfile:
|
|
|
|
|
|
|
|
```Dockerfile
|
|
|
|
FROM python:3.8
|
|
|
|
WORKDIR /app
|
|
|
|
COPY . /app
|
|
|
|
RUN pip install -r requirements.txt
|
|
|
|
CMD ["python", "./my_script.py"]
|
|
|
|
```
|
|
|
|
|
|
|
|
### docker-compose.yml
|
|
|
|
|
|
|
|
- **Purpose**: A `docker-compose.yml` file is used to define and run
|
|
|
|
multi-container Docker applications. With a single command, you can configure
|
|
|
|
all of your application's services, networks, and volumes in this YAML file
|
|
|
|
and bring everything up or down.
|
|
|
|
- **Functionality**: It simplifies the deployment and management of application
|
|
|
|
components that are meant to run in separate containers while still needing to
|
|
|
|
communicate or work together. For example, a web application might consist of
|
|
|
|
a web server, a database, and a caching system, each running in its container.
|
|
|
|
- **Usage**: `docker-compose` is used for defining and running complex
|
|
|
|
applications with Docker. Instead of using lengthy `docker run` commands with
|
|
|
|
many flags, you write a `docker-compose.yml` file and use `docker-compose up`
|
|
|
|
to start the application.
|
|
|
|
|
|
|
|
Example structure of a docker-compose.yml file:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
version: "3"
|
|
|
|
services:
|
|
|
|
web:
|
|
|
|
build: .
|
|
|
|
ports:
|
|
|
|
- "5000:5000"
|
|
|
|
db:
|
|
|
|
image: postgres
|
|
|
|
volumes:
|
|
|
|
- ./data:/var/lib/postgresql/data
|
|
|
|
```
|