Install Docker On Jenkins



Overview

This tutorial explains, how to install docker on EC2 Instance. Before starting, let's have some brief knowledge about Docker. Docker is a platform that combines applications and its dependencies in the form of packages, called container.

Basically the solution is to build a docker container which will build upon jenkins and install docker into it, at the end its basically jenkins with the docker client (not server) installed, and when running it you connect it to the host docker.sock as was mentioned above as well. The resource utilization of the Jenkins slaves is very less if you do not have builds happening continuously. In this scenario, it is better to use ephemeral Docker containers as Jenkins build slaves for better resource utilization. As you know, spinning up a new container takes less than a minute; every build spins up a new container, builds the project, and is destroyed. This way, you can. Follow the instructions for creating a docker image that can be used as a Jenkins Agent; Docker Environment. Follow the installation steps on the docker website. If your Jenkins instance is not on the same OS as the docker install, you will need to open the docker TCP port so that Jenkins can communicate with the docker daemon.

This plugin allows containers to be dynamically provisioned as Jenkins nodes using Docker. It is a Jenkins Cloud plugin for Docker.

The aim of this docker plugin is to be able to use a Docker host to dynamically provision a docker container as a Jenkins agent node, let that run a single build, then tear-down that node, without the build process (or Jenkins job definition) requiring any awareness of docker.

The Jenkins administrator configures Jenkins with knowledge of one or more docker hosts (or swarms), knowledge of one or more 'templates' (which describe the labels/tags that this template provides, the docker image, how to start it, etc) and Jenkins can then run docker containers to provide Jenkins (agent) Nodes on which Jenkins can run builds.

See also

  • Support and contribution guide

Note: There is more than one docker plugin for Jenkins. While this can be confusing for end-users, it's even more confusing when end users report bugs in the wrong place. e.g. if you are using Jenkins pipeline / workflow / Jenkinsfile builds with code including terms like docker.withDockerRegistry or docker.image etc then you're using the docker-workflow plugin and should go to its repository instead of this one.

Note: This plugin does not provide a Docker daemon; it allows Jenkins to use a docker daemon. i.e. Once you've installed docker on your OS, this plugin will allow Jenkins to use it.

A quick setup is :

  1. get a docker environment running
  2. follow the instructions for creating a docker image that can be used as a Jenkins Agent

Docker Environment

Install Jenkins On Docker Ubuntu

Follow the installation steps on the docker website.

If your Jenkins instance is not on the same OS as the docker install, you will need to open the docker TCP port so that Jenkins can communicate with the docker daemon. This can be achieved by editing the docker config file and setting (for example)

The docker configuration file location will depend your system, but it is likely to be /etc/init/docker.conf , /etc/default/docker or /etc/default/docker.io.

Multiple Docker Hosts

If you want to use more than just one physical node to run containers, you can use Docker Swarm Standalone or you can define more than one docker 'cloud'. The docker engine swarm mode API is not supported (at present; enhancement contributions would be welcomed).

To use the standalone swarm, follow docker swarm standalone instructions and configure Jenkins with the swarm's API endpoint.

Jenkins Configuration

Docker plugin is a 'Cloud' implementation. You'll need to edit Jenkins system configuration (Jenkins -> Manage -> System configuration) and add a new Cloud of type 'Docker'.

Configure Docker (or Swarm standalone) API URL with required credentials. The test button lets you check the connection.

Install docker on jenkins iii

Then configure Agent templates, assigning them labels that you can use so your jobs select the appropriate template, and set the docker container to be run with whatever container settings you require.

Creating a docker image

You need a docker image that can be used to run Jenkins agent runtime. Depending on the launch method you select, there's some prerequisites for the Docker image to be used:

Launch via SSH

  • sshd server and a JDK installed. You can use jenkins/ssh-agent as a basis for a custom image.
  • a SSH key (based on unique Jenkins master instance identity) can be injected in container on startup, you don't need any credential set as long as you use standard openssl sshd.
    For backward compatibility or non-standard sshd packaged in your docker image, you also have option to provide manually configured ssh credentials
  • Note: If the docker container's host SSH key is not trusted by Jenkins (usually the case) then you'll need to set the SSH host key verification method to 'non-verifying'.

