タスク定義に2つコンテナを追加して片方だけpushしてサービスアップデートしたときの挙動

##目的
タスク定義に2つのコンテナイメージを登録して、片方のイメージだけ更新して、サービス更新した時の動きを確認する

##結論
両方とも更新される
そもそもイメージ更新してなくても再デプロイされるから当たり前かですな。。

##事前の状態

###ECSクラスタのEC2にログインしてdocker psを実行した結果

```
$ docker ps
CONTAINER ID        IMAGE                                                         COMMAND                  CREATED             STATUS              PORTS                     NAMES
5d66e321abf7        *******.dkr.ecr.ap-northeast-1.amazonaws.com/my-nodejs   "node /src/bin/www &"    43 seconds ago      Up 41 seconds       0.0.0.0:32771->3000/tcp   ecs-p-test-node-ec2-12-my-nodejs-a0c3fdc992f6b3f47900
096f927e65a8        ******.dkr.ecr.ap-northeast-1.amazonaws.com/xray        "/usr/bin/xray -b 0.…"   43 seconds ago      Up 42 seconds       0.0.0.0:32771->2000/udp   ecs-p-test-node-ec2-12-xray-deamon-fcaed0fd81cb81d0cd01
b32773c2cb96        amazon/amazon-ecs-agent:latest                                "/agent"                 10 minutes ago      Up 10 minutes                                 ecs-agent
```

###タスク定義

```
      "image": "******.dkr.ecr.ap-northeast-1.amazonaws.com/xray",
      "image": "******.dkr.ecr.ap-northeast-1.amazonaws.com/my-nodejs",
```

###ECR

