【備忘録】ECRにDockerイメージを登録する(チュートリアル編)

## ECRにDockerイメージを登録する。
表題通り、チュートリアルを実装して行きます。

### ①ECRにリポジトリを作成する
![スクリーンショット 2022-06-14 20.53.12.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/3a710c43-9636-6307-c941-6c0b45b1472c.png)
→これでリポジトリが完成します！！では、次にDockerイメージを作成します。

### ②Dokcerイメージを作成する
```Dockerfile:
FROM golang:1.17-alpine

WORKDIR /go/src/app

RUN apk update && apk add git

COPY main.go .

CMD go run main.go
```

```go:main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello Docker")
}
```

```shell:Dockerイメージを作成
~/develop/go_hello_docker $ docker build -t go_hello_docker .                                                                         
[+] Building 32.3s (10/10) FINISHED                                                                                                                                                                      
 => [internal] load build definition from Dockerfile                                                                                                                                                0.0s
 => => transferring dockerfile: 153B                                                                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                                                                   0.0s
 => => transferring context: 2B                                                                                                                                                                     0.0s
 => [internal] load metadata for docker.io/library/golang:1.17-alpine                                                                                                                               3.5s
 => [auth] library/golang:pull token for registry-1.docker.io                                                                                                                                       0.0s
 => [1/4] FROM docker.io/library/golang:1.17-alpine@sha256:f4aeca00f75f73cbf9a66ee506a9e7f908dcf5a2aa4fa56e30665481aa55ac4d                                                                        22.7s
 => => resolve docker.io/library/golang:1.17-alpine@sha256:f4aeca00f75f73cbf9a66ee506a9e7f908dcf5a2aa4fa56e30665481aa55ac4d                                                                         0.0s
 => => sha256:aa14b58448d60a07bde054a6b95b26c837560dd5a80fe5e7308141ae557df7ef 109.86MB / 109.86MB                                                                                                 16.0s
 => => sha256:f4aeca00f75f73cbf9a66ee506a9e7f908dcf5a2aa4fa56e30665481aa55ac4d 1.65kB / 1.65kB                                                                                                      0.0s
 => => sha256:207aa29a040df934020cc55e4cd37eddcc54f646685bba09062eb3236dfc0ed0 1.36kB / 1.36kB                                                                                                      0.0s
 => => sha256:f8b62f12247befb7fcbce03596aa95972ce132c40291c08dd230092aea53de49 5.23kB / 5.23kB                                                                                                      0.0s
 => => sha256:ea60b727a1ce729d031ec1e4f2521a246e71bdb72d3fd9c9c04711cce80e1722 271.82kB / 271.82kB                                                                                                  0.3s
 => => sha256:30c4a77219572e69864cd3069b7b47e55202681be151bd7394f9cb6645b5687b 153B / 153B                                                                                                          0.6s
 => => extracting sha256:ea60b727a1ce729d031ec1e4f2521a246e71bdb72d3fd9c9c04711cce80e1722                                                                                                           0.2s
 => => sha256:53460cecce9419dcccde94b58a8fa47fa84f56a1f35cf2737c8651977ace26e0 156B / 156B                                                                                                          1.2s
 => => extracting sha256:30c4a77219572e69864cd3069b7b47e55202681be151bd7394f9cb6645b5687b                                                                                                           0.0s
 => => extracting sha256:aa14b58448d60a07bde054a6b95b26c837560dd5a80fe5e7308141ae557df7ef                                                                                                           6.3s
 => => extracting sha256:53460cecce9419dcccde94b58a8fa47fa84f56a1f35cf2737c8651977ace26e0                                                                                                           0.0s
 => [internal] load build context                                                                                                                                                                   0.0s
 => => transferring context: 106B                                                                                                                                                                   0.0s
 => [2/4] WORKDIR /go/src/app                                                                                                                                                                       1.2s
 => [3/4] RUN apk update && apk add git                                                                                                                                                             4.5s
 => [4/4] COPY main.go .                                                                                                                                                                            0.0s 
 => exporting to image                                                                                                                                                                              0.2s 
 => => exporting layers                                                                                                                                                                             0.2s 
 => => writing image sha256:23556ea1fd7350043e4d49693b5b03370c08ad14bf858fb515d6c44053e3f67e                                                                                                        0.0s 
 => => naming to docker.io/library/go_hello_docker                                                                                                                                                  0.0s 
                                                                                                                                                                                                         
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
```

```shell:イメージが作成されている事を確認
~/develop/go_hello_docker $ docker images go_hello_docker    
REPOSITORY        TAG       IMAGE ID       CREATED              SIZE
go_hello_docker   latest    23556ea1fd73   About a minute ago   330MB
```

### ③ECRにDockerイメージをPushする
```shell:AWS-CLIでログインする
~/develop/go_hello_docker $aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin XXXX.dkr.ecr.ap-northeast-1.amazonaws.com
Login Succeeded

Logging in with your password grants your terminal complete access to your account. 
For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/
```

```shell:tag付する
~/develop/go_hello_docker $ docker tag go_hello_docker:latest XXXX.dkr.ecr.ap-northeast-1.amazonaws.com/go_hello_docker:latest                 
```

```shell:ECRにイメージをPushする
~/develop/go_hello_docker $ docker push XXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/go_hello_docker:latest
The push refers to repository [XXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/go_hello_docker]
58729872d1af: Pushed 
6cbd35ec4e52: Pushed 
d2fbd33f8ab1: Pushed 
37efa7de465e: Pushed 
33c7faab0998: Pushed 
9688cd62670f: Pushed 
79f02b258438: Pushed 
24302eb7d908: Pushed 
latest: digest: sha256:0b82ac5d2b7183f3cfe25420dd06db784f30c443d2a3d544cc880c74eb32b365 size: 1990
```

### 実行結果の確認
![スクリーンショット 2022-06-14 21.10.52.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/1cc8cb7f-3b26-26a0-a0d4-d174e70290e6.png)
→ECRにDockerイメージが作成されている事を確認。