2017-08-23 7 views
0

私はAWS ECR repoでDockerを使用しています。彼らが指示するステップの1つは、「ドッカータグ」を実行して、イメージがECRに保存される場所の「完全修飾」位置を含むタグでビルドされたイメージにタグを付けることです。Docker Python API - タグ付けコンテナ

私はPython API(ドッカークライアントへのシェル呼び出しの代わりに)に持っていたスクリプトの移行に取り組んでいました。 https://docker-py.readthedocs.io/en/stable/images.htmlのAPIドキュメントで「ドッカータグ」と同等のものを見つけることができません。

誰かが正しい方向に向けることができますか?

+1

(ので、私のECSタスクは、デフォルトでは、常に最新の画像をつかむことができます)最新のが何であるかを追跡しながら、私は、各画像のバージョン番号を追跡できるように、ECRに二回の画像のタグ付けの使用を含んでいます低レベル 'API:' https:// docker-py.readthedocs.io/en/stable/api.html#module-docker.api.image' 'tag()'メソッドがあります。 – AChampion

答えて

1

AWSでECR/ECSを使用している場合は、ここをクリックしてください。ここで

aws ecr get-login --no-include-email --region us-west-2 
docker build -t myproj . 
docker tag calclab:latest XXXXXXXXXX.dkr.ecr.us-west-2.amazonaws.com/myproj:latest 
docker push XXXXXXXXXX.dkr.ecr.us-west-2.amazonaws.com/myproj:latest 

をドッカーのPython APIと後のBoto(AWSのPythonライブラリ)を使用して、ラフ同等です:

AmazonはあなたのイメージをプッシュするECRで、このような手順を説明します。これは、 `

import docker 
import boto3 

def ecrDemo(version_number): 

    # The ECR Repository URI 
    repo = XXXXXXXXXX.dkr.ecr.us-west-2.amazonaws.com/myproj 
    # The name of the [profile] stored in .aws/credentials 
    profile = "sandbox" 
    # The region your ECR repo is in 
    region = "us-west-2" 
    # How you want to tag your project locally 
    local_tag = "myproj" 

    #Set up a session 
    session = boto3.Session(profile_name=profile, region_name=region) 
    ecr = session.client('ecr') 

    docker_api = docker.APIClient() 

    print "Building image " + local_tag 
    for line in docker_api.build(path='.', tag=local_tag, stream=True, \ 
     dockerfile='./Dockerfile.myproj'): 
     process_docker_api_line(line) 

    # Make auth call and parse out results 
    auth = ecr.get_authorization_token() 
    token = auth["authorizationData"][0]["authorizationToken"] 
    username, password = b64decode(token).split(':') 
    endpoint = auth["authorizationData"][0]["proxyEndpoint"] 

    # print "Make authentication call" 
    # docker_api.login(username=user, password=password, \ 
    #    registry=endpoint, reauth=True) 
    auth_config_payload = {'username': username, 'password': password} 



    version_tag = repo + ':latest' 
    latest_tag = repo + ':' + version_number 

    print "Tagging version " + version_tag 
    if docker_api.tag(local_tag, version_tag) is False: 
     raise RuntimeError("Tag appeared to fail: " + version_tag) 

    print "Tagging latest " + latest_tag 
    if docker_api.tag(local_tag, latest_tag) is False: 
     raise RuntimeError("Tag appeared to fail: " + tag_latest) 

    print "Pushing to repo " + version_tag 
    for line in docker_api.push(version_tag, stream=True, auth_config=auth_config_payload): 
     self.process_docker_api_line(line) 

    print "Pushing to repo " + latest_tag 
    for line in docker_api.push(latest_tag, stream=True, auth_config=auth_config_payload): 
     self.process_docker_api_line(line) 

    print "Removing taged deployment images" 
    # You will still have the local_tag image if you need to troubleshoot 
    docker_api.remove_image(version_tag, force=True) 
    docker_api.remove_image(latest_tag, force=True) 

def process_docker_api_line(payload): 
    """ Process the output from API stream, throw an Exception if there is an error """ 
    # Sometimes Docker sends to "{}\n" blocks together... 
    for segment in payload.split('\n'): 
     line = segment.strip() 
     if line: 
      try: 
       line_payload = json.loads(line) 
      except ValueError as ex: 
       print "Could not decipher payload from API: " + ex.message 
      if line_payload: 
       if "errorDetail" in line_payload: 
        error = line_payload["errorDetail"] 
        sys.stderr.write(error["message"]) 
        raise RuntimeError("Error on build - code " + `error["code"]`) 
       elif "stream" in line_payload: 
        sys.stdout.write(line_payload["stream"]) 
+0

これは素晴らしいことですが、私は質問を投稿しようとしていましたが(それが必要な場合は実行します)、APIを使用してAWS ECRから画像をプル/プッシュする必要があるため、従来のドッカーログインを使用できませんここに掲載された方法を使用してください。 あなたの偉大な、またはコマンドやAPIの名前だけのボックスの外の例がある場合は、私にトラブルの世界を節約します。 –

+0

こんにちは、上の関数がビルドを含み、プロジェクトのレポや資格情報を含んでいないこと以外、どのような追加情報が必要でしたか? – Jason

+0

ヘイ・ジェイソン(Hey Jason)は素晴らしいと言いましたが、私には大きな前進となりました(この明日の作業を開始します)。私はAWS ECRからイメージを引き出し、タグを付け直して別のECR別の地域またはアカウント)。 私は上記のコードからpullコマンドがないと思います –

1

これは、画像の作成とタグ付けに使用できる手順です。

import docker 
tag = 'latest' # or whatever you want 
client = docker.from_env() 
# identifier of the image on your system 
dockername = "%s:%s" % (<name of the image on your system>, <tag>) 
# the target identifier 
target = "%s:%d/%s" % (<registry address>, <registry_port>, <id or name of the image>) 

# the url is usually unix://var/run/docker.sock' but depends on your environment 
cli = docker.APIClient(base_url=<the daemon\'s url or socket>) 
# build the image 
cli.build(path=..., tag=dockername, pull=..., buildargs=...) 
# tag it 
image = client.images.get(dockername) 
image.tag(target, tag=tag) 
+1

ありがとう!確かに、低レベルのAPIはこのライブラリと一緒に行く方法です。ビルドのようなものの進捗状況を監視できるという利点も得られます。 – Jason

関連する問題