Two Different Uses of Docker
| Use | Meaning | Example |
|---|---|---|
| Docker as the build environment | Each stage runs inside a container made from an image that has the right tools | Run mvn inside maven:3.9-eclipse-temurin-21 |
| Building Docker images | The pipeline's job is to create and push an image of your application | docker build and docker push |
Docker Agent: Run a Stage in a Container
Needs the Docker Pipeline (docker-workflow) plugin and Docker installed on the agent machine, with the Jenkins user allowed to use it.
- Jenkins starts the container, mounts the workspace into it, runs your steps inside, then removes the container
- The user in the container may differ from the Jenkins user. If files end up owned by root, add
args '-u 1000:1000'or set the right user - Benefit: every developer and agent gets exactly the same tools just by using the same image. No tool installs on agents
reuseNode trueruns the container on the same node and workspace as the surrounding stage when you set a top-level agent
Build and Push an Image
| Tag | Use |
|---|---|
BUILD_NUMBER-GIT_SHA | Traceable and unique. Deploy this |
1.4.2 | A release version from a tag |
latest | Convenience only. Do not deploy production from latest |
How Does the Build Reach Docker?
A build inside a container cannot simply run docker build: it needs a Docker engine. There are several options with different security trade-offs.
| Option | How | Risk and notes |
|---|---|---|
| Docker on the agent host | The agent machine has Docker; the Jenkins user is in the docker group | Simple. The docker group is root-equivalent on that host. Use dedicated agents |
| Mount the Docker socket | -v /var/run/docker.sock:/var/run/docker.sock into a build container | The build can control the host Docker. Do not use for untrusted code |
| Docker-in-Docker (dind) | A privileged docker:dind sidecar container | Needs --privileged. Isolated daemon, but privileged and layer cache is lost each time unless a volume is used |
| Rootless Docker / Podman | A user-level engine | Safer, some features limited |
| Kaniko / BuildKit / Buildah | Build images from a Dockerfile without a Docker daemon | Good for Kubernetes agents; no privileged mode needed (Kaniko) or reduced privileges (rootless BuildKit) |
Build With Kaniko Inside a Kubernetes Pod (Preview)
You will use this pattern again in the Kubernetes agents lesson.
Docker Layer Cache and Speed
- Use persistent agents (or a volume) so Docker's layer cache survives between builds
- In Dockerfiles, copy dependency files first, then source, so the cache works
- With BuildKit use
--cache-fromand--cache-to type=registryto share cache across agents - Pre-pull base images on agents regularly
- Run
docker system prune -f --filter until=72hon agents on a schedule to control disk
Clean Up After Builds
Common Errors
| Error | Cause and fix |
|---|---|
docker: command not found | Docker is not installed on that agent, or you are inside a container without the CLI |
permission denied ... docker.sock | Jenkins user not in the docker group; restart the agent after changing groups |
Cannot connect to the Docker daemon | Docker service stopped, or DOCKER_HOST points elsewhere |
| Files in the workspace owned by root | Container ran as root. Use -u with the Jenkins user ID, or chown at the end |
| Image pulled every time and builds are slow | No cache. Keep agents persistent, use cache options |
unauthorized: authentication required on push | Wrong credential ID or registry URL in withRegistry |
Practice Tasks
- Run a Maven or Node build inside a Docker agent and cache dependencies in a volume
- Write a pipeline that builds an image tagged with the build number and short SHA, scans it and pushes it from
mainonly - Use a
dockerfileagent with your own CI image - Compare the risks of the docker group, socket mount and Kaniko in a small table
- Add automatic image cleanup to the post section
Conclusion
Use Docker agents for repeatable build environments and the Docker Pipeline plugin to build and push images tagged with the build number and commit. Choose how builds reach Docker carefully: the docker group and socket mount are root-equivalent, so prefer isolated agents or daemonless builders such as Kaniko and rootless BuildKit.

