LoginSignup
13
14

More than 5 years have passed since last update.

Docker Machineの操作 - コマンドオプションについて

Last updated at Posted at 2015-11-19

Docker Machineの操作にはdocker-machineコマンドを利用します。
そこで、公式ドキュメントを参考にしながら実際にコマンド操作を行ってみた内容についてまとめてみました。

環境のセットアップについてはDocker Toolboxで行いましたが、もし具体的な方法について知りたい方はこちらを参照ください。

| 作業環境

今回、確認をした際の環境です。

  • ローカル環境:Mac OS X El Capitan
  • Docker Machine:version 0.5.0 (04cfa58)

| オプション

|| --version, -v

docker-machineのバージョン情報を表示します。

コマンド
$ docker-machine -v
docker-machine version 0.5.0 (04cfa58)

|| --help, -h

docker-machineコマンドで利用出来るオプションやサブコマンドの説明を表示します。

コマンド
$ docker-machine --help
Usage: docker-machine [OPTIONS] COMMAND [arg...]
Create and manage machines running Docker.

Version: 0.5.0 (04cfa58)

Author:
  Docker Machine Contributors - <https://github.com/docker/machine>

Options:
  --debug, -D                       Enable debug mode
  -s, --storage-path "/Users/kohei/.docker/machine" Configures storage path [$MACHINE_STORAGE_PATH]
  --tls-ca-cert                     CA to verify remotes against [$MACHINE_TLS_CA_CERT]
  --tls-ca-key                      Private key to generate certificates [$MACHINE_TLS_CA_KEY]
  --tls-client-cert                     Client cert to use for TLS [$MACHINE_TLS_CLIENT_CERT]
  --tls-client-key                  Private key used in client TLS auth [$MACHINE_TLS_CLIENT_KEY]
  --github-api-token                    Token to use for requests to the Github API [$MACHINE_GITHUB_API_TOKEN]
  --native-ssh                      Use the native (Go-based) SSH implementation. [$MACHINE_NATIVE_SSH]
  --help, -h                        show help
  --version, -v                     print the version

Commands:
  active    Print which machine is active
  config    Print the connection config for machine
  create    Create a machine.

Run 'docker-machine create --driver name' to include the create flags for that driver in the help text.
  env           Display the commands to set up the environment for the Docker client
  inspect       Inspect information about a machine
  ip            Get the IP address of a machine
  kill          Kill a machine
  ls            List machines
  regenerate-certs  Regenerate TLS Certificates for a machine
  restart       Restart a machine
  rm            Remove a machine
  ssh           Log into or run a command on a machine with SSH.
  scp           Copy files between machines
  start         Start a machine
  status        Get the status of a machine
  stop          Stop a machine
  upgrade       Upgrade a machine to the latest version of Docker
  url           Get the URL of a machine
  help          Shows a list of commands or help for one command

Run 'docker-machine COMMAND --help' for more information on a command.

|| --debug, -D

デバッグモードを有効にします。

これはdocker-machineコマンドのサブコマンドと一緒に利用します。
試しに、createサブコマンドを利用した時の出力内容を確かめます。

まずは通常時(デバッグモード無効)の時の出力は以下です。

コマンド
$ docker-machine create --driver virtualbox default2
Running pre-create checks...
Creating machine...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Provisioning created instance...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
To see how to connect Docker to this machine, run: docker-machine env default2

次にデバッグモードを有効にした時の出力内容を見てみます。

コマンド
$ docker-machine -D create --driver virtualbox debug-test
Docker Machine Version: 0.5.0 (04cfa58)
Found binary path at /usr/local/bin/docker-machine-driver-virtualbox
Launching plugin server for driver virtualbox
Plugin server listening at address 127.0.0.1:50311
() Calling RpcServerDriver.GetVersion
Using API Version 1
〜〜 省略 〜〜
(debug-test) DBG | END SSH
(debug-test) DBG | 
Reticulating splines...
(debug-test) Calling RpcServerDriver.GetConfigRaw
(debug-test) Calling RpcServerDriver.GetConfigRaw
To see how to connect Docker to this machine, run: docker-machine env debug-test

1700行以上もの出力になってしまうため、途中を割愛させていただきますが、少し見るだけでも違いがわかると思います。
また、デバッグモードは出力行からも想像できるように、かなり多くの情報を出力します。
中には重要な情報もありますので、セキュリティの観点からも、必要な時のみ指定する方が良さそうです。

|| --tls-ca-cert

対象ホストと通信するための証明書を指定します。

コマンド
$ docker-machine --tls-ca-cert /Users/kohei/.docker_tmp/machine/certs/ca.pem create --driver virtualbox debug-test
Running pre-create checks...
Creating machine...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Provisioning created instance...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
To see how to connect Docker to this machine, run: docker-machine env debug-test

このオプションを指定すると、Docker Machine設定情報が記載されているconf.jsonの"CaCertPath"が変更されます。

オプション指定なし
$ docker-machine inspect -format=='{{.HostOptions.AuthOptions.CaCertPath}}' default
=/Users/kohei/.docker/machine/certs/ca.pem
オプション指定あり
$ docker-machine inspect -format=='{{.HostOptions.AuthOptions.CaCertPath}}' debug-test
=/Users/kohei/.docker_tmp/machine/certs/ca.pem

|| --tls-ca-key

証明書を生成するための秘密鍵を指定します。

コマンド
$ docker-machine --tls-ca-key /Users/kohei/.docker_tmp/machine/certs/ca-key.pem create --driver virtualbox debug-test
Running pre-create checks...
Creating machine...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Provisioning created instance...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
To see how to connect Docker to this machine, run: docker-machine env debug-test

