Jenkins Docker Setup

/ Comments off

Today we’re going to learn how to build a Docker image using Jenkinsfile from a git repository and push it to the Docker Hub.

Passing Jenkins launcher parameters. Arguments you pass to docker running the jenkins image are passed to jenkins launcher, so you can run for example: $ docker run jenkins -version. This will dump Jenkins version, just like when you run jenkins as an executable war. You also can define jenkins arguments as JENKINSOPTS. Oct 06, 2020 Jenkins setup typically done through a web-based setup wizard can be a slow and error-prone. Jenkins Configuration as Code (JCasC) method can help us to automate the setup of Jenkins using Docker. As a part of our Server Management Services, we help our Customers with Docker related requests regularly. Let us today discuss the steps.

Create a new Jenkins Docker image

The official Jenkins image does not have docker installed in it. So if you try to access docker while running a container based on the official Jenkins image it would result in an error.

Since Jenkins runs on a container, you have to look for the password inside the jenkins container, and then copy&paste it on this form. Docker exec -it jenkins-nginxjenkins1 cat /var/jenkins.

How to solve this? we can create a new Jenkins Docker image by preinstalling Docker in it. Following is the Dockerfile that we use to create the new Jenkins Docker image.

Here we use the base image as Jenkins official image, download and install Docker on top of it. Later we use usermod command to change attributes of the docker and jenkins group.

Next, create a docker-compose.yml file to ease the process of Docker image creation.

Here, we mount a Docker volume myjenkins to /var/jenkins_home directory which lies inside the Docker container and we also map the Docker socket from host to the container.

Build and run the Docker image by executing the following command in the project directory.

Set up Jenkins

Once Jenkins files have been extracted, the Jenkins server will be fully up and running at http://localhost:8080.

You can find the initial admin password at /var/jenkins_home/secrets/initialAdminPassword as mentioned on the login page.

Next, we can install plugins as per our requirement.

It may take some time depending upon the number of plugins you choose to install. Once the plugins are installed, you will be prompted to create a first admin user which you can skip if you wish to continue as an admin user.

Create Jenkins job

On completion of the initial setup, create a new pipeline in Jenkins by selecting New Item.

Enter the name of the job and select the type of job you wish to run on Jenkins. We select the Pipeline option since we wish to create a Jenkins pipeline to execute a series of steps.

Docker

There are multiple options as triggers for Jenkins, however, we use the Polling method and set a schedule as * * * * * which will poll the SCM repository every minute.

Now in the Pipeline section, select the Pipeline script from SCM option, select SCM, and insert the URL of the SCM repository.

You can add credentials for authentication however, credentials are not required for repositories with public access.

You can also select a specific branch that you wish to build by adding the branch name in the Branch to build section. For example, add */main to build the main branch.

Click the Save button and go to Plugin Manager to install the Docker Build and Publish and Docker Pipeline plugin which helps us to build and push the Docker image to Docker Hub.

Add Jenkinsfile

Once the plugin has been installed, go ahead and add a Jenkinsfile script given below to the SCM repository which will be used by Jenkins while building a job.

Testing

Now, commit changes to the SCM repository to test and see if Jenkins can access the SCM repository and Jenkinsfile.

On successful completion of the job, you would be able to see the latest Docker image in your Docker Hub repository.

This plugin allows adding various Docker commands into your Jenkins Freestyle job as a build step.

If you would like to use Docker for dynamic node provisioning, you should check the Docker plugin.

Commands

Plugin currently support following commands:

  • commit changes in specified container
  • create new container from image
  • create image from Dockerfile
  • create exec command
  • kill container(s)
  • pull image from a repository
  • push image to a repository
  • remove container(s)
  • remove all containers
  • restart container(s)
  • start container(s)
  • stop container(s)
  • stop all containers
  • start/stop all containers created from specified image
  • start exec command

Build Variables

Some commands also export build variables, so that you can easily use them in subsequent build steps.

Variables exported by create and start commands:

DOCKER_CONTAINER_IDS - IDs of created/started containers

DOCKER_IP_$HOSTNAME - IP of running container with hostname $HOSTNAME

When port binding is set up (in start command), the following environment variables are exported:

DOCKER_HOST_BIND_PORTS - contains comma separated list of ports to which are bound docker container ports

DOCKER_HOST_PORT_$SCHEMA_$PORT - which docker container port is bound to this port (e.g. DOCKER_HOST_PORT_TCP_80 would contain value 8080 in case that container port 8080 is bound to port 80 on host)

DOCKER_HOST_SOCKET_$SCHEMA_$PORT - host IP:PORT to which conatiner $PORT using $SCHEMA is bound. E.g. with following port binding 127.0.0.1:80:8080, container TCP port is 8080 is bound to host port 80 on loopback and DOCKER_HOST_SOCKET_TCP_8080 will contain 127.0.0.1:80.

Jenkins docker setup free

Known limitations

Commands run without any issue only on master, on remote slaves some commands may fail execution. See JENKINS-24071 for details.

Build Nodes

The Docker service must be installed and running on nodes where you run the build.

Set Docker URL

In Jenkins global configuration, you need to specify Docker REST API URL.

Jenkins Docker Setup Java

Jenkins -> Manage Jenkins -> Configure System -> Docker Builder

  • Configure Docker server REST API URL
    • For Linux nodes, set the local socket unix:///var/run/docker.sock
    • For other nodes, you may need to set something like tcp://127.0.0.1:2375
  • Test the connection.

Commands

Jenkins Docker Setup Online

In the build area of ​​Jenkins Job, add the build step

There is a range of Docker Command options, roughly correlating with the Docker CLI commands.

Setup

Docker Pull

Fields

FieldDescription
Name of the image to pull (repository/image:tag)base name of the image
TagActual tag, e.g. 3.4.5 or latest
Registryhostname of registry used to store images e.g. mydocker.mycompany.com
Docker registry URLregistry URL to use for pull authentication, e.g. https://mydocker.mycompany.com
Registry Credentials...Choose correct credential here from the list...

Docker Tag

Fields

FieldDescription
Name of the image to tag (repository /image:tag)base name of the image
Target repository of the new tagdestination repository/image name e.g. dac/nodeynode
The tag to setActual destination tag, e.g. 3.4.5 or latest

Scenario 1: Tag a Dockerhub image with a local tag

Effective Docker command line

Settings

FieldValue
Name of the image to tag (repository /image:tag)library/node:8.9.4
Target repository of the new tagdac/nodeynode
The tag to set8.9.4
Jenkins docker setup linux

Result

Scenario 2: Tag a private repo image with a local tag

Effective Docker command line

Settings

FieldValue
Name of the image to tag (repository/image:tag)mydocker.mycompany.com/library/node:4.4.6
Target repository of the new tagdac/nodeynode
The tag to set4.4.6

Result

Docker Push

Fields

FieldDescription
Name of the image to push (repository /image)base name of the image
TagActual tag, e.g. 3.4.5 or latest
Registryhostname of registry used to store images e.g. mydocker.mycompany.com
Docker registryregistry hostname to use for authentication, e.g. mydocker.mycompany.com
Registry Credentials...Choose correct credential here from the list...

Scenario 1: Push a local image to a private repo

Effective Docker command line

Settings

FieldValue
Name of the image to push (repository /image)dac/nodeynode
Tag4.4.6
Registrymydocker.mycompany.com
Docker registrymydocker.mycompany.com
Registry Credentials...Choose correct credential here from the list...

Result

Jenkins Docker Setup
  • Jenkins Plugin ID: docker-build-step

Uses components from Docker Commons Plugin which provides APIs for other Docker-related plugins

See License