Welcome!

Building Docker Images using Jenkins

Difficulty:Advanced

Estimated Time:15-20 minutes

In this scenario you'll learn how to configure Jenkins to build Docker Images based on a Dockerfile. The scenario is designed to demostrate how you can use Docker within a CI/CD pipeline, using Images as a build artefact that can be promoted to different environments and finally production.

The environment pre-configured and running Jenkins build server for you to experiment and learn how it works. The steps guide you to installing the plugin, creating a build and viewing the results.

Congratulations!

You've completed the scenario!

Scenario Rating

This scenario took a new Jenkins build and configured it to build Docker Images. We then look an example HTTP server and created a build for it.

All plugins and configurations get persisted to the host (ssh [email protected]) at _/root/jenkins2112. Port 8080 opens the web dashboard, 50000 is used to communicate with other Jenkins agents. Finally, the image has an alpine base to reduce the size footprint.

Load Dashboard

The username is admin with the password the default 344827fbdbfb40d5aac067c7a07b9230

On your own system, the password can be found via docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassword

It may take a couple of seconds for Jenkins to finish starting and be available. In the next steps, you'll use the dashboard to configure the plugins and start building Docker Images.

Configure Docker Plugin

The first step is to configure the Docker plugin. The plugin is based on a Jenkins Cloud plugin. When a build requires Docker, it will create a "Cloud Agent" via the plugin. The agent will be a Docker Container configured to talk to our Docker Daemon.

The Jenkins build job will use this container to execute the build and create the image before being stopped. The Docker Image will be stored on the configured Docker Daemon. The Image can then be pushed to a Docker Registry ready for deployment.

Task: Install Plugin

Within the Dashboard, select Manage Jenkins on the left.

On the Configuration page, select Manage Plugins.

Manage Plugins page will give you a tabbed interface. Click Available to view all the Jenkins plugins that can be installed.

Using the search box, search for Docker. There are multiple Docker plugins, select Docker using the checkbox under the Cloud Providers header.

Click Install without Restart at the bottom.

The plugins will now be downloaded and installed. Once complete, click the link Go back to the top page.

Your Jenkins server can now be configured to build Docker Images.

Add Docker Agent

Once the plugins have been installed, you can configure how they launch the Docker Containers. The configuration will tell the plugin which Docker Image to use for the agent and which Docker daemon to run the containers and builds on.

The plugin treats Docker as a cloud provider, spinning up containers as and when the build requires them.

Task: Configure Plugin

This step configures the plugin to communicate with a Docker host/daemon.

Once again, select Manage Jenkins.

Select Configure System to access the main Jenkins settings.

At the bottom, there is a dropdown called Add a new cloud. Select Docker from the list.

The Docker Host URI is where Jenkins launches the agent container. In this case, we'll use the same daemon as running Jenkins, but you could split the two for scaling. Enter the URL tcp://[[HOST_IP]]:2345

Use Test Connection to verify Jenkins can talk to the Docker Daemon. You should see the Docker version number returned.

The Host IP address is the IP of your build agent / Docker Host.

Task: Configure Docker Agent Template

The Docker Agent Template is the Container which will be started to handle your build process.

Click Docker Agent templates... and then Add Docker Template. You can now configure the container options.

Set the label of the agent to docker-agent. This is used by the Jenkins builds to indicate it should be built via the Docker Agent we're defining.

Under Container Settings, In the "Volumes" text box enter /var/run/docker.sock:/var/run/docker.sock. This allows our build container to communicate with the host.

For Connect Method select Connect with SSH. The image is based on the Jenkins SSH Slave image meaning the default Inject SSH key will handle the authenication.

Make sure it is Enabled.

Click Save.

Jenkins can now start a Build Agent as a container when required.

Create Build Project

This step creates a new project which Jenkins will build via our new agent. The project source code is at https://github.com/katacoda/katacoda-jenkins-demo. The repository has a Dockerfile; this defines the instructions on how to produce the Docker Image. Jenkins doesn't need to know the details of how our project is built.

Task: Create New Job

On the Jenkins dashboard, select Create new jobs

Give the job a friendly name such as Katacoda Jenkins Demo, select Freestyle project then click OK.

The build will depend on having access to Docker. Using the "Restrict where this project can be run" we can define the label we set of our configured Docker agent. The set "Label Expression" to docker-agent. You should have a configuration of "Label is serviced by no nodes and 1 cloud".

If you see the error message There’s no agent/cloud that matches this assignment. Did you mean ‘master’ instead of ‘docker-agent’?, then the Docker plugin and the Docker Agent has not been Enabled. Go back to configure the system options and enable both checkboxes.

Select the Repository type as Git and set the Repository to be https://github.com/katacoda/katacoda-jenkins-demo.

We can now add a new build step using the Add Build Step dropdown. Select Execute Shell.

Because the logical of how to build is specified in our Dockerfile, Jenkins only needs to call build and specify a friendly name.

The first stage lists all the files in the directory which will be built. When calling docker build we use the Jenkins build number as the image tag. This allows us to version our Docker Images. We also tag the build with latest.

At this point, or in an additional step, you could execute a docker push to upload the image to a centralised Docker Registry.

Our build is now complete. Click Save.

Build Project

We now have a configured job that will build Docker Images based on our Git repository. The next stage is to test and try it.

Task: Build

On the left-hand side, select Build Now. You should see a build scheduled with a message "(pending—Waiting for next available executor)".

In the background, Jenkins is launching the container and connecting to it via SSH. Sometimes this can take a while to configure the Docker Agent. The error "(pending—Jenkins doesn’t have label docker-agent)" is while Jenkins waits for the Docker Agent to start.

You can see the progress using docker logs --tail=10 jenkins

It's normal for this to take a few moments to complete.

View Console Output

Once the build has completed you should see the Image and Tags using the Docker CLI docker images.

What was built into the Docker Image was a small HTTP server. You can launch it using:
docker run -d -p 80:80 katacoda/jenkins-demo:latest

Using cURL you should see the server respond:
curl host01

Jenkins will have the console output of our build, available via the dashboard. You should be able to access it below:

Help

Katacoda offerings an Interactive Learning Environment for Developers. This course uses a command line and a pre-configured sandboxed environment for you to use. Below are useful commands when working with the environment.

cd <directory>

Change directory

ls

List directory

echo 'contents' > <file>

Write contents to a file

cat <file>

Output contents of file

Vim

In the case of certain exercises you will be required to edit files or text. The best approach is with Vim. Vim has two different modes, one for entering commands (Command Mode) and the other for entering text (Insert Mode). You need to switch between these two modes based on what you want to do. The basic commands are: