LoginSignup
4
4

More than 5 years have passed since last update.

DigitalOceanにDocker MachineでCentOS7をプロビジョンする方法

Posted at

概要

DigitalOcenan 上に Docker Mahcine を使い、CentOS 7.2 のドロップレット(仮想マシン)と Docker Engine を自動プロビジョニングする方法。

事前準備

作成方法

$ export $do_token='自分のAPI_token'
$ docker-machine create \
    --driver digitalocean \
    --digitalocean-access-token $do_token  \
    --digitalocean-region sgp1 \
    --digitalocean-size 2gb \
    --digitalocean-image centos-7-0-x64 \
     centos7-docker

オプションは、上から

  • --driver … DigitalOcean 用の Docker Machine ドライバを使う( 必須 )
  • --digitalocean-access-token … API トークンの指定 ( 必須)
  • --digitalocean-region … リージョンを sgp1 (シンガポール1)
  • --digitalocean-size … ドロップレットのサイズを 2gb
  • --digitalocean-image … 仮想マシン・イメージを CentOS 7.2 にするため centos-7-0-x64 を指定
  • ホスト名を centos7-docker に(ホスト名の指定は 必須

ここで重要なのは CentOS 7.2 x86 イメージを使いたい場合は、イメージに centos-7-0-x64 (7.2ではない)を指定する必要がある。

コマンドを実行すると、次のように仮想マシンが作成される。

Running pre-create checks...
Creating machine...
(centos7-docker) Creating SSH key...
(centos7-docker) Creating Digital Ocean droplet...
(centos7-docker) Waiting for IP address to be assigned to the Droplet...
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 centos...
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: (snip)

エラーが出る場合は、エラー内容を確認すること。なお、作成したホストは docker-machine ls コマンドで確認できる。

$ docker-machine ls
NAME             ACTIVE   DRIVER         STATE     URL                          SWARM   DOCKER    ERRORS
centos7-docker   -        digitalocean   Running   tcp://<ip>:2376             v1.10.3

プロビジョニングの確認

$ docker-machine ssh centos7-docker
Last login: Thu Mar 31 07:17:47 2016 from xxx
[root@centos7-docker ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@centos7-docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

CentOS 7.2 のインストール、および docker コマンドが使える(自動的にプロビジョンされた)ことがわかる。

補足

なぜ Qiita に書こうと思ったのか

  • イメージは CenOS 7.2 なのに API でアクセスする時は centos-7-0-x64 で指定しないといけないという、罠があったため。

イメージの調べ方

$ curl -H "Authorization: Bearer $do_token" \
    -X GET "https://api.digitalocean.com/v2/images" | jq "."

API でイメージの情報を取得した結果から、 CentOS 7.2 x64 イメージを使うには、"slug": "centos-7-0-x64" の指定を使う必要がある。

{
  "images": [
    {
(省略)
    {
      "id": 16040476,
      "name": "7.2 x64",
      "distribution": "CentOS",
      "slug": "centos-7-0-x64",
      "public": true,
      "regions": [
        "nyc1",
        "sfo1",
        "nyc2",
        "ams2",
        "sgp1",
        "lon1",
        "nyc3",
        "ams3",
        "fra1",
        "tor1"
      ],
      "created_at": "2016-02-29T21:17:07Z",
      "min_disk_size": 20,
      "type": "snapshot"
    },
(省略)

参考情報

4
4
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
4
4