Chapter 06

Docker Image作成

ShigeruOda
ShigeruOda
2022.10.25に更新

■なぜ今コンテナなのか

参考
202106 AWS Black Belt Online Seminar CON110 なぜ今コンテナなのか

■Cloud9上にdockerセットアップされていることを確認

cmd

docker -v

result

Docker version 20.10.13, build a224086

■Cloud9上にDockerfileを作成

cmd

cd ~/environment
cat << EOF > Dockerfile
FROM php:7.4.0-apache
COPY src/ /var/www/html/
EOF
mkdir src
cat << EOF > ./src/index.php
<!DOCTYPE html>
<html lang="ja">
  <head>
    <title>Hello! Jaws Days 2022!!</title>
  </head>
  <body>
    <p>Hello! Jaws Days 2022!!</p>
    <?php echo gethostname(); ?>
  </body>
</html>
EOF
ls -l ./Dockerfile ./src/index.php

result

-rw-rw-r-- 1 ec2-user ec2-user  47 Sep 15 12:01 ./Dockerfile
-rw-rw-r-- 1 ec2-user ec2-user 190 Sep 15 12:01 ./src/index.php

■Cloud9上でDocker イメージを構築

cmd

docker build \
  -t jaws-days-2022/container-hands-on .

result

Sending build context to Docker daemon  9.822MB
Step 1/2 : FROM php:7.4.0-apache
7.4.0-apache: Pulling from library/php
000eee12ec04: Pull complete 
8ae4f9fcfeea: Pull complete 
60f22fbbd07a: Pull complete 
ccc7a63ad75f: Pull complete 
a2427b8dd6e7: Pull complete 
91cac3b30184: Pull complete 
d6e40015fc10: Pull complete 
9858aa646efe: Pull complete 
7940985f7eb2: Pull complete 
b23f72eebcfb: Pull complete 
75bb7b8d192c: Pull complete 
7edf943992b0: Pull complete 
c8bf9d9d0e11: Pull complete 
Digest: sha256:686af696a87d3836c694380588368ff4e7ad3e30f1faef387c545890b340edee
Status: Downloaded newer image for php:7.4.0-apache
 ---> bf262c8621c1
Step 2/2 : COPY src/ /var/www/html/
 ---> 31879637952a
Successfully built 31879637952a
Successfully tagged jaws-days-2022/container-hands-on:latest

■Cloud9上でDocker イメージを構築されたことを確認

cmd

docker images \
  --filter reference= jaws-days-2022/container-hands-on:latest

result

REPOSITORY                          TAG       IMAGE ID       CREATED          SIZE
jaws-days-2022/container-hands-on   latest    31879637952a   47 seconds ago   414MB

■Cloud9上でDocker イメージを起動

cmd

docker run \
  --name container-hands-on \
  -d -p 8080:80 jaws-days-2022/container-hands-on:latest

result

90d092942d12681bcf38fe41e72e56bec3b241be4560e12365169c6a773b809c

画面

img

  • Cloud9のヘッダ部分のPreview-> Preview Runnnig Applicationのボタン押下
  • Hello! Jaws Days 2022!!と記載された画面が表示されること

Positive
: 作業ミス等によりコンテナを止めたい場合には以下を実行ください

docker stop $(docker ps -q)
docker rm $(docker ps -q -a)

■Docker ImageにTag付けを行う

cmd

docker tag \
  jaws-days-2022/container-hands-on:latest `echo ${AccountID}`.dkr.ecr.ap-northeast-1.amazonaws.com/jaws-days-2022/container-hands-on:latest

result

(なし)

■Docker ImageにTag付けの確認

cmd

docker images \
  --filter reference=`echo ${AccountID}`.dkr.ecr.ap-northeast-1.amazonaws.com/jaws-days-2022/container-hands-on:latest

result

REPOSITORY                                                                            TAG       IMAGE ID       CREATED              SIZE
123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/jaws-days-2022/container-hands-on   latest    31879637952a   About a minute ago   414MB

■認証トークンを取得し、レジストリに対して Docker クライアントを認証します

cmd

aws ecr get-login-password \
  --region ap-northeast-1 | \
  docker login \
  --username AWS \
  --password-stdin `echo ${AccountID}`.dkr.ecr.ap-northeast-1.amazonaws.com

result

WARNING! Your password will be stored unencrypted in /home/ec2-user/.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 ImageをECRにPush

  • Cloud9で作成したイメージをECRに格納します

img

cmd

docker push \
  ${AccountID}.dkr.ecr.ap-northeast-1.amazonaws.com/jaws-days-2022/container-hands-on:latest

result

The push refers to repository [123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/jaws-days-2022/container-hands-on]
3d025b51ca1a: Pushed 
536365481ed8: Pushed 
4b3693c51878: Pushed 
677c3ce9f0b4: Pushed 
c08f4d9c281b: Pushed 
ed13170590f7: Pushed 
37cbdda31557: Pushed 
9691e5d7a4c7: Pushed 
6a4d393f0795: Pushed 
e38834ac7561: Pushed 
ec64f555d498: Pushed 
840f3f414cf6: Pushed 
17fce12edef0: Pushed 
831c5620387f: Pushed 
latest: digest: sha256:385537de56b405bf07d5218a21eb9160bf023021713bbe05582cd9a2d7365ce5 size: 3242