Work on your own laptop, at your own pace. Each part ends with a Done when check: when it passes, move on. Parts 5 and 6 can be finished at home.
The counter app in lab/counter/ is ready: you write only the Dockerfile and the
compose.yaml. It greets with the AUTHOR environment variable and, when REDIS_HOST is set,
counts visits in Redis.
A host port is taken? If Docker says port is already allocated, or the port is used by
another program on your laptop, change the left number of -p (or of ports: in Compose):
-p 8001:5000 instead of -p 8000:5000, then open localhost:8001. The right number is the
port inside the container and stays the same.
| Part | Topic |
|---|---|
| 0 | Setup |
| 1 | Containers |
| 2 | Images |
| 3 | Registry |
| 4 | Network by hand |
| 5 | Compose |
| 6 | Fix it (extra) |
Install Docker if you don't have it yet (steps from Docker's documentation, not tested here):
- Windows: install WSL 2 (
wsl --installin an administrator PowerShell, then reboot), then Docker Desktop from https://docs.docker.com/desktop/setup/install/windows-install/. - macOS: Docker Desktop from https://docs.docker.com/desktop/setup/install/mac-install/. Pick the Apple silicon download on Macs with an M chip (M1, M2, ...), Intel otherwise. The chip is shown in Apple menu > About This Mac.
- Linux: Docker Desktop from https://docs.docker.com/desktop/setup/install/linux/, or
Docker Engine from https://docs.docker.com/engine/install/ and then
sudo usermod -aG docker $USER(log out and in again).
Start Docker Desktop, then:
docker --version
docker compose version
docker run hello-world
Pull the images the lab needs, so you don't wait later:
docker pull nginx
docker pull python:3.12-slim
docker pull redis:8-alpine
Done when docker run hello-world prints Hello from Docker!.
- Run nginx in the background on host port 8080, named
site, and openlocalhost:8080. - Change its page from inside the container:
docker execa command that writes a new/usr/share/nginx/html/index.html. Reload the browser. - Remove the container and run it again. Is your change still there?
- Make a folder
sitewith anindex.htmlin it. Remove thesitecontainer, then run nginx again with that folder mounted on/usr/share/nginx/html(-v, with an absolute path to your folder).
Commands you need: docker run (-d, -p, --name, -v), docker exec, docker rm -f.
Bind-mount path per system: Linux and macOS "$(pwd)/site"; Windows PowerShell "${PWD}\site"
(not tested); Windows cmd "%cd%\site" (not tested). Docker Desktop only shares your home folder
by default on Linux: keep the site folder somewhere under it, or you get mounts denied.
Done when editing your local index.html changes the page on reload, with no restart.
- In
lab/counter/write aDockerfilefor the app: base imagepython:3.12-slim, installrequirements.txt, copy the code, startpython app.py. The app listens on port 5000. - Set
ENV AUTHORto your name in theDockerfile. - Build it as
counter:1.0and run it with host port 8000. - Change a string in
app.py(a new one, not one you used before) and build again.
Commands you need: docker build -t, docker run --rm -p.
Done when localhost:8000 greets with your name, and the second build shows the
pip install step as CACHED. If it isn't, look at the order of the COPY lines.
-
Create a GitHub personal access token (classic) with only the
write:packagesscope and a short expiry: https://github.com/settings/tokens/new?scopes=write:packages. Fine-grained tokens don't work with ghcr.io. Never paste the token in a file or a chat. -
Log in to
ghcr.iowith your GitHub username, passing the token on standard input:export TOKEN=YOUR_TOKEN echo $TOKEN | docker login ghcr.io -u USERNAME --password-stdinWindows PowerShell (not tested):
$env:TOKEN="YOUR_TOKEN", then$env:TOKEN | docker login ghcr.io -u USERNAME --password-stdin. -
Build for both CPU types and push it as
ghcr.io/USERNAME/counter:1.0, withdocker buildx build --platform linux/amd64,linux/arm64 ... --push.Type
USERNAMEin lowercase even if your GitHub name has capitals: Docker rejectsghcr.io/SomeUser/counter:1.0withrepository name (SomeUser/counter) must be lowercase.Why both platforms: an image built on an Apple Silicon Mac is arm64 only, and an x86 laptop can't run it (
exec format error). Docker Desktop builds both with no setup. On Docker Engine for Linux without Docker Desktop, if the build fails, run these two commands once and try again (not tested):docker run --privileged --rm tonistiigi/binfmt --install all docker buildx create --name multi --driver docker-container --useIf that doesn't work either, or Docker Desktop says the multi-platform build is not supported, push with
--platform linux/amd64only: it runs on Windows and Linux laptops, and Docker Desktop on Apple Silicon Macs runs it through emulation. -
On GitHub, open your profile, the Packages tab, then
counter. A new package is private: open Package settings, and under Danger Zone use Change visibility to make it public. -
docker logout ghcr.io.
Done when a classmate runs docker run --rm -p 8000:5000 ghcr.io/USERNAME/counter:1.0 and
sees your name.
- Create a network called
lab-net. - Run Redis (
redis:8-alpine) on it, namedcache. Don't publish its port. - Run
counter:1.0on the same network, host port 8000, withREDIS_HOST=cache. - Remove the counter and run it again with
REDIS_HOST=localhost. Read the error on the page: what doeslocalhostmean inside a container?
Commands you need: docker network create, docker run --network ... -e ....
Done when the counter increments on every refresh with REDIS_HOST=cache.
Clean up part 4 first (docker rm -f the two containers): they hold host port 8000.
Write lab/counter/compose.yaml with two services:
web: built from theDockerfile, host port 8000,REDIS_HOSTpointing at the other service. It is the only service with a published port.cache:redis:8-alpine, its data (/data) in a named volumecache-data, and ahealthcheck(redis-cli ping).webwaits untilcacheis healthy.
Commands you need: docker compose up -d --build, docker compose ps, docker compose down,
docker compose down -v.
Done when the count survives docker compose down and up again, and is back to 1 after
docker compose down -v.
The result. Once your image is public on ghcr.io, anyone can run your app without the code:
a compose.yaml whose web service says image: ghcr.io/USERNAME/counter:1.0 instead of
build: ., then:
docker compose pull
docker compose up -d
curl localhost:8000
Each folder in lab/broken/ is a small setup that fails. Its README.md says what you see. Make
it work, and say in one sentence what was wrong.
1-build-hangs2-unreachable3-localhost4-port-taken
Done when each one runs, and you can explain the cause.
- Delete your
counterpackage on GitHub (Package settings, Danger Zone, Delete this package). - Dockerize your own project, and check it against criterion 18 of the project review:
- a
Dockerfilethat builds and runs; - dependencies installed before the code is copied;
- a
.dockerignorethat excludes.git,.venvand.env; - no secrets in the image;
- a
compose.yamlwhen there is more than one service.
- a
- Add a GitHub Action to your project that builds the image on every pull request.
- Move the passwords of
compose.yamlto a.envfile (${MYSQL_PASSWORD}incompose.yaml), keep.envout of git and of the image, and commit a.env.example. - Split the services of the example over two networks, and show that a name stops resolving when two services share no network.