30
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker Machine を使って EC2 に Dockerホストを立てる

Last updated at Posted at 2015-02-28

概要

Docker公式オーケストレーションツールの 1 つ Docker Machine を使って AWS に Docker がインストールされた EC2 インスタンスを作成し、hello world を出力するコンテナを走らせてみるという話

必要なもの

  • AWS のアカウント

Hello World

docker-machine create で Docker ホストを作る。(ドライバーオプションは適宜変更)

$ docker-machine create \
  --driver amazonec2 \
  --amazonec2-access-key XXXXXXXXXXXXXXXXXXXXX \
  --amazonec2-secret-key xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
  --amazonec2-ami ami-20b6aa21 \
  --amazonec2-instance-type t2.micro \
  --amazonec2-region ap-northeast-1 \
  --amazonec2-root-size 16 \
  --amazonec2-security-group docker-machine \
  --amazonec2-vpc-id vpc-XXXXXXXX \
  --amazonec2-subnet-id subnet-XXXXXXX \
  --amazonec2-zone c \
  production-03

INFO[0000] Launching instance...
INFO[0021] Waiting for SSH on XX.XX.XXX.XX:22
INFO[0066] Configuring Machine...
INFO[0263] "production-03" has been created and is now the active machine.
INFO[0263] To point your Docker client at it, run this in your shell: $(docker-machine env production-03)

マシンリストを見てみる

$ docker-machine ls
NAME            ACTIVE   DRIVER      STATE     URL                       SWARM
production-03   *        amazonec2   Running   tcp://XX.XX.XXX.XX:2376

docker ps

$ docker $(docker-machine config production-03) ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Hello World

$ docker $(docker-machine config production-03) run busybox echo hello world
Unable to find image 'busybox' locally
511136ea3c5a: Pull complete
df7546f9f060: Pull complete
ea13149945cb: Pull complete
4986bf8c1536: Pull complete
busybox:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.

Status: Downloaded newer image for busybox:latest
hello world
$ docker $(docker-machine config production-03) ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                      PORTS               NAMES
6617b37677e2        busybox:latest      "echo hello world"   12 seconds ago      Exited (0) 12 seconds ago                       pensive_payne

SSH ログイン

$ docker-machine ssh production-03
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-44-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sat Feb 28 05:10:04 UTC 2015

  System load:  0.27              Processes:              107
  Usage of /:   6.7% of 15.61GB   Users logged in:        0
  Memory usage: 9%                IP address for eth0:    10.0.13.85
  Swap usage:   0%                IP address for docker0: 172.17.42.1

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud


*** System restart required ***
ubuntu@production-03:~$

SSH 経由でのコマンド実行

$ docker-machine ssh production-03 "hostname && uname -a"
production-03
Linux production-03 3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

マシンのストップ

$ docker-machine stop production-03
$ docker-machine ls
NAME            ACTIVE   DRIVER      STATE      URL                       SWARM
production-03   *        amazonec2   Stopping   tcp://XX.XX.XXX.XX:2376

マシンのスタート

$ docker-machine start production-03
$ docker-machine ls
NAME            ACTIVE   DRIVER      STATE     URL                       SWARM
production-03   *        amazonec2   Running   tcp://XX.XX.XX.XX:2376

マシンの削除

$ docker-machine rm production-03
$ docker-machine ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM

エラーが出る

2015年02月27日現在は EC2 インスタンスの起動に失敗する事例が見られる。

さらに 1 度作成して消しても既にその keypair があると言われる。

ERRO[0000] Error creating machine: There is already a keypair with the name production.  Please either remove that keypair or use a different machine name.
WARN[0000] You will want to check the provider to make sure the machine and associated resources were properly removed.
FATA[0000] Error creating machine

どこが壊れてるのかフィードバックしつつ、とりあえず EC2 で動かしてみたい場合は以下の方法で回避できるかもしれない

debugモード

$ docker-machine --debug create

でどんなエラーが出ているか確認して問題を把握する。

ドライバーオプションをできるだけ指定する

公式ドキュメントに書かれた ap-northeast-1 デフォルトの AMI が古かったりする。そして AMI がハードコーディングされている
https://github.com/docker/machine/blob/c3e9c585406b4e0c08daf793afa53adcfcda6fde/drivers/amazonec2/utils.go#L20

できるだけ全部しておいた方が無難

REF

30
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?