Running Jenkins in a Docker Container with Persistent Storage
This guide will help you set up Jenkins in a Docker container, with a persistent volume to ensure Jenkins configuration, jobs, and plugins are preserved even after container restarts.
Prerequisites
Docker installed on your host machine
Basic knowledge of Docker commands
Steps
Step 1: Set Up a Directory for Persistent Storage
Create a directory on your host machine to store Jenkins data:
sudo mkdir -p /var/lib/jenkins
This directory will serve as a persistent volume for Jenkins data.
Set ownership of the directory to user ID 1000, which is the default Jenkins user inside the Docker container:
sudo chown -R 1000:1000 /var/lib/jenkins
Step 2: Run the Jenkins Docker Container
Pull the Jenkins image if it’s not already available on your machine:
docker pull jenkins/jenkins:lts
Run the container with the volume mounted:
docker run -d \
--name jenkins \
-p 8080:8080 \
-p 50000:50000 \
-v /var/lib/jenkins:/var/jenkins_home \
jenkins/jenkins:lts
-p 8080:8080
: Maps Jenkins’s web interface to port 8080 on your host.-p 50000:50000
: Enables connections from Jenkins agents.-v /var/lib/jenkins:/var/jenkins_home
: Mounts the host directory/var/lib/jenkins
to/var/jenkins_home
in the container. This is where Jenkins stores its data by default.
Step 3: Access Jenkins
Open a web browser and navigate to http://localhost:8080
(replace localhost
with your server’s IP address if accessing remotely).
Unlock Jenkins:
During the initial setup, Jenkins will prompt for an administrator password.
Retrieve this password from the file located at /var/lib/jenkins/secrets/initialAdminPassword
on the host machine.
To view the password, run:
cat /var/lib/jenkins/secrets/initialAdminPassword
Complete the Jenkins setup by following the on-screen instructions:
Install the recommended plugins or select plugins as per your requirements.
Create your first administrator user.
Step 4: Verify Data Persistence
To ensure that Jenkins data persists across container restarts:
Stop the Jenkins container:
docker stop jenkins
Start the container again:
docker start jenkins
Confirm that your settings, jobs, and plugins are retained by visiting
http://localhost:8080
and logging in. All your previous configurations should remain intact.