'AttributeError: 'ImageCollection' object has no attribute 'tag' Docker sdk 2.0
I want to build, tag and push a docker image into an ecr repository using python3, from an ec2 i'm using docker and boto3 docker==4.0.1
here i am building the image and turns out ok
response = self._get_docker_client().images.build(path=build_info.context,
dockerfile=build_info.dockerfile,
tag=tag,
buildargs=build_info.arguments,
encoding=DOCKER_ENCODING,
quiet=False,
forcerm=True)
here it fails because he cant find the attribute "tag"
self._get_docker_client().images.tag(repository=repo, tag=tag, force=True)
another way to get the same error i'm trying to give the method a target image id to make the tag using the response of the build, i get in my IDE (intellij) 2 different methods to make tags, one with "ImageApimixin" and other with "Image" as you may see so i tried a different aproach 
for i in response[1]:
print(i)
if 'Successfully built' in i.get('stream', ''):
print('JECN BUILD data[stream]')
print(i['stream'])
image = i['stream'].strip().split()[-1]
print('JECN BUILD image')
print(image)
self._get_docker_client().images.tag(self, image=image, repository='999335850108.dkr.ecr.us-east-2.amazonaws.com/jenkins/alpine', tag='3.13.1', force=True)
in both cases i get the same error (i burned some code in the last try)
'ImageCollection' object has no attribute 'tag' amazon-ebs: Process Process-1:2: amazon-ebs: Traceback (most recent call last): amazon-ebs: File "/root/.local/lib/python3.7/site-packages/bulldocker/services/build_service.py", line 25, in perform amazon-ebs: build_info.image_id = self.__build(build_info) amazon-ebs: File "/root/.local/lib/python3.7/site-packages/bulldocker/services/build_service.py", line 114, in __build amazon-ebs: self._get_docker_client().images.tag(self, image=image, repository='999335850108.dkr.ecr.us-east-2.amazonaws.com/jenkins/alpine', tag='3.13.1', force=True) amazon-ebs: AttributeError: 'ImageCollection' object has no attribute 'tag'
i still don't get why the library is confused here, when i look into the ImageCollection it only appears inside the Scope used by docker client and models libary but i really ran out of ideas 
here i build my docker client
def get_ecr_docker_client():
print('JECN GETTING DOCKER CLIENT')
access_key_id, secret_access_key, aws_external_id = get_param_from_store()
aws_region = DEFAULT_AWS_REGION
print(os.environ)
print(access_key_id)
print(secret_access_key)
docker_client = docker.from_env()
client = boto3.client(
service_name='sts',
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
region_name=aws_region,
)
assumed_role_object = client.assume_role(
RoleArn="arn:aws:iam::999335850108:role/adl-pre-ops-jenkins-role",
RoleSessionName="AssumeRoleSession1",
ExternalId=aws_external_id
)
credentials = assumed_role_object['Credentials']
ecr_client = boto3.client(
service_name='ecr',
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'],
region_name=aws_region
)
ecr_credentials = \
(ecr_client.get_authorization_token(registryIds=[ECR_OPERATIONAL_REGISTRY_ID]))['authorizationData'][0]
ecr_username = 'AWS'
decoded = base64.b64decode(ecr_credentials['authorizationToken'])
ecr_password = (base64.b64decode(ecr_credentials['authorizationToken']).replace(b'AWS:', b'').decode('utf-8'))
ecr_url = ecr_credentials['proxyEndpoint']
docker_client.login(
username=ecr_username, password=ecr_password, registry=ecr_url)
return docker_client
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

