如何使用 hdockerpy 和 aws ecr repo 修复无基本身份验证凭据错误?

How fix no basic auth credentials error wit hdockerpy and aws ecr repo?

我正在尝试执行以下 python 代码:

import logging
import sys
import docker, boto3
from base64 import b64decode

logging.basicConfig(stream=sys.stdout, level=logging.INFO)

LOCAL_REPOSITORY = '111111111111.dkr.ecr.us-east-1.amazonaws.com/my_repo:latest'

image = '111111111111.dkr.ecr.us-east-1.amazonaws.com/my_repo'
ecr_registry, _ = image.split('/', 1)
client = docker.from_env()

# Get login credentials from AWS for the ECR registry.
ecr = boto3.client('ecr')
response = ecr.get_authorization_token()
token = b64decode(response['authorizationData'][0]['authorizationToken'])
username, password = token.decode('utf-8').split(':', 1)

# Log in to the ECR registry with Docker.
client.login(username, password, registry=ecr_registry)
logging.info("loggined")
client.images.pull(image,   auth_config={
  username: username,
  password: password
})

出现异常:

C:\myPath>python app/pull_example.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:loggined
Traceback (most recent call last):
  File "C:\Python3\lib\site-packages\docker\api\client.py", line 261, in _raise_for_status
    response.raise_for_status()
  File "C:\Python3\lib\site-packages\requests\models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http+docker://localnpipe/v1.35/images/create?fromImage=111111111111.dkr.ecr.us-east-1.amazonaws.com%2Fmy_repo

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "app/pull_example.py", line 41, in <module>
    password: password
  File "C:\Python3\lib\site-packages\docker\models\images.py", line 445, in pull
    repository, tag=tag, stream=True, **kwargs
  File "C:\Python3\lib\site-packages\docker\api\image.py", line 415, in pull
    self._raise_for_status(response)
  File "C:\Python3\lib\site-packages\docker\api\client.py", line 263, in _raise_for_status
    raise create_api_error_from_http_exception(e)
  File "C:\Python3\lib\site-packages\docker\errors.py", line 31, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 500 Server Error: Internal Server Error ("Get https://111111111111.dkr.ecr.us-east-1.amazonaws.com/v2/my_repo/tags/list: no basic auth credentials")

问题是什么?为什么即使在 client.login 调用之后我也无法拉取图像,而这种情况在没有任何例外的情况下发生。从 ECR 存储库和 dockerpy 执行登录和拉取镜像的正确方法是什么?

这是由于 - https://github.com/docker/docker-py/issues/2157

删除 ~/.docker/config.json 解决了问题。