Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
How do I push and pull from docker hub in Jenkins declarative pipeline

I am successfully building docker images from my jenkinsfile pipeline on a slave, just using standard "sh" commands.

I want to be able to try and pull an image from docker/hub, and if it fails (because it hasn't been saved there yet) build it and push it to docker hub.

Obviously I somehow need to store credentials for docker hub and provide them to a docker login command.

My problem is I am confused. There are docker pipeline plugins, but I think they are for running jenkins steps inside a docker container - not what I am trying to do. Also many examples seem to be scripted pipeline rather than declarative.

In simple "steps" terms I think I want do do something like

agent {
    label 'pi'
}
steps {
  sh 'docker login -u my-account -p xxxx'
  sh 'docker pull my-account/image:version || (docker build -f dockerfile -t my-account/image:version && docker push my-account/image:version)'
....
}

but I also suspect I maybe need to do something like

steps {
  script {
    withDockerRegistry([credentialsId: 'something', url: 'docker.io/my-account']) {
      sh 'docker pull my-account/image:version || (docker build -f dockerfile -t my-account/image:version && docker push my-account/image:version)'
    ....
  }
}

but I don't want to make any mistakes and screw a pipeline that is currently working fine except for this little bit.

Am I along the right lines?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

If you have the Docker pipeline plugin you can create and push images like this.

dir(config.buildFolder){
                newImage = docker.build(${imageTag})
                docker.withRegistry("https://${registryAddress}", '${credentialsId}'){
                     newImage.push("${variables.version}")

            }

You can pull and execute some code inside a container like this:

testbed = docker.image('${imageName}')
testbed.inside("-u root:root"){
                echo 'executing build inside container'
                sh 'dotnet restore'
            }

This will automatically mount the folder from which you created the container as root directory of your container so that you can work with that data (like build an application)

over 4 years ago · Santiago Trujillo Report

0

I'm also working on the same issue. I wish to pull Docker images from my Dockerhub repo and perform some tests with them. I have managed to get my pipeline working by following the steps shown here.

https://gist.github.com/jglick/0aa389c053196e38e2a1

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!