Running Jenkins in Docker

29 Dec 2020

Get the latest LTS (long term support) image:

Bash
1
docker pull jenkins/jenkins:lts

Create a volume to store persistent data:

Bash
1
docker volume create jenkins_vol

Create and run a container:

Bash
1
2
3
4
5
docker container run -d ^
  -p 8081:8080 ^
  -v jenkins_vol:/var/jenkins_home ^
  --name jenkins-local ^
  jenkins/jenkins:lts

Arguments:

-d:     detached mode
-v:     attach volume (volname:mountpath:optionaloptions)
-p:     assign port target (e.g. host 8081 mapped from container 8080)
--name: name of the container

Using the more modern --mount:

--mount type=volume,source=jenkins_vol,target=/var/jenkins_home

Example:

Bash
1
2
3
4
5
docker container run -d ^
  -p 8081:8080 ^
  --mount type=volume,source=jenkins_vol,target=/var/jenkins_home ^
  --name jenkins-local ^
  jenkins/jenkins:lts

Output is the full ID of the launched container:

231b93f9979451a22c45cda22438f53b5309ac1608534364f225b5d138ba8050

See the process running:

Bash
1
docker container ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS              PORTS                               NAMES
231b93f99794        jenkins/jenkins:lts   "/sbin/tini -- /usr/…"   About a minute ago   Up About a minute   50000/tcp, 0.0.0.0:8081->8080/tcp   jenkins-local

Now go to http://localhost:8081

Get the initial security password:

Bash
1
2
3
docker container exec ^
  231b93f99794 ^
  sh -c "cat /var/jenkins_home/secrets/initialAdminPassword"

Output:

32af70bf57de4...699d7f3e2d29c4e

User ID = admin, p/w = whatever

Restart an existing container:

Bash
1
docker start jenkins-local

Logs can be tailed/followed:

Bash
1
docker logs -f 231b93f99794

Full details about additional set-up and configuration:

Official Jenkins Docker image

Topics include:

  • Backing up configuration in the jenkins_home directory.
  • Setting the number of build executors (e.g. via port 50000)
  • Passing JVM parameters.
  • etc.

Log on to the container:

Bash
1
docker exec -it [container-id] /bin/bash

Or, to log on as root:

Bash
1
docker exec -u 0 -it [container-id] /bin/bash