サーバ構築

DockerMachineさわってみる

更新日:

概要

DockerMachineでできること

  • ローカルマシンから、クラウドサービス上またはVMware/VirtualBox上にDockerホスト用のインスタンス作成できる
  • 作成したDockerホスト用のインスタンスの操作(作成/削除/起動/停止とか)ができる
  • 作成したDockerホスト用のインスタンスにDockerコンテナ起動できる

Docker動かすためのサーバ作成、Dockerインストール、コンテナ起動など面倒なことを一通りコマンドで楽にやってくれる。

インストール

Docker Toolbox をインストールすれば一緒に入ってる。

個別にDockerMachineだけインストールする場合は、バイナリを直接もってきて置いておけば良い。

# curl -L https://github.com/docker/machine/releases/download/v0.15.0/docker-machine-`uname -s`-`uname -m` > /usr/local/bin/docker-machine 
# chmod +x /usr/local/bin/docker-machine

確認

# docker-machine version 
docker-machine version 0.15.0, build b48dc28d

操作してみる

Dockerホスト作成

  • ローカルのVirtualBoxに作成
$ docker-machine create -d "virtualbox" docker-host
Creating CA: /Users/hogehoge/.docker/machine/certs/ca.pem
Creating client certificate: /Users/hogehoge/.docker/machine/certs/cert.pem
Running pre-create checks...
(docker-host) Image cache directory does not exist, creating it at /Users/hogehoge/.docker/machine/cache...
(docker-host) No default Boot2Docker ISO found locally, downloading the latest release...
(docker-host) Latest release for github.com/boot2docker/boot2docker is v18.06.0-ce
(docker-host) Downloading /Users/hogehoge/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.06.0-ce/boot2docker.iso...
(docker-host) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(docker-host) Copying /Users/hogehoge/.docker/machine/cache/boot2docker.iso to /Users/hogehoge/.docker/machine/machines/docker-host/boot2docker.iso...
(docker-host) Creating VirtualBox VM...
(docker-host) Creating SSH key...
(docker-host) Starting the VM...
(docker-host) Check network to re-create if needed...
(docker-host) Found a new host-only adapter: "vboxnet3"
(docker-host) 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 docker-host
  • 確認
$ docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
docker-host   -        virtualbox   Running   tcp://192.168.99.100:2376           v18.06.0-ce   
  • 作成したDockerホストに接続するための環境設定
$ docker-machine env docker-host
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/hironori_megumi/.docker/machine/machines/docker-host"
export DOCKER_MACHINE_NAME="docker-host"
# Run this command to configure your shell: 
# eval $(docker-machine env docker-host)
  • Dockerホストに接続する
$ eval $(docker-machine env docker-host)
↑でDockerホストに接続されて↓のコマンドはリモートサーバ(Dockerホスト)上で実行されている
$ docker info 
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 18.06.0-ce
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 0
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: d64c661f1d51c48782c9cec8fda7604785f93587
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.93-boot2docker
Operating System: Boot2Docker 18.06.0-ce (TCL 8.2.1); HEAD : 1f40eb2 - Thu Jul 19 18:48:09 UTC 2018
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 995.6MiB
Name: docker-host
ID: G2F3:M7IF:Y3NJ:P4IZ:5BM3:J6LU:WZOE:7SIN:5DIK:XCND:RCB6:F57Z
Docker Root Dir: /mnt/sda1/var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
 provider=virtualbox
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

Dockerホスト上にコンテナ起動してみる

  • ローカルPCにあるcomposeファイルでコンテナ起動
$ docker-compose up -d
Pulling dnmonster (amouat/dnmonster:1.0)...
1.0: Pulling from amouat/dnmonster
....
  • 起動確認
$ docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                        NAMES
fc38d0f7b0bb        identidock_identidock   "/cmd.sh"                53 seconds ago      Up 53 seconds       9090/tcp, 0.0.0.0:5000->5000/tcp, 9191/tcp   identidock_identidock_1
759d2b6ca5be        amouat/dnmonster:1.0    "npm start"              54 seconds ago      Up 53 seconds       8080/tcp                                     identidock_dnmonster_1
9d4c4ce09f82        redis:4.0               "docker-entrypoint.s…"   54 seconds ago      Up 53 seconds       6379/tcp                                     identidock_redis_1
  • dockerホストのIP指定して、ローカルPCからcurlで接続確認
$ curl -I $(docker-machine ip docker-host):5000
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 333
Server: Werkzeug/0.14.1 Python/3.7.0
Date: Wed, 08 Aug 2018 13:47:36 GMT

DockerホストへのSSH接続

$ docker-machine ssh docker-host
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.0-ce, build HEAD : 1f40eb2 - Thu Jul 19 18:48:09 UTC 2018
Docker version 18.06.0-ce, build 0ffa825
docker@docker-host:~$ 

docker@docker-host:~$ docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                        NAMES
fc38d0f7b0bb        identidock_identidock   "/cmd.sh"                5 minutes ago       Up 5 minutes        9090/tcp, 0.0.0.0:5000->5000/tcp, 9191/tcp   identidock_identidock_1
759d2b6ca5be        amouat/dnmonster:1.0    "npm start"              5 minutes ago       Up 5 minutes        8080/tcp                                     identidock_dnmonster_1
9d4c4ce09f82        redis:4.0               "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes        6379/tcp                                     identidock_redis_1
docker@docker-host:~$ 

Dockerホスト削除

  • とりあえず停止
$ docker-machine stop docker-host 
Stopping "docker-host"...
Machine "docker-host" was stopped.
  • もういらないので削除
$ docker-machine rm docker-host 
About to remove docker-host
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed docker-host

-サーバ構築
-

Copyright© 明日から頑張ります。 , 2026 All Rights Reserved Powered by STINGER.