There’s a nice range of approaches to steady integration and steady supply (CI/CD). Containerizing apps with Docker is a typical step to take, for a wide range of causes. As soon as containerized, a key aspect to the CI pipeline is to construct a picture upon check-in, run the assessments, after which publish the picture to Docker Hub (or one other registry) to be used by downstream steps.
This text focuses on how one can arrange Jenkins to drag up to date software code from GitHub, construct a Docker picture, run the assessments, and publish it to the registry.
You’ll use a easy Java app for this goal, however the course of is fairly comparable throughout totally different stacks.
Publish to Docker Hub
This tutorial does a great job of introducing the fundamentals of Java, Jenkins, and Docker. We’re going to take it a step additional right here and describe how one can pull the most recent GitHub check-in, construct a Docker picture of the app, and publish it to Docker Hub if profitable. You’ll want a Docker Hub account, which is free.
Publishing to a central container picture repo (on this case, Docker Hub) is a key aspect to enterprise CI/CD, as a result of it offers a typical location for reuse of the artifact at varied phases, from QA and take a look at to manufacturing.
Spin up Jenkins and Docker-in-Docker
To arrange Jenkins with the potential of operating Docker, we’ll run two Docker photos in a VM. One container will host “Docker-in-Docker” (the docker:dind picture) for accessing Docker itself and the opposite will host Jenkins. The 2 containers will work together through community and volumes. That is the tactic advocated by the Jenkins docs.
First, create a brand new VM in your cloud supplier of alternative. I’ll be utilizing Google Cloud Platform (GCP).
A superb technique to jumpstart this structure is to start out with a VM that already has Docker and the Docker-in-Docker picture put in. Within the GCP console, I entry the Compute Engine and click on Create New Occasion. I’m utilizing a reasonably beefy N1-Medium occasion as a result of each Docker and Jenkins are hefty installs.
The trick is to pick out Set up Picture. For the picture title, use docker:dind. It will make sure the Docker CLI is avaiable. Additionally, expose port 8080 to exterior visitors, as Jenkins might be listening on that port.
As soon as your VM is up, ensure that Docker is operating by typing docker --version
.
Now you’re going to create the Docker community bridge that the Jenkins and Docker-in-Docker containers will share. Execute docker community create jenkins
.
Arrange Docker-in-Docker
The VM is already operating the docker:dind picture, however we wish to customise it. So kind docker container listing
, discover the (first few characters of the) UID for the container and cease it with docker cease
. For fine-grained particulars on these configurations, verify the docs.
Now begin the Docker-in-Docker container again up with the command in Itemizing 1.
Itemizing 1. Run docker:dind
docker run --name jenkins-docker --rm --detach --privileged --network jenkins --network-alias docker --env DOCKER_TLS_CERTDIR="" --volume jenkins-docker-certs:/certs/shopper --volume jenkins-data:/var/jenkins_home --publish 2735:2375 --publish 2376:2376 docker:dind --storage-driver overlay2
Itemizing 1 begins up the docker picture whereas listening on port 2375, and taking part within the Jenkins community you created earlier. Be aware that 2375 is the non-SSL port; your safety necessities might necessitate listening on 2376 and offering applicable keys.
Run the Jenkins container
Now we’ll run an occasion of the official Jenkins picture, additionally taking part within the community bridge. Run the command in Itemizing 3.
Itemizing 3. Working the Jenkins picture in a container
sudo docker run --name my-jenkins --detach --network jenkins --env DOCKER_HOST=tcp://docker:2375 --volume /residence/matthewcarltyson/jenkins:/var/jenkins --volume /usr/bin/docker:/consumer/bin/docker --publish 8080:8080 --publish 50000:50000 --user root jenkins/jenkins:lt
Now Jenkins is operating with entry to Docker. It is best to be capable to hit your operating VM at :8080. Be aware that a number of of the flags on this command are non-compulsory — specifically, listening on 50000 is simply required if you’ll have Jenkins agent cases calling into this occasion as “grasp.”
Jenkins system password
Jenkins outputs a password to its logs. The online web page will ask for it. Go to the VM command line and kind docker container listing
to acquire your UID for the “jenkins” occasion, and kind docker logs --tail 50
to acquire your admin password.
Now enter the admin password into the net app type.
Configure Jenkins
Go forward and click on Set up Steered Plugins. Subsequent you’ll be requested to create an admin consumer. Try this. Additionally settle for the default IP tackle and restart the server to load the plug-ins when prompted.
Add the Docker plug-in
There are a number of Docker plug-ins out there. We’ll use the one imaginatively named “Docker plugin,” which supplies you numerous energy with minimal configuration. For extra detailed management, you may take into account the docker-build-step plug-in, or switching to a pipeline construct.
Go to Handle Jenkins -> Plugins -> Accessible and kind “docker” into the sector. Choose “Docker plugin” and set up it.
Jenkins refers back to the Docker plugin as a “cloud.” Click on Handle Jenkins as soon as once more, and now click on the Handle Clouds and Nodes button within the center. Now click on Configure Clouds on the left.
It will let you entry the bridge community you created earlier and connect with the container operating Docker.
For the title, use one thing you’ll acknowledge like “Docker.” Click on Docker Cloud Particulars to reveal the Docker Host URI subject, and enter “tcp://docker:2375” there.
Reserve it and return to the Jenkins dashboard.
Arrange the Jenkins venture
Now we’ll arrange a Jenkins venture that may use two construct steps — one which pulls the most recent code from a Git repo and builds the app, and one which builds the picture and pushes it as much as Docker Hub. That is deliberately quite simple; in a real-world state of affairs, the app is likely to be constructed contained in the Dockerfile, or one other “infrastructure as code” (IaC) device.
Click on New Merchandise in the primary menu and choose Freestyle Venture. Give it a reputation you want.
Within the venture particulars, scroll to the Construct part, choose Execute Shell Script, and insert the contents of Itemizing 4.
Itemizing 4. Shell script to obtain and construct simple-java
#!/bin/shREPOSRC=https://github.com/MTyson/simple-java.git
LOCALREPO=idg-java-docker# We do it this manner in order that we are able to summary if from simply git afterward
LOCALREPO_VC_DIR=$LOCALREPO/.gitif [ ! -d $LOCALREPO_VC_DIR ]
then
git clone $REPOSRC $LOCALREPO
else
cd $LOCALREPO
git pull $REPOSRC
fichmod 777 mvnw
./mvnw clear package deal
# Finish
The script in Itemizing 4 will try or pull the most recent supply code for the Java venture discovered at https://github.com/MTyson/simple-java.git. That is only a easy “Howdy world” Spring Boot app. As soon as it has the most recent, the script modifies permission on the Maven command (in real-world use, 777 is just too permissive), after which executes the Maven construct.
Use the Docker plug-in to construct and publish the picture
Now add a construct step after the shell script and choose Construct/Publish Docker Picture. Within the Listing for Dockerfile subject, kind “./idg-java-docker.” This corrosponds to the repo title you used within the earlier step.
Within the Cloud subject, choose the “Docker” cloud you created earlier.
Choose the Push Picture checkbox.
Within the Picture subject, you’ll wish to put the title of the consumer or group at Docker Hub (in my case, matttyson) adopted by a slash and the picture title, which is “simple-java.” So the shape is username/simple-java. In my case, matttyson/simple-java.
Now choose Registry Credentials. In an actual venture, you’d positively wish to use a secret token (creatable at Docker.io) however we’ll hold it easy right here and use your consumer title and password from Docker Hub. Underneath Credentials, click on Add. Depart scope as World and use Username with Password. Now merely add your consumer title and password that you simply use to log into the Docker internet app.
After accepting the brand new credential, ensure that it’s set because the consumer on the construct step we’re configuring. Now save the construct.
Run the construct
Now click on your venture’s title on the high to return to its overview, then click on the Construct Now button on the left.
After a second, you’ll see the construct seem within the listing on the bottom-left. Click on it and choose Console Output on the left to observe the progress of the logs because the construct proceeds.
When the construct completes, you’ll be able to navigate through browser to your listing of repositories in Docker Hub to see your freshly baked new picture, as in Determine 1.
Determine 1. The brand new picture at Docker Hub
Git to the registry
Devops is an unlimited and complicated space. This text offers you the instruments for dealing with one vital aspect: taking a uncooked app from model management, and turning it into a picture out there on the Docker Hub registry.
Copyright © 2021 IDG Communications, Inc.