Avast ye! This be a machine-translated text, an’ it may contain errors, aye!
Automation with Dockers
With Compose ye describe yer desired setup in a file an’ let Docker create de containers. Here be startin’ wit’ an Nginx web-server from a ready-made image. Then y’all may add a service an’ inspect what gets created automatically.
Before we set sail: Docker Engine an’ Compose from [[Docker Level 1 - Get Started|Docker Level 1]] must work. Run docker compose version in the Linux VM. Ye need not have a Flask app, Dockerfile or GitHub account. [[Git in Terminal Level 3 - YAML|The YAML reference]] provides support for indentation and lists; ye can read it without havin’ crafted a GitHub workflow.
Do “Task 1” first an’ check the web response. Updatin’ is a lookup fer later work. “Task 2” lets ye try multiple services in the same project.
What Be Ye Callin’ Docker Compose?
With Docker Compose ye describe one or more services in a single file. In this drill each service runs one container. Here we use docker-compose.yml.
compose.yaml an’ docker-compose.yml be names Compose supports. Use either of them in yer project folder, an’ run th’ commands from that folder. Here we use docker-compose.yml.
services:
web:
image: nginx:latest
ports:
- "127.0.0.1:8083:80"
One command sets it all up:
docker compose up -d
The Compose file can be used on another vessel, but ye must also shift yer necessary cargo an’ local files, set yer passwords, networkin’, an’ other arrangements. Tags like latest may point to a new ship later on. If yon builds are to be identical, ye must also pin down which image is in use.
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 | Describe desired setup in Compose file |
| Repeat everything on next ship | docker compose up -d |
The Compose file be part o’ technical documentation an’ describe the desired rig state. Check actual condition with docker compose ps an’ logs. Reasons, test results, an’ backup routines must be documented as well.
Read th’ little example
services gathers th’ services. web be th’ name we give t’ web server. image picks a ready-made image, whilst ports links port 8083 on th’ VM’s loopback address to port 80 inside th’ container. So ye test from th’ VM itself. Buildin’ from source code comes in Docker Level 2.
Useful Docker Orders
| Order | What it does |
|---|---|
docker compose up -d | Starts all services in the background |
docker compose down | Stops an’ removes the project’s containers an’ networks |
docker compose logs -f | Follows the logs in real-time |
docker compose pull | Fetches the images that the project’s tags point to |
docker compose restart | Restarts the project’s containers, without changin’ the image |
docker ps | Shows runnin’ containers |
When ye need to stow yer loot
Keepin’ yer plunder outside the vessel explains how to lash up bind mounts an’ named volumes. The Nginx drill below needs no treasure chest nor storage ties. Muster yer storage support afore ye expand thy voyage with a service that must keep its booty safe.
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
Ahoy! up -d be usin’ the fresh new image by rebuildin’ that there container. Ye best check if the service be workin’ proper after the update; a brand-new image version may demand changes in yer settings or treasure (data).
Task 1 - Set up Nginx with Docker Compose
Work on the vessel’s console. Make a new folder so this drill don’t alter another Compose project:
# Create directory for piratey composition
mkdir compose-pirate
cd compose-pirate
nano docker-compose.yml
If ye folder be there already, open it an’ check its contents afore ye proceed. Write down the Nginx example from “What is Docker Compose?” inna file. Save with Ctrl+O, confirm with Enter, an’ quit with Ctrl+X. Check an’ start:
docker compose config --quiet
docker compose up -d
docker compose ps
curl http://127.0.0.1:8083
config --quiet shall quit without a curse o’ errors. If ye mend a blunder in the file, run the check once more afore launchin’. ps should show web sailin’, and curl oughta reveal the Nginx booty with Welcome to nginx!.
If the port be taken by another crew, pick a different open port on yer ship’s manifest an’ use that same number fer curl. Should the vessel sink, read the logbook via docker compose logs web. Do not set sail upon a new quest in a random cove just t’ try again.
Speak before ye press on: Which scroll describes the service, an’ what did docker compose up -d build? Halt the fleet with docker compose stop, raise anchor ag’in with docker compose up -d, an’ repeat the curl trial. If ye are usin’ this page as backup plunder for another task, ye may return there now.
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
Use a unique service name under services, at the same indentation level as web. Read the service’s official Docker examples for the necessary image, port, and storage. Use an available host port and restrict access to the practice environment. A latest tag is not a locked version.
First run docker compose config --quiet. Then start it up with docker compose up -d and see that both services are running simultaneously. If ye use bind mounts like fer Nginx, test the port from the VM; access from another machine requires yer own clarification of address, port, and network.
Show docker compose ps and an expected response from each service on the address and port ye actually have set up. Explain orally or in writing what Compose creates automatically, and what ye still must sort out yerself, such as data, settings, and backups. If one service stops, read docker compose logs before ye change the configuration. End any log tailing with Ctrl+C.
Summary
- Docker Compose lets ye define multiple services in one file
- The Compose file is both configuration an’ documentation
- Updatin’s are
docker compose pull+docker compose up -d - Docker makes it easy to move services between servers
Sources: Docker Compose: first project an’ Compose services.