Learn / Lessons · Docker

Docker advanced

Your image works. Is it small, safe, and ready to run for months?

Chapters

  1. 0:00Introduction
  2. 0:06With Server AI Hub: Look inside
  3. 0:18What you gain: The benefits
  4. 0:27The alternative: By hand
  5. 0:36Smaller images: Multi-stage builds
  6. 0:46Lock it down: Run it safely
  7. 0:59Keys: Build secrets
  8. 1:08Repeatable: Pin your versions
  9. 1:20Two processors: Arm and x86
  10. 1:30Stay up: Health and restarts
  11. 1:42Share fairly: Limits and GPUs
  12. 1:54Housekeeping: Mind the disk
  13. 2:05Protect data: Back up a volume
  14. 2:20Quiet growth: Cap your logs
  15. 2:28Recap: You've got it
  16. 2:33Wrap up
Read the transcript

Introduction

Your image works. Is it small, safe, and ready to run for months?

With Server AI Hub: Look inside

With Server AI Hub, open a container to see what it's using, how it's set up and what it's logging. If one gets stuck restarting, it shows up in Watchdog, and one click holds it.

What you gain: The benefits

Health shows at a glance. Before cleaning up, you see which images would go. And removing a volume asks first, because its data goes too.

The alternative: By hand

The other way is by hand. Making an image small, safe and repeatable is Terminal and Dockerfile work. Here's how.

Smaller images: Multi-stage builds

A multi stage build uses one stage to build, and a second, slim stage to run. Only what you copy across ends up in the final image.

FROM golang:1.27 AS build
WORKDIR /src
COPY . .
RUN go build -o /app .
FROM debian:trixie-slim
COPY --from=build /app /app
CMD ["/app"]

Lock it down: Run it safely

Don't run as root if you don't need to. Set a user in the Dockerfile. Avoid privileged mode. And never hand a container the Docker socket unless you trust it with the whole machine.

Keys: Build secrets

Don't bake keys into an image; they can be read back out. For builds, pass a build secret. The Dockerfile mounts it for one step only.

docker build --secret id=token,src=token.txt .

Repeatable: Pin your versions

Tags can move. Choose a specific version tag, or pin a digest to get the exact same image every time. Then update on purpose, and rebuild for security fixes.

Two processors: Arm and x86

A DGX Spark uses an Arm processor. Build for both Arm and x86 with build x, and push the result to a registry.

docker buildx build \
  --platform linux/arm64,linux/amd64 \
  -t you/myapp:1.0 --push .

Stay up: Health and restarts

A health check asks your app whether it's really working. A restart policy such as unless stopped brings a container back after a crash or a reboot. Logs tell you why it fell over.

Share fairly: Limits and GPUs

Set limits so one container can't take everything. To use an NVIDIA graphics processor, install the NVIDIA Container Toolkit, and ask for the GPU when you run.

docker run --memory 8g --cpus 4 myapp
docker run --gpus all myapp

Housekeeping: Mind the disk

Images and build cache grow. Docker system df shows the space used. Prune removes unused things, so read the warning before you say yes.

docker system df
docker image prune
docker system prune

Protect data: Back up a volume

A volume needs backing up like any other data. List your volumes first, because Compose puts the project name in front. A short lived container can pack it into a file in your current folder. Stop the app first, so the files aren't changing.

docker volume ls
docker compose stop
docker run --rm -v myproject_mydata:/data \
  -v "$PWD":/backup alpine \
  tar czf /backup/data.tgz -C /data .
docker compose start

Quiet growth: Cap your logs

Container logs can grow without limit by default. Set a maximum size, and a number of files to keep.

docker run -d \
  --log-opt max-size=10m \
  --log-opt max-file=3 myapp

Recap: You've got it

Small, safe, repeatable and watched. That's production Docker.

Wrap up

Harden your images by hand, and let Server AI Hub keep watch: stuck containers caught, clean-up you can check first, and a question before data is removed.

Sources

More lessons