Pushing to a Registry

After building an image, push it to a container registry like Docker Hub or a private registry.

Syntaxdocker.withRegistry('https://index.docker.io/v1/', 'dockerhub-login') { image.push() }

Once an image is built, you push it to a registry so other systems (like Kubernetes) can pull and run it.

Authenticating

Use docker.withRegistry(url, credentialsId) to log in with a stored credential, then push inside that block.

Common registries

  • Docker Hub — the default public registry.
  • GitHub Container Registry (ghcr.io).
  • Cloud registries — AWS ECR, Google Artifact Registry, Azure ACR.
  • Private/self-hosted — Harbor, Nexus.

Example

Example · bash
pipeline {
    agent any
    stages {
        stage('Build & Push') {
            steps {
                script {
                    def img = docker.build("myuser/myapp:${env.BUILD_NUMBER}")
                    docker.withRegistry('https://index.docker.io/v1/', 'dockerhub-login') {
                        img.push()
                        img.push('latest')
                    }
                }
            }
        }
    }
}

When to use it

  • After a successful CI build, the pipeline pushes the Docker image to Docker Hub tagged with both the build number and 'latest'.
  • A pipeline authenticates to Amazon ECR using an AWS credential binding and pushes the image to a private registry.
  • A multi-branch pipeline pushes images to different registries depending on whether the branch is main or a feature branch.

More examples

Push to Docker Hub

Logs in to Docker Hub using masked credentials and pushes two tags: the build number and latest.

Example · groovy
stage('Push Image') {
  steps {
    withCredentials([usernamePassword(
      credentialsId: 'dockerhub-creds',
      usernameVariable: 'HUB_USER',
      passwordVariable: 'HUB_PASS'
    )]) {
      sh """
        echo $HUB_PASS | docker login -u $HUB_USER --password-stdin
        docker push myorg/myapp:${BUILD_NUMBER}
        docker push myorg/myapp:latest
      """
    }
  }
}

Push to Amazon ECR

Authenticates to Amazon ECR using an AWS credential binding and pushes the built image.

Example · groovy
stage('Push to ECR') {
  steps {
    withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
                       credentialsId: 'aws-ecr-creds']]) {
      sh """
        aws ecr get-login-password --region us-east-1 \\
          | docker login --username AWS --password-stdin \\
              123456789.dkr.ecr.us-east-1.amazonaws.com
        docker push 123456789.dkr.ecr.us-east-1.amazonaws.com/myapp:${BUILD_NUMBER}
      """
    }
  }
}

Docker Pipeline plugin push

Uses the Docker Pipeline plugin to build and push an image to a private registry with a single credentialsId.

Example · groovy
stage('Build and Push') {
  steps {
    script {
      docker.withRegistry('https://registry.example.com', 'registry-creds') {
        def img = docker.build("myapp:${env.BUILD_NUMBER}")
        img.push()
        img.push('latest')
      }
    }
  }
}

Discussion

  • Be the first to comment on this lesson.