1
1

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 3 years have passed since last update.

Docker Machine(個人的備忘録)

Last updated at Posted at 2020-09-03

Docker Machineとは

Docker Engineを搭載した仮想マシンの作成、起動、停止、再起動などコマンドラインから実行できるツール。ローカルPCだけでなく、リモートのクラウドプロバイダでDockerホストを立ち上げ管理することも可能。

Docker for Mac, Docker Toolboxを使っている人
・VirtualBoxで仮想マシンを起動

Docker for Windowsを使っている人
・Hyper-Vによって仮想マシンを起動

Docker Machineを利用してローカルPCにDockerホストを作成する方法

Docker Machineのダウンロード

% base=https://github.com/docker/machine/releases/download/v0.16.0 && curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/usr/local/bin/docker-machine && chmod +x /usr/local/bin/docker-machine
Usage: docker-machine [OPTIONS] COMMAND [arg...]

Create and manage machines running Docker.

Version: 0.16.0, build 702c267f

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

Options:
  --debug, -D                                                   Enable debug mode
  --storage-path, -s "/Users/torigoshikouki/.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]
  --bugsnag-api-token                                           BugSnag API token for crash reporting [$MACHINE_BUGSNAG_API_TOKEN]
  --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
  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
  provision             Re-provision existing 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
  mount                 Mount or unmount a directory from a machine with SSHFS.
  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
  version               Show the Docker Machine version or a machine docker version
  help                  Shows a list of commands or help for one command
  
Run 'docker-machine COMMAND --help' for more information on a command.

ダウンロードが完了したら上記が表示される。

Dockerホストの表示

-# dockerホストの表示
% docker-machine ls

Dockerホストの作成コマンド

-# % docker-machine create —driver virtualbox Dockerホスト名
% docker-machine create —driver virtualbox default
-# dockerホストの表示
% docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12

—driver virtualboxはデフォルトでvirtualboxが使われるので付け足す必要はない。
「default」というdockerホストが作成されたのがわかる。

作成したdockerホストに接続する方法

-# docker-machine env dockerホスト名
% docker-machine env default
-# 操作対象のdockerホストに設定するための設定コマンドが表示される。
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/ユーザー名/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell: 
# eval $(docker-machine env default)

exportの部分の環境変数を設定すると、dockerコマンドの操作対象がenvの引数に指定したdockerホストに設定される。しかし下記に書いてあるとおり、**eval $(docker-machine env default)**を実行することにより、まとめて実行することができる。

dockerホストのACTIVEの確認

-# dockerホストの表示
% docker-machine ls
-# ACTIVEに「*」がついており、どのdockerホストがACTIVEか確認できる。
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12


-# 確認のためにdocker run hello-worldで起動
% docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:7f0a9f93b4aa3022c3a4c147a449bf11e0941a1fd0bf4a8e6c9408b2600777c5
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

「Hello from Docker!」これは今まで使用してきたDocker Macなどの仮想マシンではなく、今回作成したdefaultホスト上で動作しているということ。docker-machine sshでホストにssh接続することも可能。

DockerホストへのSSH接続

-# % docker-machine ssh dockerホスト名
% docker-machine ssh default
docker@default:~$ docker ps -a                                                                                                                                               
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
ab7af80f762d        hello-world         "/hello"            4 minutes ago       Exited (0) 4 minutes ago                       cranky_mccarthy

これでdockerホストでコンテナが実行されたことがわかる。exitで元のターミナルに戻る

DockerホストのIPの確認

-# % docker-machine ip dockerホスト名
% docker-machine ip default
192.168.99.100

Nginxを起動して、IP指定でwebページが返ってくることを確認する

-# nginxの起動
% docker run -d -p 8080:80 nginx

起動ができたら、192.168.99.100:8080で確認する。dockerホストを作成した時点で起動していたが、明示的に行う場合はdocker-machine startdocker-machine stopを使用する。

Dockerホストの停止

-# % docker-machine stop dockerホスト名
% docker-machine stop default
-# dockerホストの表示
% docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL   SWARM   DOCKER    ERRORS
default   -        virtualbox   Stopped                 Unknown

STATEが**「Stopped」**に変わっており停止していることがわかる。

Dockerホストの起動

-# % docker-machine start dockerホスト名
% docker-machine start default
-# dockerホストの表示
% docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12

STATEが**「Running」**に変わっており起動していることがわかる。

Env,stop,startコマンドは引数にdockerホストを指定しなかった場合、自動的にdefaultという名前が指定されたものとして動作する。

ACTIVEのdockerマシンの選択を解除

Docker-machine envコマンドで設定した環境変数を削除することにより解除することができる。

-# Docker-machineの環境変数を削除するためのコマンドが表示される。
% docker-machine env -u
unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
# Run this command to configure your shell: 
# eval $(docker-machine env -u)
-# 設定したと同じようにeval $(docker-machine env -u)でまとめて解除することができる。
% eval $(docker-machine env -u)
-# dockerホストの全表示
% docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12

ACTIVEが**「-」**になり解除されているのがわかる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?