How to Create a Docker Pipeline With Jenkins

Published: August 30, 2020 by Author's Photo Shane Rainville | Reading time: 3 minutes
Learn how to create a Docker pipeline with Jenkins to build and run containers on a remote host

In this tutorial, you will learn how to create a Docker pipeline with Jenkins that will build, deploy, and run containers on remote hosts.

What You will Learn

  • Executing Docker commands on remote hosts
  • Creating a Docker pipeline job in Jenkins

Prerequisites

In order to create a Docker CICD pipeline in Jenkins, you will need the following.

  • A Jenkins instance
  • Docker installed on the Jenkins node
  • SSHAgent plugin installed on Jenkins

Dockerfile

Remote Docker Commands

Docker supports executing commands on a remote host, which will be key for creating a continuous delivery pipeline. The connection type used will be SSH for increased security.

To execute a remote command with Docker you use the -H or --host flag.

docker --host ssh://user@host [options] [commands]

The SSH connection will require a RSA key-pair for the user account used to make the remote connection.

RSA Key Pair

Create a new key-pair using the ssh-key command. When you are prompted for a passphrase press Enter. Do not file in a passphrase, as the remote connection will not prompt for it. This will cause the connection to fail.

ssh-keygen jenkins_id_rsa

Two files will be generated by this command: a private key file and a public key file. Copy the public file to the user account on the remote host Jenkins will connect with. For example, if you have a user named jenkins on your remote Docker host, copy the new public to it’s profile with the ssh-copy-id command.

ssh-copy-id jenkins@docker.host -i jenkins_id_rsa

The private key, which must be stored securely to protect against unauthorized access, will be added to a Credential in Jenkins.

Jenkins Credential

With the RSA public key installed on your remote Docker host(s), you will now need to add your private key as a Jenkins Credential, which allows you to use the key securely within your pipelines.

  1. Log onto your Jenkins web console
  2. Navigate to Credentials > System > Global credentials from the top-left Jenkins menu.
  3. Click Add Credentials from the top-left sidebar menu.
  4. From the Kind dropdown, select SSH Username with private key
  5. Set the ID to a value you will reference in your pipeline scripts, such as jenkins_docker
  6. In the description field, set the value to something that will describe what the credential is used for.
  7. In the username field, set this value to the username of the Docker host(s).
  8. In the Private Key field, select Enter Directly and then paste the entire contents of the private created earlier. When done click the Add button.
  9. Click the Ok button.

You know have an SSH credential for your remote Docker hosts.

Docker Pipeline

The Docker pipeline in this example is fairly basic. We will begin by building an a new Docker image for our application, which will use a few built-in Jenkins variables for versioning.

Once built, the new Docker image will be deployed to our image repository. This provides as an artifact to deliver into any environment, from staging to production.

final String staging_docker_host = "ssh://jenkins@staging.docker.host"    
final String prod_docker_host = "ssh://jenkins@prod.docker.host"

node {
    stage("Build") {
        sh "docker build -t myapp:${version} ."
    }
    stage("Deploy Artifact") {
        sh "docker push -t myapp:${version}"
    }
}

stage("Staging Deploy Approval") {
    input "Deploy to Staging?"
}

node {
    stage("Deploy Stagin"){
        withEnv(["DOCKER_HOST=${staging_docker_host}"]) {
            sshagent( credentials: ['jenkins_docker']) {
                sh "docker run -d -p 80:80 myapp:${version}"
            }
        }
    }
}

stage("Production Deploy Approval") {
    input "Deploy to Prod?"
}

node {
    stage("Deploy Prod"){
        withEnv(["DOCKER_HOST=${staging_docker_host}"]) {
            sshagent( credentials: ['jenkins_docker']) {
                sh "docker -h ${prod_docker_host} run -d -p 80:80 myapp:${version}"
            }
        }
    }
}

Conclusion

In this tutorial, you have learned how to use Jenkins to perform remote Docker commands using the Docker CLI and SSH private/public key-pairs. While the example pipeline file above is basic, it shows how someone could implement a complete CI/CD Docker workflow with Jenkins.

Author Photo
Blogger, Developer, pipeline builder, cloud engineer, and DevSecOps specialist. I have been working in the cloud for over a decade and running containized workloads since 2012, with gigs at small startups to large financial enterprises.

How to Deploy Jenkins on Kubernetes

Publised August 24, 2020 by Shane Rainville

Learn how to create a Docker pipeline with Jenkins to build and run containers on a remote host

How to Deploy Jekyll on Kubernetes

Publised September 15, 2020 by Shane Rainville

Learn how to create a Docker pipeline with Jenkins to build and run containers on a remote host

How to Deploy Java Apps with Tomcat on Kubernetes

Publised September 1, 2020 by Shane Rainville

Learn how to create a Docker pipeline with Jenkins to build and run containers on a remote host

How to Check Memory and CPU Utilization of Docker Container

Publised August 28, 2020 by Shane Rainville

Learn how to create a Docker pipeline with Jenkins to build and run containers on a remote host

How to Remove Docker Containers and Volumes

Publised August 28, 2020 by Shane Rainville

Learn how to create a Docker pipeline with Jenkins to build and run containers on a remote host

How to Add Persistent Data to Mysql with Docker Compose

Publised August 27, 2020 by Shane Rainville

Learn how to create a Docker pipeline with Jenkins to build and run containers on a remote host

How to Set PHP Options for Wordpress in Docker

Publised August 27, 2020 by Shane Rainville

Learn how to create a Docker pipeline with Jenkins to build and run containers on a remote host

How to Solve Wordpress Redirects to Localhost 8080

Publised August 27, 2020 by Shane Rainville

Learn how to create a Docker pipeline with Jenkins to build and run containers on a remote host