为什么会收到“ https://registry-1.docker.io/v2/{reponame}/tags/list:未授权:错误的用户名或密码”?

时间:2019-05-28 09:51:15

标签: java docker kotlin aws-ecr docker-java

摘要

我正在尝试使用docker-java库,但是在尝试提取图像时遇到了问题。我正在尝试从ECR上的私有存储库中提取信息。

我尝试过的

我一直在尝试的实现大部分来自以下问题:
Pulling image from Amazon ECR using docker-java

区别在于我正在Kotlin中实现它。我正在MacOS上进行开发。

执行dockerClient.authCmd()时出现“成功登录”,因此我可以很好地登录。

代码

        val awsCredentialsProvider: AWSCredentialsProvider = 
                DefaultAWSCredentialsProviderChain()
        val ecrClient = AmazonECRClientBuilder.standard()
                .withRegion(Regions.EU_WEST_2)
                .withCredentials(awsCredentialsProvider)
                .build()

        val getAuthTokenRequest = GetAuthorizationTokenRequest()
        val registryIds = ArrayList<String>()
        registryIds.add(registryId)
        getAuthTokenRequest.setRegistryIds(registryIds)

        val getAuthTokenResult = ecrClient.getAuthorizationToken(getAuthTokenRequest)
        val authData = getAuthTokenResult.authorizationData[0]
        val userPassword = StringUtils.newStringUtf8(Base64.decode(authData.authorizationToken))
        val user = userPassword.substring(0, userPassword.indexOf(":"))
        val password = userPassword.substring(userPassword.indexOf(":")+1)

        val config = DefaultDockerClientConfig.createDefaultConfigBuilder()
            config.withDockerTlsVerify(false)
            config.withRegistryUsername(user)
            config.withRegistryPassword(password)
            config.withRegistryUrl(authData.proxyEndpoint)
            config.build()

        //Docker client
        val dockerClient = DockerClientBuilder.getInstance(config)
            .withDockerCmdExecFactory(JerseyDockerCmdExecFactory())
            .build()

        val response = dockerClient.authCmd().exec()
        println(response.status)

        val pullImageCmd = dockerClient
                .pullImageCmd(nameOfRepository)
                .withAuthConfig(dockerClient.authConfig())
                .withRepository(nameOfRepository)

        pullImageCmd
                .exec(PullImageResultCallback())
                .awaitCompletion()

结果

我及以上出现以下错误:

Caused by: com.github.dockerjava.api.exception.InternalServerErrorException: {"message":"Get https://registry-1.docker.io/v2/{name_of_repo}/tags/list: unauthorized: incorrect username or password"}

URI中的registry-1.docker.io/v2/应该不是我自己的仓库吗? 有人可以阐明我遇到的问题吗?

0 个答案:

没有答案