my-nodejsを更新する
![image.png](https://qiita-image-store.s3.amazonaws.com/0/78184/79ac50f8-d33a-c666-7191-003f6d8557a1.png)

現状は、2018/11/11/22:14のイメージが最新
![image.png](https://qiita-image-store.s3.amazonaws.com/0/78184/19fdcb82-f470-13b1-a085-54de9c3c1ee6.png)

##手順

###my-nodejsのPush

```
$(aws ecr get-login --no-include-email --region ap-northeast-1)
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /Users/atsum/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

$ docker build -t my-nodejs .
Sending build context to Docker daemon  53.87MB
Step 1/6 : FROM node
 ---> 4e4c445311e6
Step 2/6 : RUN npm config set strict-ssl false;cd /src; npm install express;npm install aws-sdk;npm install aws-xray-sdk
 ---> Using cache
 ---> e1119dabe9de
Step 3/6 : COPY ./sampleapp /src
 ---> Using cache
 ---> dca5910601b9
Step 4/6 : RUN touch a.txt
 ---> Running in d21560d6bf43
Removing intermediate container d21560d6bf43
 ---> 3b2e08fee0a8
Step 5/6 : EXPOSE  3000
 ---> Running in 27d0f44837b2
Removing intermediate container 27d0f44837b2
 ---> f0ad9689a5d6
Step 6/6 : CMD ["node", "/src/bin/www", "&"]
 ---> Running in e9a308a9f0d2
Removing intermediate container e9a308a9f0d2
 ---> ba1fb6350c96
Successfully built ba1fb6350c96
Successfully tagged my-nodejs:latest

$ docker tag my-nodejs:latest ******.dkr.ecr.ap-northeast-1.amazonaws.com/my-nodejs:latest

$ docker push ******.dkr.ecr.ap-northeast-1.amazonaws.com/my-nodejs:latest
The push refers to repository [******.dkr.ecr.ap-northeast-1.amazonaws.com/my-nodejs]
f47635bb058a: Pushed 
8ff354860706: Layer already exists 
0fa739427d72: Layer already exists 
91072b192927: Layer already exists 
a6611280a43a: Layer already exists 
c4d021050ecd: Layer already exists 
9978d084fd77: Layer already exists 
1191b3f5862a: Layer already exists 
08a01612ffca: Layer already exists 
8bb25f9cdc41: Layer already exists 
f715ed19c28b: Layer already exists 
latest: digest: sha256:4eb1b6e76da52b6f078f7098922d5893a00ca701f141d66d47dc4a116b7ac171 size: 2637
```

イメージが更新された

![image.png](https://qiita-image-store.s3.amazonaws.com/0/78184/d5c1d52c-abcf-27fa-fdd7-9c6680de3a38.png)

###サービスを更新する

サービスを選択して更新をクリック
![image.png](https://qiita-image-store.s3.amazonaws.com/0/78184/ebbb5620-1671-dcce-479c-acf62ac8904e.png)

リビジョンに最新を指定して新しいデプロイの強制のチェックボックスをクリックしてサービスの更新を実施

![image.png](https://qiita-image-store.s3.amazonaws.com/0/78184/351172ce-d1e6-82ee-9840-19b2aa7b86e7.png)

新しいタスクが起動する

![image.png](https://qiita-image-store.s3.amazonaws.com/0/78184/1a61f6bb-d2bf-f093-dde0-74ea75a77a7f.png)

###確認

新しいタスク定義でタスクが起動する

```
$ docker ps
CONTAINER ID        IMAGE                                                         COMMAND                  CREATED              STATUS              PORTS                     NAMES
537811dea3fd        ******.dkr.ecr.ap-northeast-1.amazonaws.com/my-nodejs   "node /src/bin/www &"    About a minute ago   Up About a minute   0.0.0.0:32772->3000/tcp   ecs-p-test-node-ec2-12-my-nodejs-b8dcc0b588d5e0e67600
7baf08f94a2e        ******.dkr.ecr.ap-northeast-1.amazonaws.com/xray        "/usr/bin/xray -b 0.…"   About a minute ago   Up About a minute   0.0.0.0:32772->2000/udp   ecs-p-test-node-ec2-12-xray-deamon-9ab1b4b2beb8fd919701
5d66e321abf7        b13bb187ade5                                                  "node /src/bin/www &"    25 minutes ago       Up 25 minutes       0.0.0.0:32771->3000/tcp   ecs-p-test-node-ec2-12-my-nodejs-a0c3fdc992f6b3f47900
096f927e65a8        ******.dkr.ecr.ap-northeast-1.amazonaws.com/xray        "/usr/bin/xray -b 0.…"   25 minutes ago       Up 25 minutes       0.0.0.0:32771->2000/udp   ecs-p-test-node-ec2-12-xray-deamon-fcaed0fd81cb81d0cd01
b32773c2cb96        amazon/amazon-ecs-agent:latest                                "/agent"                 35 minutes ago       Up 35 minutes                                 ecs-agent
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/78184/619a75f1-506e-be47-c804-6dd095cbdf49.png)

しばらくすると古いタスク定義のタスクが停止する

```
[ec2-user@ip-172-31-14-134 ~]$ docker ps
CONTAINER ID        IMAGE                                                         COMMAND                  CREATED             STATUS              PORTS                     NAMES
537811dea3fd        ******.dkr.ecr.ap-northeast-1.amazonaws.com/my-nodejs   "node /src/bin/www &"    7 minutes ago       Up 7 minutes        0.0.0.0:32772->3000/tcp   ecs-p-test-node-ec2-12-my-nodejs-b8dcc0b588d5e0e67600
7baf08f94a2e        ******.dkr.ecr.ap-northeast-1.amazonaws.com/xray        "/usr/bin/xray -b 0.…"   7 minutes ago       Up 7 minutes        0.0.0.0:32772->2000/udp   ecs-p-test-node-ec2-12-xray-deamon-9ab1b4b2beb8fd919701
b32773c2cb96        amazon/amazon-ecs-agent:latest                                "/agent"                 41 minutes ago      Up 41 minutes                                 ecs-agent
```

##約束事項
投稿内容は私個人の意見であり、所属企業・部門見解を代表するものではありません。