Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda