プログラミングと旅と映画の日々

普段はスマホ決済サービスの会社でバッグエンドを担当しているエンジニアです。プログラミングと趣味の映画、株、時々うどんに関してブログを書いていこうと思います。海外ドラマ、クロスバイクも好きです。

【Docker】AWSにDocker hostを作成する方法【入門】

AWSにDocker hostを作成する方法

AWSにDocker hostを作成し、管理する方法についてみていきます!

AWSを操作するため、AWSの認証情報を取得

まずはAWSのウェブ画面にログインします。

左上のサービス>セキュリティ、 アイデンティティコンプライアンス>I AM
をクリックします。
f:id:takanori5:20181125130117p:plain
左のメニューからユーザーを選択
f:id:takanori5:20181125125925p:plain
ユーザーを追加をクリック
f:id:takanori5:20181125130200p:plain
ここで任意のユーザー名を入れ、プログラムによるアクセスにチェックを入れます!
そして次のステップへ!!
f:id:takanori5:20181125130342p:plain
既存のポリシーを直接アタッチをクリック!
Administrator Accessをチェックして
次のステップへをクリック!
f:id:takanori5:20181125130524p:plain
確認して問題なければそのままユーザーを作成をクリックしましょう!
f:id:takanori5:20181125130701p:plain
ユーザーが作成できました!
f:id:takanori5:20181125130759p:plain
アクセスキーはAWSにアクセスする際の鍵となります。
それではこれらのキーをPCに設定しましょう!

AWSの認証情報をPCに設定

キー情報はdocker-machineコマンドでも設定できます。
ただ、毎回入力するのはめんどいのでhomeディレクトリ配下にAWSディレクトリを用意してここに設定を入れておきます。

$ mkdir ~/.aws
$ vim ~/.aws/credencial
$ cat ~/.aws/credencial
[default]
aws_access_key_id = XXX
aws_secret_access_key = XXX

XXXは先ほどのAWSの画面上に表示された値を入れましょう!

AWS EC2インスタンスをdocker-machineで作成

まずはウェブUI上でインスタンスが存在していないことを確認します。
f:id:takanori5:20181125132015p:plain
ではコマンドからEC2インスタンスを作成してみます!

$ docker-machine create --driver amazonec2 --amazonec2-open-port 8000 --amazonec2-region ap-northeast-1 aws-sandbox

起動したインスタンスの8000ポートを外部に公開します。
regionには東京リージョンを指定し
hostにはaws-sandboxという名前を指定しています。
では実行します。

$ docker-machine create --driver amazonec2 --amazonec2-open-port 8000 --amazonec2-region ap-northeast-1 aws-sandbox
Running pre-create checks...
Creating machine...
(aws-sandbox) Launching instance...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(systemd)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env aws-sandbox

インスタンスが作成されました!
ではコンテナを起動してみましょう
まず操作対象のhostを指定します。
docker-machineで起動したhostを指定するときと同じで、以下のコマンドでhostを指定します。

$ eval $(docker-machine env aws-sandbox)

では、docker run コマンドでコンテナを起動します。
hello-worldコンテナでやってみます。

$ docker run -d -p 8000:80 --name webserver kitematic/hello-world-nginx
Unable to find image 'kitematic/hello-world-nginx:latest' locally
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
e6def26304053e18a4db9eb8c41343b33281264960d6ea9b72b9fdeec5f1ad94

それではipを確認して、ブラウザからみてみましょう!

 docker-machine ip aws-sandbox
13.231.242.178

ブラウザから確認すると正常にコンテナが起動していますね!
f:id:takanori5:20181125133446p:plain
コンテナを止めちゃいましょう

$ docker-machine stop aws-sandbox
Stopping "aws-sandbox"...


Machine "aws-sandbox" was stopped.

最後にこのコンテナを削除します!

$ docker-machine rm aws-sandbox
About to remove aws-sandbox
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed aws-sandbox