How to run Jenkins on Docker container | How to create Jenkins Volumes on Docker | Beginners |
#docker #jenkins #automation #devops #ci #cd
Running Jenkins Server in Docker Container with Systemd
Jenkins is an open-source automation server used to automate repetitive tasks that are often encountered in continuous integration and delivery of software. In this guide, we will see how you can run Jenkins server on a Docker container managed via Systemd init system.
This guide is specific for Docker guys who like containerizing applications and any infrastructure management tools.
Running Jenkins Server in Docker – Dependencies
Running Jenkins Server on a Docker has few dependencies that you’ll need to satisfy.
• Linux or macOS
• Docker Engine installed and running
• A user account with sudo privileges
Step 1: Install Docker Engine
Start by installing Docker engine on your base operating system.
To install latest and stable version of docker, configure its official package repository using the following command,
$ sudo dnf config-manager --add-repo=https://download.docker.com/linux/cen...
Now run following dnf command to install docker,
$ sudo dnf install -y docker-ce
docker version
Step 2: Add jenkins user
Next is to add Jenkins system user to your server. This user will manage Jenkins service.
sudo groupadd --system jenkins
sudo useradd -s /sbin/nologin --system -g jenkins jenkins
sudo usermod -aG docker jenkins
Ensure this user is added to the docker group.
id jenkins
uid=999(jenkins) gid=998(jenkins) groups=998(jenkins),999(docker)
Pull the LTS docker image release of Jenkins
sudo docker pull jenkins/jenkins:lts
Confirm that the image download was successful.
$ docker images
Step 3: Create a Jenkins Data directory & container
We need a persistent storage for Jenkins data to ensure that the data is made to remain intact and can, therefore, be re-used in the event that the container shuts down or crashes.
sudo mkdir /var/jenkins
sudo chown -R 1000:1000 /var/jenkins
Step 4: Create a Jenkins Container Systemd service
Create a new systemd service unit file for Jenkins.
sudo vim /etc/systemd/system/jenkins-docker.service
Add:
[Unit]
Description=Jenkins Server
Documentation=https://jenkins.io/doc/
After=docker.service
Requires=docker.service
[Service]
Type=simple
User=jenkins
Group=jenkins
TimeoutStartSec=0
Restart=on-failure
RestartSec=30s
ExecStartPre=-/usr/bin/docker kill jenkins-server
ExecStartPre=-/usr/bin/docker rm jenkins-server
ExecStartPre=/usr/bin/docker pull jenkins/jenkins:lts
ExecStart=/usr/bin/docker run \
--name jenkins-server \
--publish 8080:8080 \
--publish 50000:50000 \
--volume /var/jenkins:/var/jenkins_home \
jenkins/jenkins:lts
SyslogIdentifier=jenkins
ExecStop=/usr/bin/docker stop jenkins-server
[Install]
WantedBy=multi-user.target
Reload systemd and start jenkins service
sudo systemctl daemon-reload
sudo systemctl start jenkins-docker
On checking the status, you should get a running message.
$ systemctl status jenkins-docker
Step 5: Unlock Jenkins Server installation
Browse to the URL to access web installation wizard:
http://[serverip|hostname]:8080
When you first access a new Jenkins instance, you are asked to unlock it using an automatically generated password.
Get the password from
cat /var/jenkins/secrets/initialAdminPassword
In the next page, install recommended plugins or plugins that suit your desired Jenkins usage. if not sure, select installation of recommended plugins.