In this article, we'll walk you through setting up Jenkins in a Docker container, ensuring data persistence on the host machine. We'll also cover how to configure the container to avoid common permission issues and optimize for shared access among users.
Why Use Docker for Jenkins?
Docker provides an efficient, isolated environment to run Jenkins, making it easy to manage and deploy. By configuring persistent storage, you ensure that Jenkins data, such as job configurations and build artifacts, is retained even when the container is restarted or replaced.
Steps to Set Up Jenkins with Docker
Prerequisites
- Docker: Ensure Docker is installed on your system.
- Host Directory for Data Persistence: Decide where you want Jenkins to store its data. For this setup, we’ll use /vm/data/jenkins-20170823.
Create the Host Directory
Create the directory on the host machine to store Jenkins data persistently:
mkdir -p /vm/data/jenkins-20170823
Set Up Group Access
To allow shared access to the Jenkins directory, create a group and assign users to it:
Create a Group
We are using jenkins_user as the group name because in the container also has the user jenkins_user part of the group jenkins_user. This is really not important, but for the consistency we used it.
sudo groupadd jenkins_user
Add Users ubuntu and vmuser to the Group
sudo usermod -aG jenkins_user ubuntu
sudo usermod -aG jenkins_user vmuser
Find UID and GID of the Host User
Docker containers run processes as specific users and groups, identified by their UID (User ID) and GID (Group ID). To ensure the Jenkins container can read and write to the host directory /vm/data/jenkins-20170823, the container's user and group IDs must match the directory's ownership on the host machine. By aligning the UID and GID of the container with the host system, we prevent permission issues when Jenkins interacts with the mounted volume.
How to Determine the UID and GID
The UID (User ID) is the numeric identifier for the user running the Docker container. To find it, use:
id -u
For example, for the vmuser user, this command will returned 111, which is the UID used in the command below.
The GID (Group ID) is the numeric identifier for the group. To find the GID of jenkins_user, use:
getent group jenkins_user
This will return an output like:
jenkins_user:x:1001:ubuntu,vmuser
Here, 1001 is the GID for the jenkins_user.
Update Ownership and Permissions
After determining the UID and GID, update the ownership and permissions of the directory:
sudo chown -R 111:jenkins_user /vm/data/jenkins-20170823
chmod -R 770 /vm/data/jenkins-20170823
Pull the Jenkins Docker Image
Download the latest Jenkins LTS image:
docker pull jenkins/jenkins:lts
Start Jenkins Container
Run the Jenkins container with the following configuration:
docker run -d \
--name jenkins-20170823 \
-p 8888:8080 \
-p 50000:50000 \
-u 111:1001 \
-v /vm/data/jenkins-20170823:/var/jenkins_home \
jenkins/jenkins:lts
Explanation
- --name jenkins-20170823: Assigns a custom name to the container for easier identification.
- -p 8888:8080: Maps the Jenkins UI to port 8888 on the host.
- -p 50000:50000: Maps the Jenkins agent communication port.
- -u 111:1001: Ensures the container runs with UID 111 (corresponding to the host user vmuser) and GID 1001 (corresponding to the group jenkins_user). This configuration ensures that the container has proper permissions to read and write to the host directory /vm/data/jenkins-20170823, which is owned by UID 111 and GID 1001.
- -v /vm/data/jenkins-20170823:/var/jenkins_home: Mounts the host directory to Jenkins' home directory for persistent storage.
Access Jenkins
- Open a browser and navigate to: http://
:8888
Replace
with your server’s IP address or localhost if you’re accessing from the same machine. - Retrieve the initial admin password: docker exec jenkins-20170823 cat /var/jenkins_home/secrets/initialAdminPassword
- Complete the setup wizard and start configuring Jenkins.
Managing Jenkins
Stop the Container
To stop Jenkins:
docker stop jenkins-20170823
Restart the Container
To restart Jenkins:
docker start jenkins-20170823
View Logs
To check Jenkins logs:
docker logs -f jenkins-20170823
Backup and Restore
Backup Jenkins Data
Since Jenkins data is stored in /vm/data/jenkins-20170823 on the host, you can back it up using standard tools:
tar -czvf jenkins_backup_20170824.tar.gz /vm/data/jenkins-20170823
Restore Jenkins Data
To restore data:
tar -xzvf jenkins_backup_20170824.tar.gz -C /vm/data/jenkins-20170823
Backup Container State with docker commit
This method preserves the entire Jenkins container state, including settings, logs, and configurations.
Backup the Jenkins Container as a new image.
Create an Image Snapshot, that saves the current state of the running or stopped Jenkins container:
docker commit jenkins-20170823 jenkins-backup:20170824
This creates a new Docker image named jenkins-backup:20170824.
Save the Image to a File
Export the Jenkins image for backup:
docker save -o jenkins-backup-20170824.tar jenkins-backup:20170824
This .tar file can be transferred or stored for recovery.
Restore the Jenkins Container
Load the Backup Image If restoring on the same or another system, load the saved image:
docker load -i jenkins-backup-20170824.tar
Run a New Container from the Backup Start Jenkins using the restored image:
docker run -d --name jenkins-restored \
-p 8888:8080 -p 50000:50000 \
-v /vm/data/jenkins-20170823:/var/jenkins_home \
jenkins-backup:20170824
This ensures that Jenkins runs with its previous settings and configurations intact.
By following these backup methods, you can fully restore Jenkins, including both its data (/vm/data/jenkins-20170823) and its container state for a complete recovery
Conclusion
By following this guide, you’ve successfully set up Jenkins in a Docker container with persistent data storage. This configuration ensures that Jenkins is portable, resilient to container restarts, and easily manageable by multiple users. With group-based access, you’ve also enabled secure collaboration for system administrators.
If you have any questions or face issues, feel free to reach out in the comments section below!
Post a comment