Sub topic 13
Keeping disk usage down in the first place
This chapter assumes you can run docker run, docker build and docker ps, and that you have already used docker system df and the prune commands from earlier chapters. Pruning removes waste after it exists. The practices below stop much of that waste from being created.
Where the waste comes from
Each source of growth has a matching habit that prevents it.
Use --rm for short-lived containers
A container that has exited still keeps its writable layer on disk. The --rm flag deletes the container, and its anonymous volumes, when it exits.
docker run --rm alpine:3.20 echo "done"Use it for one-off commands, test runs and scripts. Leave it off for containers whose logs or filesystem you may need to inspect after a crash.
Exclude files with .dockerignore
docker build sends the whole build context (the directory you point it at) to the builder. Anything a COPY . . instruction picks up ends up in an image layer. A .dockerignore file in the context root keeps unwanted files out of both.
.git
node_modules
*.log
tmp/
.envThis shrinks the context upload and the layers. It also keeps secrets such as .env out of images.
Ship only what you run with multi-stage builds
Compilers and package caches are needed to build an application but not to run it. A multi-stage build compiles in one stage and copies only the result into a small final image.
package main
import "fmt"
func main() {
fmt.Println("hello")
}FROM golang:1.22 AS build
WORKDIR /src
COPY main.go .
RUN go mod init example.com/hello \
&& CGO_ENABLED=0 go build -o /hello .
FROM alpine:3.20
COPY --from=build /hello /hello
CMD ["/hello"]The golang image is several hundred megabytes. The final image contains only Alpine and one binary. The build stage stays in the build cache and never ships.
Rotate container logs
By default the json-file log driver has no size limit, so a chatty container can fill the disk. Set limits for every new container in /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Restart the daemon after editing the file. The limits apply only to containers created afterwards, so recreate existing containers to pick them up. To limit a single container instead, pass the options on the command line:
docker run -d --log-opt max-size=10m --log-opt max-file=3 nginx:1.27Common mistakes
- Using
--rmon a container that holds data in its writable layer. The data is deleted with the container. Put it in a named volume instead. - Forgetting to ignore
node_modulesor.git. The build context becomes huge, and aCOPY . .can overwrite dependencies installed in the image. - Putting the compiler in the final stage. A multi-stage build only helps if the last
FROMis a lean image.
Exercise
A CI job runs docker build and docker run on every commit, and the build server's disk keeps filling up. Name two changes that reduce growth without pruning, and say what each prevents.
Show answer
Add --rm to the docker run command so each test container is deleted on exit instead of piling up as a stopped container. Switch the Dockerfile to a multi-stage build so build tools and caches do not end up in the final image. A .dockerignore file or a log size limit in daemon.json are also valid answers.