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

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

【Docker】Docker machineを使用したDocker hostの作成方法【入門】

f:id:takanori5:20181028132956p:plain

Docker machineを使用したDocker hostの作成

docker hostの確認コマンド

$ docker-machine ls

試しに実行すると起動中のhostがないことがわかります。

$ docker-machine ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM   DOCKER   ERRORS

docker hostの起動コマンド

以下は仮想化のdriverにvirtualboxを使用してdefaultという名前でhostを作成

$ docker-machine create --driver virtualbox default

コマンドを実行して見ます。

hodzumitakanori-no-MacBook-Air:DockerAutomatedBuild hodzumitakanori$ docker-machine create --driver virtualbox default
Creating CA: /Users/hodzumitakanori/.docker/machine/certs/ca.pem
Creating client certificate: /Users/hodzumitakanori/.docker/machine/certs/cert.pem
Running pre-create checks...
(default) Image cache directory does not exist, creating it at /Users/hodzumitakanori/.docker/machine/cache...
(default) No default Boot2Docker ISO found locally, downloading the latest release...
(default) Latest release for github.com/boot2docker/boot2docker is v18.09.0
(default) Downloading /Users/hodzumitakanori/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.09.0/boot2docker.iso...


(default) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(default) Unable to get the local Boot2Docker ISO version:  Did not find prefix "-v" in version string
(default) Default Boot2Docker ISO is out-of-date, downloading the latest release...
(default) Latest release for github.com/boot2docker/boot2docker is v18.09.0
(default) Downloading /Users/hodzumitakanori/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.09.0/boot2docker.iso...

(default) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
(default) Copying /Users/hodzumitakanori/.docker/machine/cache/boot2docker.iso to /Users/hodzumitakanori/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Found a new host-only adapter: "vboxnet0"
(default) Waiting for an IP...

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 boot2docker...
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 default

起動確認

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v18.09.0

defaultの名前でdocker hostが起動しました!

hostが起動したので、このhostに接続する方法を見ていきます!
こちらのコマンドで、捜査対象のdocker hostに接続するための設定が出力されます。

$ docker-machine env default

それでは実行してみます。
下に記載のコマンド(eval $(docker-machine env default))を記載することで、まとめて環境変数を設定できます。

$ docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/hodzumitakanori/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env default)

やってみます。

$ eval $(docker-machine env default)

これで設定できました。簡単ですね。

では、このhost上でhelloworldコンテナを起動してみましょう!!!

docker hostでhellowoldコンテナを起動

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
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/

実行できました!!
ではdefaultのhostにssh接続して、このhostでhello worldが実行されたことを
確認してみます。

$ docker-machine ssh default
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker ps

psコマンドでprocessを確認すると
hello worldgが実行されたことがわかります。

docker@default:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
8741da52aca1        hello-world         "/hello"            36 seconds ago      Exited (0) 35 seconds ago                       amazing_keller
docker@default:~$

docker hostとの接続を解除

defaultとの接続を解除しましょう!
先ほどコマンドで設定した環境変数を削除すればOKです。

$ 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)