How to Build a Docker image using Jenkins

0
8506
How to Build a Docker image using Jenkins
Image Source: http://cdn.rancher.com

Image Source : http://cdn.rancher.com

As part of devops, docker is getting famous more than any other tools in that case when there will much frequent image update may happen. For that we can make a setup to automate it. Here will see how Jenkins help us to create the docker images from GIT repo.

Step 1:

The command used to launch the container was:

# docker run -d -u root --name jenkins -p 8080:8080 -p 50000:50000 -v /root/jenkins:/var/jenkins_home  jenkins:1.651.1-alpine

All plugins and configurations get persisted to the host at /root/jenkins. 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.

You can access the Jenkins dashboard using http://localhost:8080 or http://<<IP-Address>>:8080

Now will use the dashboard to configure the plugins and start building Docker Images.

Step 2:

Task: Install Plugin

  1. Within the 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 Docker plugin. There are multiple Docker plugins, select Docker plugin using the checkbox.
  5. While on this page, install the Git plugin for obtaining the source code from a Git repository.
  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.

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

Step 3:

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.

  1. Once again, 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.
  4. You can now configure the container options. Set the name of the agent to docker-agent.
  5. The “Docker URL” 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://<<IP-Address>>:2345
  6. Use Test Connection to verify Jenkins can talk to the Docker Daemon. You should see the Docker version number returned.

Task: Configure Image

Our plugin can now communicate with Docker. In this step, we’ll 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 motoskia/test-apache. This image is configured with a Docker client and available at https://hub.docker.com/r/motoskia/test-apache/
  3. To enable builds to specify Docker as a build agent, set a label of docker-apache-agent.
  4. Jenkins uses SSH to communicate with agents. Add a new set of “Credentials”. The username is jenkins and the password is jenkins. (you can give any username/password based on your environment/setup)
  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:

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/foxutech/apacheDockerFile . 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

  1. On the Jenkins dashboard, select Create new jobs
  2. Give the job a friendly name such as apache Jenkins and select Freestyle project.
  3. 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”.
  4. Select the Repository type as Git and set the Repository to be https://github.com/foxutech/apacheDockerFile . If Git is not in the “Source Code Management” list, you need to install the Git plugin as mentioned in step 2.
  5. We can now add a new Build Step using the dropdown. Select Execute Shell.
  6. Because the logical of how to build is specified in our Dockerfile, Jenkins only needs to call build and specify a friendly name.

For example

docker info
docker build -t motoskia/test-apache:${BUILD_NUMBER} 
docker tag motoskia/test-apache:${BUILD_NUMBER} motoskia/test-apache:latest
docker images

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 centralized Docker Registry.

  1. Our build is now complete. Click Save.

Step 5:

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 moment or two.

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

As it’s a container, you can view it using the Docker CLI tools docker ps -a

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 motoskia/test-apache:latest

Using cURL you should see the server respond: curl docker

NO COMMENTS