Docker advanced
Your image works. Is it small, safe, and ready to run for months?
Chapters
- 0:00Introduction
- 0:06With Server AI Hub: Look inside
- 0:18What you gain: The benefits
- 0:27The alternative: By hand
- 0:36Smaller images: Multi-stage builds
- 0:46Lock it down: Run it safely
- 0:59Keys: Build secrets
- 1:08Repeatable: Pin your versions
- 1:20Two processors: Arm and x86
- 1:30Stay up: Health and restarts
- 1:42Share fairly: Limits and GPUs
- 1:54Housekeeping: Mind the disk
- 2:05Protect data: Back up a volume
- 2:20Quiet growth: Cap your logs
- 2:28Recap: You've got it
- 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 myappHousekeeping: 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 pruneProtect 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 startQuiet 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 myappRecap: 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
- docs.docker.com/build/building/multi-stage
- docs.docker.com/build/building/best-practices
- docs.docker.com/build/building/multi-platform
- docs.docker.com/build/building/secrets
- docs.docker.com/engine/containers/start-containers-automatically
- docs.docker.com/engine/containers/resource_constraints
- docs.docker.com/reference/dockerfile/#healthcheck
- docs.docker.com/engine/security
- docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/docker-specialized.html
- docs.docker.com/engine/storage/volumes/#back-up-restore-or-migrate-data-volumes
- docs.docker.com/engine/logging/drivers/json-file
More lessons
- CUDA advanced 2:08
- Docker basics 3:24
- Docker intermediate 2:26
- Docker networking 2:28
- GitHub basics 1:51
- GitHub intermediate 1:57