This is a machine-translated text that may contain errors!
Docker simplifies the deployment of applications. Instead of manually installing and configuring software on a server, you define everything in configuration files. The result is reproducible, portable, and quick to set up.
What is Docker Compose?
With Docker Compose, you define multiple services in a single file (docker-compose.yml). Each service is a container with its own configuration.
services:
web:
image: nginx:latest
ports:
- "80:80"
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
One command sets everything up:
docker compose up -d
# Starter alle tjenester i bakgrunnen.
# Starts all services in the background.
Need to move the service to another server? Copy the file and run the same command. Everything is identical.
Why is this automation?
Consider the difference:
| Manual setup | With Docker Compose |
|---|---|
| Install Nginx manually | image: nginx:latest |
| Configure ports | ports: "80:80" |
| Install Grafana manually | image: grafana/grafana:latest |
| Document all steps | Everything is documented in the .yml file |
| Repeat everything on the next server | docker compose up -d |
The Docker Compose file is the documentation. It accurately describes which services are running, which ports they use, and how they are configured.
Useful Docker Commands
| Command | What it does |
|---|---|
docker compose up -d | Starts all services in the background |
docker compose down | Stops and removes all containers |
docker compose logs -f | Follows the logs in real time |
docker compose pull | Fetches the latest version of all images |
docker compose restart | Restarts all services |
docker ps | Shows running containers |
Volumes: Store Data Outside the Container
Containers are temporary. If you delete a container, all its content disappears. To persist data, we use volumes:
services:
database:
image: postgres:16
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: hemmelig # This is the database password
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
Here, the database files are stored in a volume called db-data. Even if you delete and recreate the container, the data will still be there.
Updating Services
Updating a Docker service is simple:
# Fetch latest version
docker compose pull
# Restart with new version
docker compose up -d
Compare this to manually updating installed software, where you might have to download, configure, and hope nothing breaks.
Task 1 - Set up Nginx with Docker Compose
Create a docker-compose.yml that runs an Nginx web server:
- Create a new folder and create the file
docker-compose.yml - Define a service with
image: nginx:lateston port 80 - Run
docker compose up -d - Visit
http://localhostin the browser
Task 2 - Add a Service
Extend the docker-compose.yml from task 1 with an additional service. For example:
- Uptime Kuma on port 3001
- Grafana on port 3000
Start everything with docker compose up -d and verify that both services are running simultaneously.
Summary
- Docker Compose allows you to define multiple services in one file
- The Compose file is both configuration and documentation
- Volumes store data outside the container so that it survives restarts
- Updating is
docker compose pull+docker compose up -d - Docker makes it easy to move services between servers