Docker Guides

Docker tutorial : Build Docker images using Jenkins

Docker as we know, is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.

Today we are going to check how to configure Jenkins to build Docker Images based on a Dockerfile. Below are the steps of how you can use Docker within a CI/CD pipeline, using Images as a build artifact that can be promoted to different environments and finally production.

This quickstart assumes basic understanding of Docker concepts, please refer to earlier posts for understanding on Docker & how to install and containerize applications.

Step #1: Launch Jenkins

Currently, I have Jenkins running on Docker container, if you do docker ps command it would show you the status of the container.

Image – Docker ps command to review the status of the containers

Launch Jenkins’ dashboard

Build Docker images using Jenkins – Jenkins dashboard

Step #2: Configure the plugins and start building Docker Images.

Our 1st step is to configure the Docker plugin. Whenever a Jenkins 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.

  1. Once you are inside the Jenkins Dashboard, select Manage Jenkins on the left.
  2. On the Configuration page, select Manage Plugins.
  3. Manage Plugins page will give you a tabbed interface. Click Available to view all the Jenkins plugins that can be installed.
  4. Using the search box, search for the Docker plugin. There are multiple Docker plugins, select Docker plugin using the checkbox.

    Build Docker images using Jenkins – Install Docker Plugin

  5. While on this page, install the Git plugin for obtaining the source code from a Git repository.

    Build Docker images using Jenkins – Installing plugins

  6. Click Install without Restart at the bottom.
  7. The plugins will now be downloaded and installed. Once complete, click the link Go back to the top page.

Once the Docker & GIT plugins have been installed, now we can go ahead and configure how they launch the Docker Containers.

The configuration would be used by the plugin which Docker Image to use 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.

Step #3: Configure Docker agent

  1. On the Jenkins Dashboard, select Manage Jenkins.
  2. Select Configure System to access the main Jenkins settings.
  3. At the bottom, there is a dropdown called Add a new cloud. Select Docker from the list.

    Build Docker images using Jenkins – Docker Add new cloud

  4. You can now configure the container options. Set the name of the agent to docker-agent.

    Build Docker images using Jenkins – Set Docker Agent options

  5. The “Docker URL” is where Jenkins launches the agent container. In this case, we’ll use the same daemon as running Jenkins, but in real-world scenario it should be separate instances so that it can scale.
  6. Use Test Connection to verify Jenkins can talk to the Docker Daemon. You should see the Docker version number returned.

Now plugin can communicate with Docker, the next step would be to configure how to launch the Docker Image for the agent.

  1. Using the Images dropdown, select Add Docker Template dropdown.
  2. For the Docker Image, use sample one which has Docker client benhall/dind-jenkins-agent. This image is configured with a Docker client and available here.
  3. To enable builds to specify Docker as a build agent, set a label of docker-agent.
  4. Jenkins uses SSH to communicate with agents. Add a new set of “Credentials”. The username is jenkins and the password is jenkins.
  5. Finally, expand the Container Settings section by clicking the button. In the “Volumes” text box enter /var/run/docker.sock:/var/run/docker.sock
  6. Click Save.

Step #4: Test the setup

To test the setup, create a new job to

  1. On the Jenkins dashboard, select Create new jobs of type Freestyle project & create new job ex.Jenkins Demo.

    Build Docker images using Jenkins – Create New Project

  2. 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”.
  3. Select the Repository type as Git and set the Repository. I’m using my GIT location https://github.com/karthi4india/jenkins/.
  4. We can now add a new Build Step using the dropdown. Select Execute Shell.

    Build Docker images using Jenkins – Add Build Step

  5. Dockerfile takes care of build, Jenkins only needs to call build and specify a friendly name.

Build step :

ls 

docker info 

docker build -t jenkins-demo:${BUILD_NUMBER} . 

docker tag jenkins-demo:${BUILD_NUMBER} jenkins-demo:latest 

docker images

The first command 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.

Docker File:

FROM scratch
EXPOSE 80
COPY http-server /
CMD ["/http-server"]

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

Build Docker images using Jenkins – Jenkins Build

Jenkins is launching the container and connecting to it via SSH. Sometimes this can take a moment or two.

You can see the progress using

docker logs --tail=10 jenkins

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

docker images

Like this post? Don’t forget to share it!

Additional Resources:

Summary
Article Name
Docker tutorial : Build Docker images using Jenkins
Description
This article explains how you can use Docker within a CI/CD pipeline, using Images as a build artifact that can be promoted to different environments and finally production.
Author
Publisher Name
Upnxtblog
Publisher Logo
Karthik

Allo! My name is Karthik,experienced IT professional.Upnxtblog covers key technology trends that impacts technology industry.This includes Cloud computing,Blockchain,Machine learning & AI,Best mobile apps, Best tools/open source libs etc.,I hope you would love it and you can be sure that each post is fantastic and will be worth your time.

Share
Published by
Karthik

Recent Posts

Navigating Volatility: Investing in Crypto Derivatives and Risk Management Strategies

The cryptocurrency market is famed for its volatility, presenting each opportunity and demanding situations for…

2 weeks ago

How Game Developers Use AI in Mobile Games in 2024?

Games since time immemorial have been winning at captivating the users and teleporting them onto…

2 weeks ago

The Impact of AI on Software Development

We are living within an innovation curve wherein cutting-edge technologies are making a hustle and…

2 weeks ago

AI Tools for Research Paper Writing: Learn What They Can Do

Whether it’s the healthcare industry or the automobile sector, artificial intelligence has left its impact…

4 weeks ago

Embracing Innovation: 5 Ways AI is Transforming the Landscape in 2024

Facts only- The big Artificial Intelligence push is unraveling in 2024. No, it wasn’t merely…

4 weeks ago

The Startup Guide to Acquiring Exceptional Developers

In the fiercely competitive world of Hire Developers for Startup, success hinges not just on…

2 months ago

This website uses cookies.