DEV Community

Cover image for Docker + Jenkins: Chemistry
AnupamMahapatra
AnupamMahapatra

Posted on

Docker + Jenkins: Chemistry

With jenkins you can use docker in two contexts.

1 - use docker container from a private repo as the jenkins node to build your code

2 - use jenkins node to build your docker container and probably push it to your private repo

This was confusing when i started and so i want to declutter this :

Assuming

  • you have a jenkins single agent cluster with docker daemon running on that agent.
  • a private docker registry with credentials to pull and push

use docker container from a private repo as the jenkins node to build your code

pipeline {

    /* start with outmost.*/
    agent any

    /* This method fetches a container defination from the registered repo and build and runs the steps inside it.*/
    stages {
        stage('Test') {
            agent {
                    docker { 
                        image 'node:7-alpine' 
                        registryUrl 'https://my-docker-virtual.artifactory.rogers.com/'
                        registryCredentialsId 'ARTIFACTORY_USER'
                        }
            }
            steps {
                sh 'node --version'
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • IMPORTANT: Inside this container, i have no context of the docker which is running on bare metal jenkins. hence i have no context of the registry . I can not write a push command in the steps as i am inside the container.
  • I can also use a docker file as a agent definition instead of fetching a docker definition from registry
pipeline {
    agent { dockerfile true }
    stages {
        stage('Test') {
            steps {
                sh 'node --version'
                sh 'svn --version'
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Here it is looking into the root directory for the Dockerfile which it will use as a build agent.

use jenkins node to build your docker container and probably push it to your private repo

node {

    checkout scm

    docker.withRegistry('https://registry.hub.docker.com','dockerhubcredentails'){
    def myImage= docker.build("digitaldockerimages/busybox:0.0.1")

    /*push container to the registry*/
    myImage.push()
    }
}
Enter fullscreen mode Exit fullscreen mode

Here i am using a private registry and fetching the credentials for the registry from jenkins secret storage.

Ref:

https://www.youtube.com/watch?v=z32yzy4TrKM

https://jenkins.io/doc/book/pipeline/docker/#sidecar

https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Controlling-your-build-environment

https://issues.jenkins-ci.org/browse/JENKINS-39684

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay