Docker for Window で稼働しているローカルLinuxコンテナをAWSへ移動させてみた

  • 投稿者:
  • 投稿カテゴリー:docker

クライアント端末(Windows 10)必要なソフトをインストールする

Microsoft Windows で AWS Command Line Interface をインストールする
windows10へのdockerインストール
 記事を参考に「Kitematic(カイトマチック)」もインストールした。

Kitematic(カイトマチック)のイメージ



hello-world-nginxのコンテナをローカルで実行する

PS C:\Users\shimizu> docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (xxxx): xxxx
Password:
Login Succeeded

PS C:\Users\shimizu> docker search hello-world
NAME                                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   489                 [OK]
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   97
...

PS C:\Users\shimizu> docker pull kitematic/hello-world-nginx
Using default tag: latest
latest: Pulling from kitematic/hello-world-nginx
77c6c00e8b61: Pull complete
9b55a9cb10b3: Pull complete
e6cdd97ba74d: Pull complete
7fecf1e9de6b: Pull complete
6b75f22d7bea: Pull complete
e8e00fb8479f: Pull complete
69fad424364c: Pull complete
b3ba6e76b671: Pull complete
a956773dd508: Pull complete
26d2b0603932: Pull complete
3cdbb221209e: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:ec0ca6dcb034916784c988b4f2432716e2e92b995ac606e080c7a54b52b87066
Status: Downloaded newer image for kitematic/hello-world-nginx:latest

PS C:\Users\shimizu> docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
kitematic/hello-world-nginx   latest              03b4557ad7b9        2 years ago         7.91MB

PS C:\Users\shimizu> docker run --name mydocker -d -p 80:80 kitematic/hello-world-nginx
cfbb354fff9823393377d4e2dfe70cb7962d9732ae8cba089f2da07f934c8724

PS C:\Users\shimizu> docker ps -a
CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS              PORTS                NAMES
cfbb354fff98        kitematic/hello-world-nginx   "sh /start.sh"      34 seconds ago      Up 33 seconds       0.0.0.0:80->80/tcp   mydocker

PS C:\WINDOWS\system32> docker stop mydocker
mydocker

localhostにアクセスするとコンテナが動作していることがわかる。

AWSユーザを作成し、AWS CLIに認識させる

特定IPアドレスにadministrator権限を与えるIAMポリシーを作成し、ユーザに紐づける。
そのユーザでアクセスキーを作成し、AWS CLIに設定する。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AdministratorAccess",
      "Effect": "Allow",
      "Action": "*",
      "Resource": "*"
    },
    {
      "Sid": "SourceIPRestriction",
      "Effect": "Deny",
      "Action": "*",
      "Resource": "*",
      "Condition": {
        "NotIpAddress": {"aws:SourceIp": "x.x.x.x"}
      }
    }
  ]
}
PS C:\Users\shimizu> aws configure
AWS Access Key ID [None]: AKIAxxxxxxxx
AWS Secret Access Key [None]: 9Qexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json

Amazon Elastic Container Service (ECS)

ECSはEC2が必要であるが、Fargateを利用するとEC2の管理が不要となる。
Fargateは2018年4月現在東京リージョンにないためECSを利用する。

Elastic Container Registryにローカルのimageを登録する

ECRを作成する。

指示に従ってローカルのimageをECRへ登録する。

PS C:\WINDOWS\system32> aws ecr get-login --no-include-email --region ap-northeast-1
docker login -u AWS -p e...z https://377343199566.dkr.ecr.ap-northeast-1.amazonaws.com
PS C:\WINDOWS\system32> docker login -u AWS -p e...z https://377343199566.dkr.ecr.ap-northeast-1.amazonaws.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded

PS C:\WINDOWS\system32> docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
kitematic/hello-world-nginx   latest              03b4557ad7b9        2 years ago         7.91MB
PS C:\WINDOWS\system32> docker tag kitematic/hello-world-nginx 377343199566.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world-nginx
PS C:\WINDOWS\system32> docker images
REPOSITORY                                                            TAG                 IMAGE ID            CREATED             SIZE
377343199566.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world-nginx   latest              03b4557ad7b9        2 years ago         7.91MB
kitematic/hello-world-nginx                                           latest              03b4557ad7b9        2 years ago         7.91MB

PS C:\WINDOWS\system32> docker push 377343199566.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world-nginx:latest
The push refers to repository [377343199566.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world-nginx]
5f70bf18a086: Pushed
b51acdd3ef48: Pushed
3f47ff454588: Pushed
f19fb69b288a: Pushed
b11278aeb507: Pushed
fb85701f3991: Pushed
15235e629864: Pushed
86882fc1175f: Pushed
9e8c93c7ea7e: Pushed
e66f0ebc2eef: Pushed
6a15a6c08ef6: Pushed
461f75075df2: Pushed
latest: digest: sha256:583f0c9ca89415140fa80f70f8079f5138180a6dda2c3ff3920353b459e061a3 size: 3226

クラスターを作成する






タスク定義とクラスターへのタスク登録





EC2インスタンスにアクセスするとコンテナが動作していることがわかる。

参考URL

【初心者向け】Dockerを速習しよう
初心者か゛伝えるDocker超入門
AWS管理コンソールへのアクセスをIPアドレスで制限したい