How to Create a Docker Pipeline With Jenkins
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.
- Log onto your Jenkins web console
- Navigate to Credentials > System > Global credentials from the top-left Jenkins menu.
- Click Add Credentials from the top-left sidebar menu.
- From the Kind dropdown, select SSH Username with private key
- Set the ID to a value you will reference in your pipeline scripts, such as jenkins_docker
- In the description field, set the value to something that will describe what the credential is used for.
- In the username field, set this value to the username of the Docker host(s).
- 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.
- 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.
Follow Us