網頁

2023/9/25

AWS 建立Go ECR image

建立一個簡單的Go網路應用程式,建置並部署為AWS ECR image。



事前要求

參考「Mac Homebrew 安裝 AWS CLI」安裝AWS CLI。

參考「AWS ECR 建立private repository」建立ECR private repositroy,名稱為ecr-1

參考「Golang 建立網頁伺服器 Web Server」建立Go網路應用程式。

參考「Go 建立docker image」在應用程式專案根目錄下建立Dockerfile(不需要build image)。


Build image

在專案根目錄下以命令列執行docker build --platform linux/amd64 -t go-image:test .來使用Dockerfile來建立image,名稱為go-image

~/../go-demo% docker build --platform linux/amd64 -t go-image:test .
[+] Building 20.0s (12/12) FINISHED                                                                                                                                                                                                                        docker:desktop-linux
 =>     [internal] load .dockerignore                                                                                                                                                                                                                                          0.0s
 =>     =>     transferring context: 2B                                                                                                                                                                                                                                            0.0s
 =>     [internal] load build definition from Dockerfile                                                                                                                                                                                                                       0.0s
 =>     =>     transferring dockerfile: 193B                                                                                                                                                                                                                                       0.0s
 =>     resolve image config for docker.io/docker/dockerfile:1                                                                                                                                                                                                                 3.9s
 =>     docker-image://docker.io/docker/dockerfile:1@sha256:ac85f380a63b13dfcefa89046420e1781752bab202122f8f50032edf31be0021                                                                                                                                                   0.8s
 =>     =>     resolve docker.io/docker/dockerfile:1@sha256:ac85f380a63b13dfcefa89046420e1781752bab202122f8f50032edf31be0021                                                                                                                                                       0.0s
 =>     =>     sha256:ac85f380a63b13dfcefa89046420e1781752bab202122f8f50032edf31be0021 8.40kB / 8.40kB                                                                                                                                                                             0.0s
 =>     =>     sha256:e929b0d024894103bb3a8577aad825a7df006a8f6767747bffd373d804c3ee67 482B / 482B                                                                                                                                                                                 0.0s
 =>     =>     sha256:19c37871da0fa7da4b2a871455ccea62d3b08eb94a7f6f0cf310fe02f14f5089 1.27kB / 1.27kB                                                                                                                                                                             0.0s
 =>     =>     sha256:cf80d9b4bd1c75ee551ce5ff6f950a178fbb62e661c02e42d41b8c772d1efc1d 11.02MB / 11.02MB                                                                                                                                                                           0.6s
 =>     =>     extracting sha256:cf80d9b4bd1c75ee551ce5ff6f950a178fbb62e661c02e42d41b8c772d1efc1d                                                                                                                                                                                  0.1s
 =>     [internal] load metadata for docker.io/library/golang:1.19-alpine                                                                                                                                                                                                      2.2s
 =>     [1/5] FROM docker.io/library/golang:1.19-alpine@sha256:0ec0646e208ea58e5d29e558e39f2e59fccf39b7bda306cb53bbaff91919eca5                                                                                                                                                9.4s
...
 =>     [2/5] WORKDIR /app                                                                                                                                                                                                                                                     0.3s
 =>     [3/5] COPY ./* ./                                                                                                                                                                                                                                                      0.2s
 =>     [4/5] RUN go mod download                                                                                                                                                                                                                                              0.3s
 =>     [5/5] RUN go build -o /go-demo                                                                                                                                                                                                                                         2.6s
 =>     exporting to image                                                                                                                                                                                                                                                     0.1s
 =>     =>     exporting layers                                                                                                                                                                                                                                                    0.1s
 =>     =>     writing image sha256:aefaee7951eca593bba06326da0bbba91f3067dd4e58ae59b7512c16a6f8c720                                                                                                                                                                               0.0s
 =>     =>     naming to docker.io/library/go-image:test

輸入docker images檢視建立的image如下。

~% docker images
REPOSITORY                                                TAG           IMAGE ID       CREATED        SIZE
go-image                                                  test          aefaee7951ec   2 hours ago    386MB


部署image到ECR repository

例如要部署的ECR private repository的URI為123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1

Docker登入ECR private repository,輸入aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com123456789012替換成你的AWS帳戶號碼,ap-northeast-1替換為ECR repository所在的region。

~% aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com
Login Succeeded

輸入docker tag go-image:test 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latestgo-demo:test image標籤為123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest

~% docker tag go-image:test 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest

輸入docker images檢視標記的image如下。

~% docker images
REPOSITORY                                                TAG           IMAGE ID       CREATED        SIZE
go-image                                                  test          aefaee7951ec   2 hours ago    386MB
123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1   latest        aefaee7951ec   2 hours ago    386MB

輸入docker push 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest標記的image推送到ECR repository。

~% docker push 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1:latest
The push refers to repository [123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/ecr-1]
01f113103f06: Pushed
69def86d3a04: Pushed
5d6068acadd0: Pushed
1c96cb736df1: Pushed
ffbff62e3514: Pushed
9bfd5e9c96bf: Pushed
f4285c491509: Pushed
4693057ce236: Pushed
latest: digest: sha256:2e4e39a03be72ea67a317a6e7159e33431069852e57300e55a7b77707a0ec9b5 size: 1993

測試

在AWS console,前往ECR repository檢視上傳的image。。




沒有留言:

張貼留言