Launch via JNLP

  • a JDK installed. You can use jenkins/inbound-agent as a basis for a custom image.
  • Jenkins master URL has to be reachable from container.
  • container will be configured automatically with agent's name and secret, so you don't need any special configuration of the container.

Launch attached

  • a JDK installed. You can use jenkins/agent as a basis for a custom image.

To create a custom image and bundle your favorite tools, create a Dockerfile with the FROM to point to one of the jenkins/*-agent reference images, and install everything needed for your own usage, e.g.

Note on ENTRYPOINT

Avoid overriding the docker command, as the SSH Launcher relies on it.

You can use an Entrypoint to run some side service inside your build agent container before the agent runtime starts and establish a connection ... but you MUST ensure your entrypoint eventually runs the passed command:

Further information

More information can be obtained from the online help built into the Jenkins WebUI. Most configurable fields have explanatory text. This, combined with knowledge of docker itself, should answer most questions.

Jenkins can be configured using Groovy code, and the docker plugin is no exception. For example, this configuration script could be run automatically upon Jenkins post-initialization or through the Jenkins script console. If run, this script will configure the docker-plugin to look for a docker daemon running within the same OS as the Jenkins master (connecting to Docker service through unix:///var/run/docker.sock) and with the containers connecting to Jenkins using the 'attach' method.

This tutorial explains, how to install docker on EC2 Instance. Before starting, let’s have some brief knowledge about Docker.

Docker is a platform that combines applications and its dependencies in the form of packages, called container. We can run multiple containers on a single Operating System. Containers are not Virtual Machines. Virtual Machines requires Operating System to run applications, but containers do not. As containers don’t need an Operating System, it takes very little time to start and hence it is very faster than Virtual Machines.

Also Learn: How to install Ansible on Amazon Linux(EC2 )

and Launching Amazon Linux EC2 Instance

Let’s Install Docker on EC2 Instance

In this tutorial, we will use Amazon Linux to install docker. Amazon Linux/Amazon Linux2 is the Linux Operating System used in EC2 Instance.

Step 1– Log to Linux EC2 Instance and update it.

$ sudo yum update -y

Step 2– Install docker using Yum command

$ sudo yum install docker -y

Step 3– Start Docker Service

$ Service docker start

Step 4– Check docker Version.

$ docker -v

Basic Configuration (Creating Docker File / Running Docker Image)

Install Docker On Jenkins Iii

After you install docker on ec2 , let’s have some basic setup for Docker.

Step 1 – Create a docker file

Dockerfile is a text file where we write instructions to build an image.

Create a folder name Images on the Amazon Linux box.

$ mkdir images

Move to folder images

$ cd images

Create a file name Dockerfile inside images folder

$ nano Dockerfile

Put the content in the following way inside the Dockerfile.

FROM Ubuntu ( It will use the Official image Ubuntu to build your image)

MAINTAINER chandan <[email protected]>

( It sets the Author field of the generated image)

RUN apt-get update ( It will update the image at the time of Build)

CMD [ “echo”, ” Hello World ……….! “] ( It will run echo command when we run the docker container image)

Please also readHow to install docker on ec2 Instance(Windows)

You may also likeInstall Docker on Ubuntu using Script

Step 2– Run the docker build command to build the image.

$ docker build .

Note : ( . command is used when you are inside images folder where Dockerfile is present, alternatively you can put the docker file absolute path like /home/ec2-user/images/Dockerfile)

You can run the following command as well to Name and tag your Image. Here myimage is the Name and 01 is Tag of the image.

Install Docker On Jenkins Container

$ docker build -t myimage:01 .

Step 3– See the Docker Images after build

$ docker images

Step 4 – Run the docker image

$ docker run 07deaa33585e

Here 07deaa33585e is the IMAGE ID that can be seen in docker images command.

You can see here, it executed the echo command as soon as we run the docker image. In the same way, we can run any script, commands or any application by running a docker image.

Click to tweet this tip !

I hope you enjoyed this tutorial and learned to install docker on ec2 instance (Amazon Linux). If you think this is really helpful, please do share this with others as well. Please also share your valuable feedback, comment or any query in the comment box. I will really happy to resolve your all queries anytime.

Thank You

Install Jenkins Agent On Docker In Ubuntu

If you think we helped you or just want to support us, please consider these:-

Install Docker On Jenkins Facebook

Connect to us: Facebook | Twitter