AWS CodeBuild による Docker イメージの作成

2024年2月3日

はじめに

AWS CodeBuild で Docker イメージを作成して Amazon ECR に上げるテスト。

S3

S3 バケットを用意する。代わりに CodeCommit でもよい。バケット名は "codebuild-docker-test-なんとか" とする。

下記のファイルを用意。

buildspec.yml

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG

Dockerfile

FROM golang:1.12-alpine AS build
#Install git
RUN apk add --no-cache git
#Get the hello world package from a GitHub repository
RUN go get github.com/golang/example/hello
WORKDIR /go/src/github.com/golang/example/hello
# Build the project and send the output to /bin/HelloWorld 
RUN go build -o /bin/HelloWorld

FROM golang:1.12-alpine
#Copy the build's output binary from the previous build container
COPY --from=build /bin/HelloWorld /bin/HelloWorld
ENTRYPOINT ["/bin/HelloWorld"]

これらのファイルを直接含む zip ファイルを作成 (ここでは Ubuntu で作業)。

$ zip build-docker buildspec.yml Dockerfile

zip ファイルをバケットにアップロードする。

ECR

ECR でリポジトリを作る。リポジトリ名は "codebuild-docker-test" にする。

エンドポイントの作成

VPC

  • [エンドポイント] の [エンドポイントを作成]。
  • ecr で検索し、"com.amazonaws.ap-northeast-1.ecr.api" を選択。
  • VPC を選択。適切なサブネット (プライベートサブネット)、セキュリティグループを設定。
  • [エンドポイントを作成]。

以下も同様に作成。

  • "com.amazonaws.ap-northeast-1.ecr.dkr"
  • "com.amazonaws.ap-northeast-1.logs"
  • "com.amazonaws.ap-northeast-1.s3" の Gateway 型

CodeBuild

CodeBuild でビルドプロジェクトを作成する。

  • プロジェクト名: build-docker-test
  • ソースプロバイダ: Amazon S3、パケットを指定。S3 オブジェクトキー: build-docker.zip
  • 環境イメージ: マネージド型イメージ
  • OS: Ubuntu
  • ランタイム: Standard
  • イメージ: aws/codebuild/standard:5.0
  • 特権付与: ON
  • サービスロール: 新しいサービスロール、ロール名: codebuild-service-role
  • 環境変数
    • AWS_DEFAULT_REGION: リージョン "ap-northeast-1"
    • AWS_ACCOUNT_ID: アカウントID (ECR の リポジトリの URI を見るとわかる)
    • IMAGE_REPO_NAME: リポジトリ名 "codebuild-docker-test"
    • IMAGE_TAG: "latest"

[ビルドプロジェクトを作成する]。

作成されたロール (ここでは "codebuild-service-role") にポリシーを追加する。

{
  "Version": "2012-10-17",
  "Statement": [
    ...
    {
      "Action": [
        "ecr:BatchCheckLayerAvailability",
        "ecr:CompleteLayerUpload",
        "ecr:GetAuthorizationToken",
        "ecr:InitiateLayerUpload",
        "ecr:PutImage",
        "ecr:UploadLayerPart"
      ],
      "Resource": "*",
      "Effect": "Allow"
    }
  ]
}

KMS を使っている場合は、その設定も追加する。

ビルドプロジェクト build-docker-test を選んで、[ビルド開始]。

ステータスが「成功」になって、ECR のリポジトリにイメージが入っていたら OK。

参考