Avast ye! This be a machine-translated text, an’ it may contain errors, aye!
Docker be simplifies the launchin’ o’ applications. Instead o’ installin’ and configurin’ software manually on a server, ye define all in configuration files. The result be reproducible, portable, and quick to set up.
What be Docker Compose?
With Docker Compose, ye define multiple services in a single file (docker-compose.yml). Each service be a container with its own configuration, aye.
services:
web:
image: nginx:latest
ports:
- "80:80"
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
# Aye, this be the web service, matey!
# Shiver me timbers, 'tis Grafana we be havin' here!
One command sets it all up:
docker compose up -d
# Starter tjenestene i bakgrunnen, arr!
Need ye be shiftin’ the service to another server? Copy the file an’ run the same command. All be identical, aye.
Why Be 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 the steps | All be documented in the .yml file |
| Repeat all on the next server | docker compose up -d |
The Docker Compose file be the documentation. It describes exactly which services be runnin’, which ports they use, and how they be 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 | Pulls the latest version of all images |
docker compose restart | Restarts all services |
docker ps | Shows running containers |
Volumes: Stashin’ Data Outside the Hold
Containers be temporary, aye. If ye be sendin’ a container to Davy Jones’ locker, all its contents be vanishin’ with it. To keep yer treasure safe, we use volumes:
services:
database:
image: postgres:16
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: hemmelig # Ahoy, 'tis a secret password, ye scurvy dogs!
volumes:
- db-data:/var/lib/postgresql/data # Store the treasure here, savvy?
volumes:
db-data:
Here be where the database files be stored in a volume called db-data. Even if ye be deletin’ and recreatin’ the container, the data still remains, aye.
Updatin’ the Services
Updatin’ a Docker service be a simple task, aye:
# Fetch the latest version, aye!
docker compose pull
# Restart with the new version, shiver me timbers!
docker compose up -d
Compare this to updatin’ software ye installed yerself, where ye might have to download, configure, an’ pray nothin’ 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 yer browser
Task 2 - Add a Service
Expand the docker-compose.yml from task 1 with an extra service. For example:
- Uptime Kuma on port 3001
- Grafana on port 3000
Start all with docker compose up -d and see that both services be runnin’ at the same time.
Summary
- Docker Compose allows ye to define multiple services in a single file
- The Compose file be both configuration and documentation
- Volumes store data outside the container so it survives restarts
- Updating be
docker compose pull+docker compose up -d - Docker makes it easy to move services between servers