準備
$ pip install boto3
$ pip install docker
ECR から pull
import boto3
import base64
import docker
account_id = '123456789012'
region = 'ap-northeast-1'
name = '%s.dkr.ecr.%s.amazonaws.com/%s' % (account_id, region, 'example')
session = boto3.Session(
profile_name = SWITCH_ACCOUNT_PROFILE,
region_name = region
)
ecr = session.client('ecr')
authorization_token = ecr.get_authorization_token()['authorizationData'][0]
token = base64.b64decode(authorization_token['authorizationToken']).split(':')
user = token[0]
pwd = token[1]
auth_cred = { 'username': user, 'password': pwd }
client = docker.from_env()
client.images.pull(name, auth_config = auth_cred)
転送先の ECR 用に tag を設定
import docker
src_account_id = '123456789012'
dist_account_id = '210987654321'
region = 'ap-northeast-1'
src = '%s.dkr.ecr.%s.amazonaws.com/%s' % (src_account_id, region, 'example')
dist = '%s.dkr.ecr.%s.amazonaws.com/%s' % (dist_account_id, region, 'example')
client = docker.from_env()
images = client.images.list(name = src)
for image in images:
for tag in image.tags:
t = tag.split(':')[1]
image.tag(dist, t)
別のアカウントの ECR に push
import boto3
import base64
import docker
account_id = '210987654321'
region = 'ap-northeast-1'
session = boto3.Session(
profile_name = SWITCH_ANOTHER_ACCOUNT_PROFILE,
region_name = region
)
name = '%s.dkr.ecr.%s.amazonaws.com/%s' % (account_id, region, 'example')
ecr = session.client('ecr')
authorization_token = ecr.get_authorization_token()['authorizationData'][0]
token = base64.b64decode(authorization_token['authorizationToken']).split(':')
user = token[0]
pwd = token[1]
auth_cred = { 'username': user, 'password': pwd }
client = docker.from_env()
images = client.images.list(name = name)
for image in images:
for tag in image.tags:
client.images.push(tag, auth_config = auth_cred)