Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline: docker.withRegistry(...) failed

Tags:

docker

jenkins

OS: MacOS Ventura 13.0.1
Jenkins LTS 2.361.3 installed from homebrew, running in my local MacOS host.
Docker 20.10.20 from Docker Desktop 

I'm struggling to build up my Jenkins CI pipeline with the environments explained above. Here is my Jenkinsfile describing the declarative pipeline.

Jenkinsfile:

pipeline {
    agent any

    // polling by schedular
    // triggers {
    //     pollSCM('*/3 * * * *')
    // }

    environment {
        registry = 'my-docker-repository-name'
        registryCredential = 'my-jenkins-credential-for-dockerhub'
        dockerImage = ''
    }

    stages {
        stage('Echo') {
            agent any
            steps {
                echo 'from github repository'
                echo 'build number is ' + "${env.BUILD_NUMBER}"
            }
        }
        // stage for cloning your github repository
        stage('Prepare') {
            agent any
            steps {
                echo 'Cloning Repository'
                git url: 'my-github-repository-url',
                branch: 'master',
                credentialsId: 'jenkins-credential-for-github' 
            }
            post {
                failure {
                    error 'This pipeline stops here...'
                }
            }
        }

        // gradle build
        stage('Bulid Gradle') {
            agent any
            steps {
                echo 'Bulid Gradle'
                dir ('.'){
                  sh './gradlew clean build'
                }
            }
            post {
                failure {
                    error 'This pipeline stops here...'
                }
            }
        }

        // docker build
        stage('Bulid Docker') {
            agent any
            steps {
                echo 'Bulid Docker'
                script {
                    dockerImage = docker.build registry
                }
            }
            post {
                failure {
                  error 'This pipeline stops here...'
                }
            }
        }

        // docker push
        stage('Push Docker') {
            agent any
            steps {
                echo 'Push Docker'
                script {
                    docker.withRegistry('https://registry.hub.docker.com', registryCredential) {
                        dockerImage.push("${env.BUILD_NUMBER}") 
                        dockerImage.push('latest')
                    }
                }
            }
            post {
                failure {
                    error 'This pipeline stops here...'
                }
            }
        }
    }
}

This pipeline failed at the stage 'Push Docker' with the message below:

Using the existing docker config file.Removing blacklisted property: authsRemoving blacklisted property: credsStore$ docker login -u me -p me https://registry.hub.docker.com
Current context "desktop-linux" is not found on the file system, please check your config file at /Users/me/.jenkins/workspace/ci-pipeline@2@tmp/6e9fd8bf-aa3e-4654-b7b6-54b6138478df/config.json

When I tried this script with docker.withRegistry('') in the Push Docker stage, (i.e., without url and dockerhub credential) I can push the image successfully.

Could you tell me what am I missing?

Thanks.

=== Other configuration files are below.

Dockerfile:

FROM openjdk:11-jdk
LABEL maintainer="email"
ARG JAR_FILE=build/libs/me-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} me.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/docker-springboot.jar"]

.docker/config.json

{
        "auths": {
                "https://index.docker.io/v1/": {},
                "registry.hub.docker.com": {}
        },
        "credsStore": "desktop",
        "currentContext": "desktop-linux"
}

/opt/homebrew/Cellar/jenkins-lts/2.361.3

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
        <key>Label</key>
                <string>Aqua</string>
                <string>Background</string>
                <string>LoginWindow</string>
                <string>StandardIO</string>
                <string>System</string>
        </array>
        <key>ProgramArguments</key>
        <array>
                <string>/opt/homebrew/opt/openjdk@17/bin/java</string>
                <string>-Dmail.smtp.starttls.enable=true</string>
                <string>-jar</string>
                <string>/opt/homebrew/opt/jenkins-lts/libexec/jenkins.war</string>
                <string>--httpListenAddress=127.0.0.1</string>
                <string>--httpPort=8888</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>EnvironmentVariables</key>
        <dict>
                <key>PATH</key>
                <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Docker.app/Contents/Resources/bin/:/Users/me/Library/Group\ Containers/group.com.docker</string>
        </dict>
</dict>
</plist>
like image 258
shoukou-lee Avatar asked Oct 20 '25 11:10

shoukou-lee


1 Answers

I had the same issue recently. Jenkins copies the config from .docker/config.json

If you update the currentContext to default, then it seems to work without any issues.

{
    "auths": {
            "https://index.docker.io/v1/": {},
            "registry.hub.docker.com": {}
    },
    "credsStore": "desktop",
    "currentContext": "default"
}

Should solve your issue. As it seems it solved mine. For my case, issue started happening after the recent docker update.

like image 144
Aniket Singla Avatar answered Oct 24 '25 11:10

Aniket Singla