Jenkins on Docker Container
This blog will help you setup your Jenkins on Docker Container.
If you are learning these technologies for the first time, I would suggest you to use Cloud based Virtual Machines like (AWS EC2, or Azure VM or GCP Compute Engine)
I am using Ubuntu O.S. for this Setup
Step 1: Setup your Docker environment
0. Uninstall Old Version of Docker Container
$ sudo apt-get remove docker docker-engine docker.io containerd runc
1. Update the
apt
package index and install packages to allowapt
to use a repository over HTTPS:
$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
2. Add Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
2.1 Verify that you now have the key with the fingerprint
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
, by searching for the last 8 characters of the fingerprint.
$ sudo apt-key fingerprint 0EBFCD88
3. Use the following command to set up the stable repository. To add the nightly or test repository, add the word
nightly
ortest
(or both) after the wordstable
in the commands below. Learn about nightly and test channels.
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
Install Docker Engine
- Update the
apt
package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
Verify that Docker Engine is installed correctly by running the
hello-world
image.
$ sudo docker run hello-world
For more information on how to install docker please refer to the documentation of docker https://docs.docker.com/engine/install/ubuntu/
2. Installing Jenkins on Docker Container
There are several Docker images of Jenkins available.
The recommended Docker image to use is the jenkinsci/blueocean
image (from the Docker Hub repository). This image contains the current Long-Term Support (LTS) release of Jenkins (which is production-ready) bundled with all Blue Ocean plugins and features. This means that you do not need to install the Blue Ocean plugins separately.
1. Create a bridge network in Docker using the following
docker network create
command:
$ docker network create jenkins
2. Create the following volumes to share the Docker client TLS certificates needed to connect to the Docker daemon and persist the Jenkins data using the following
docker volume create
commands:
$ docker volume create jenkins-docker-certs
$ docker volume create jenkins-data
3. In order to execute Docker commands inside Jenkins nodes, download and run the
docker:dind
Docker image using the followingdocker container run
command:
$ docker container run --name jenkins-docker --rm --detach \
--privileged --network jenkins --network-alias docker \
--env DOCKER_TLS_CERTDIR=/certs \
--volume jenkins-docker-certs:/certs/client \
--volume jenkins-data:/var/jenkins_home \
--publish 2376:2376 docker:dind
4. Download the
jenkinsci/blueocean
image and run it as a container in Docker using the followingdocker container run
command:
$ docker container run --name jenkins-blueocean --rm --detach \
--network jenkins --env DOCKER_HOST=tcp://docker:2376 \
--env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 \
--volume jenkins-data:/var/jenkins_home \
--volume jenkins-docker-certs:/certs/client:ro \
--publish 8080:8080 --publish 50000:50000 jenkinsci/blueocean
To know the meaning of each line used in above script please refer to Jenkins documentation https://www.jenkins.io/doc/book/installing/
3. Post Installation
- open Jenkins on your browser using https://localhost:8080 . If you are using cloud service then you have to first update the inbound rule on port 8080 and then serve your website using https://<YOUR_IP_ADDRESS>:8080
- You will prompted for Security Key. For this you have to SSH into your Jenkins docker container that you have created earlier:
$ docker exec -it <CONTAINER-ID> /bin/bash
3. After you ssh into your container run the following command to get the initial security key
$ cat /var/jenkins_home/secrets/initialAdminPassword
4. Copy the password and paste it in Admin Password field of Jenkins.
You have successfully installed Jenkins on Your Docker Platform
Happy Learning!!