Subscribe

TechnologyArticle

Running Jenkins in Docker: A Practical Server Setup

A CI server you can recreate in one command. Run Jenkins in a container, keep its state in a volume, and give it controlled access to Docker for building images.

Published By Aviox Blogs Engineering · 2 min read
On this page
  1. Install Docker
  2. Give Jenkins a Docker client
  3. Run the container

Running Jenkins in a container keeps the server itself disposable: its jobs, plugins and credentials live in a Docker volume, so the container can be upgraded or replaced without losing anything. This guide sets that up on an Ubuntu server and prepares Jenkins to build and deploy Docker-based applications.

Before you start

You’ll need an Ubuntu server (20.04 or 22.04 recommended), root or sudo access, and an internet connection.

Install Docker

sudo apt update
sudo apt install -y docker.io
sudo systemctl enable --now docker
docker --version

Give Jenkins a Docker client

The official jenkins/jenkins:lts image does not include the Docker CLI. To let pipelines run docker build, extend the image with the client — the daemon itself stays on the host:

FROM jenkins/jenkins:lts
USER root
RUN apt-get update \
 && apt-get install -y --no-install-recommends ca-certificates curl \
 && install -m 0755 -d /etc/apt/keyrings \
 && curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc \
 && echo "deb [signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
      > /etc/apt/sources.list.d/docker.list \
 && apt-get update && apt-get install -y docker-ce-cli \
 && rm -rf /var/lib/apt/lists/*
USER jenkins
Dockerfile
docker build -t aviox/jenkins-docker .

Run the container

docker run -d \
  --name jenkins \
  --restart unless-stopped \
  -p 8080:8080 \
  -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  --group-add $(stat -c '%g' /var/run/docker.sock) \
  aviox/jenkins-docker
What each option does.
OptionPurpose
-p 8080:8080The Jenkins web interface
-p 50000:50000Communication with Jenkins agents
-v jenkins_home:…Persistent storage for jobs, plugins and credentials
-v /var/run/docker.sock:…Lets Jenkins drive the host’s Docker daemon
--group-add …Grants the jenkins user permission to use that socket

The Docker socket is root access

Anything that can talk to docker.sock can control the host. Restrict who can create and edit jobs, keep plugins updated, and consider dedicated build agents for untrusted code.

Keep reading — it’s free

The remaining 1 min of this story is for Dispatch subscribers. Subscribing takes one field, and the whole story opens right here.

Originally published at Aviox Blogs engineering handbook.

Written by Aviox Blogs Engineering

Field notes, deployment guides and research from the engineers who build web, cloud and mobile products at Aviox Blogs.