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, Ye Scallywags!
| Command | What it does, Arrr! |
|---|---|
docker compose up -d | Starts all the services in the shadows |
docker compose down | Stops and scuttles all the containers |
docker compose logs -f | Keeps a weather eye on the logs in real-time |
docker compose pull | Fetches the latest booty for all images |
docker compose restart | Reboots all the services, matey! |
docker ps | Shows which containers be sailin’ |
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: secret # secret, matey
volumes:
- db-data:/var/lib/postgresql/data
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!
Quest 1 - Set up Nginx with Docker Compose
Forge a docker-compose.yml that runs an Nginx webserver:
- Carve out a new folder and create the
docker-compose.ymlfile - Define a service with
image: nginx:lateston port 80 - Run
docker compose up -d - Set sail to
http://localhostin yer browser
Task 2 - Add a new service to the crew
Expand docker-compose.yml from task 1 with an extra service. For example:
- Uptime Kuma on port 3001
- Grafana on port 3000
Set sail with docker compose up -d and see if 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