このオプションを指定すると、Docker Machine設定情報が記載されているconf.jsonの"CaPrivateKeyPath"が変更されます。

オプション指定なし
$ docker-machine inspect -format=='{{.HostOptions.AuthOptions.CaPrivateKeyPath}}' default
=/Users/kohei/.docker/machine/certs/ca-key.pem
オプション指定あり
$ docker-machine inspect -format=='{{.HostOptions.AuthOptions.CaPrivateKeyPath}}' debug-test
=/Users/kohei/.docker_tmp/machine/certs/ca-key.pem

|| --tls-client-cert

TLS通信を行うためのクライアント証明書を指定します。

コマンド
$ docker-machine --tls-client-cert /Users/kohei/.docker_tmp/machine/certs/cert.pem create --driver virtualbox debug-test
Running pre-create checks...
Creating machine...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Provisioning created instance...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
To see how to connect Docker to this machine, run: docker-machine env debug-test

このオプションを指定すると、Docker Machine設定情報が記載されているconf.jsonの"ClientCertPath"が変更されます。

オプション指定なし
$ docker-machine inspect -format=='{{.HostOptions.AuthOptions.ClientCertPath}}' default
=/Users/kohei/.docker/machine/certs/cert.pem
オプション指定あり
$ docker-machine inspect -format=='{{.HostOptions.AuthOptions.ClientCertPath}}' debug-test
=/Users/kohei/.docker_tmp/machine/certs/cert.pem

|| --tls-client-key

TLS認証で利用される秘密鍵を指定します。

コマンド
$ docker-machine --tls-client-key /Users/kohei/.docker_tmp/machine/certs/key.pem create --driver virtualbox debug-test
Running pre-create checks...
Creating machine...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Provisioning created instance...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
To see how to connect Docker to this machine, run: docker-machine env debug-test

このオプションを指定すると、Docker Machine設定情報が記載されているconf.jsonの"ClientKeyPath"が変更されます。

オプション指定なし
$ docker-machine inspect -format=='{{.HostOptions.AuthOptions.ClientKeyPath}}' default
=/Users/kohei/.docker/machine/certs/key.pem
オプション指定あり
$ docker-machine inspect -format=='{{.HostOptions.AuthOptions.ClientKeyPath}}' debug-test
=/Users/kohei/.docker_tmp/machine/certs/key.pem

|| -s, --storage-path

Docker Machineを作成する場所を変更します。

コマンド
$ docker-machine -s /Users/kohei/.docker_tmp/machine create --driver virtualbox debug-test
Running pre-create checks...
Creating machine...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Provisioning created instance...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
To see how to connect Docker to this machine, run: docker-machine env debug-test

パス指定して作成した場合、デフォルトのパスではない場所となるため、docker-machineコマンドからは見えなくなります。
作成したDocker Machineに対してコマンドを実行したい場合は-sオプションを指定することで実行可能です。

試しにdocker-machine lsコマンドで試してみましょう。

オプション指定なし
$ docker-machine ls
NAME       ACTIVE   DRIVER       STATE     URL                         SWARM
default    -        virtualbox   Running   tcp://192.168.99.100:2376   
default2   -        virtualbox   Running   tcp://192.168.99.101:2376 
オプション指定あり
$ docker-machine -s /Users/kohei/.docker_tmp/machine ls
NAME         ACTIVE   DRIVER       STATE     URL                         SWARM
debug-test   -        virtualbox   Running   tcp://192.168.99.118:2376   
default      -        virtualbox   Running   tcp://192.168.99.100:2376   
default2     -        virtualbox   Running   tcp://192.168.99.101:2376 

次にdocker-machine envコマンドでも試してみます。

オプション指定なし
$ docker-machine env debug-test
Host "debug-test" does not exist
オプション指定あり
$ docker-machine -s /Users/kohei/.docker_tmp/machine env debug-test
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.118:2376"
export DOCKER_CERT_PATH="/Users/kohei/.docker_tmp/machine/machines/debug-test"
export DOCKER_MACHINE_NAME="debug-test"
# Run this command to configure your shell: 
# eval "$(docker-machine -s /Users/kohei/.docker_tmp/machine env debug-test)"

|| --github-api-token

Github APIを利用するためのトークンを指定します。

コマンド
$ docker-machine --github-api-token=1234567890abcdefghijklmnopqrstuvwxwz1234 create --driver virtualbox debug-test
Running pre-create checks...
Creating machine...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Provisioning created instance...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
To see how to connect Docker to this machine, run: docker-machine env debug-test

|| --native-ssh

このオプションを指定することでGo言語ベースの"native ssh"を利用します。

コマンド
$ docker-machine --native-ssh ssh debug-test
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.9.0, build master : 16e4a2a - Tue Nov  3 19:49:22 UTC 2015
Docker version 1.9.0, build 76d6bc9

Docker Machineを利用する際、ローカル環境にsshがインストールされているかを確認し、必要であればSSHコマンドを利用します。それはDocker Machine作成時であっても利用されます。もし、ローカル環境にsshがインストールされていない場合は、crypto/sshのGo言語で実装されたネイティブ版が利用されます。これはアクセスツールがインストールされていないUNIX系環境や、msysgitがインストールされていないWindows環境である場合に役に立ちます。

よほどのことがない限り、Docker Machineは勝手に処理してくれるので、どうやって細かいことをきにする必要はありません。ただ、敢えてGo言語のネイティブ版を使いたいなら、オプションを指定することで指定が可能です。
  
  
  
とりあえず今回は以上です。オプションの検証の際にサブコマンドを利用する場面がありましたが、サブコマンドについての検証はまた今度書こうと思います。
また、この記事についてもおかしな箇所があれば、随時直していきたいと思います。

13
14
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
13
14