Docker Machine でローカルマシン上に Docker ホストを構築する #docker

この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。

Docker Machine とは、Docker ホストをローカルマシンやクラウド、データセンター上に構築するツールです。ローカルマシン上で操作する感覚で Docker を操作することができます。
本稿では、ローカルマシンの Debian GNU/Linux 8.1 (amd64) 上に Docker ホストを構築してみます。ローカルマシンに Docker をインストールするのと何が違うのか、ということをまず説明しましょう。Docker Machine は VirtualBox を使って Docker ホストを構築します。作成したそれぞれの仮想マシンが Docker ホストになるので、ローカルマシン上のファイルシステムなどのリソースを Docker に直接使われる心配がなく、Docker 環境の切り替えも簡単です。では Docker Machine の手順通りに見ていきましょう。

まず、Docker Machine の実行バイナリと md5 チェックサムファイルを取得します。ここでは Debian GNU/Linux 8.1 (amd64) をローカルマシンとして使っているので、linux-amd64 をダウンロードします。2015年7月31日現在の最新安定版は v0.3.1 です。

% wget https://github.com/docker/machine/releases/download/v0.3.1/docker-machine_linux-amd64 https://github.com/docker/machine/releases/download/v0.3.1/docker-machine_linux-amd64.md5
:
%

md5 チェックサムが正しいことを確認します。

% md5sum -c docker-machine_linux-amd64.md5
docker-machine_linux-amd64: 完了
%

実行フラグを設定し、バージョン番号を表示する -v オプションを付与して実行してみます。

% chmod +x docker-machine_linux-amd64.md5
% ./docker-machine_linux-amd64 -v
docker-machine_linux-amd64 version 0.3.1 (40ee236)
%

ls サブコマンドは Docker ホストの状態を表示します。まだ 1つも Docker ホストを作成していないので、何も表示されません。

% ./docker-machine_linux-amd64 ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM
%

Docker ホストを作成するには create サブコマンドを使います。--driver (-d) オプションは Docker ホストを作成する対象のドライバを指定します。ここではローカルマシン上に VirtualBox を使って Docker ホストを作成するので virtualbox を指定します。その次に Docker ホストの名前を指定します。ここでは「dev」としています。

% ./docker-machine_linux-amd64 create --driver virtualbox dev
Creating CA: /home/dai/.docker/machine/certs/ca.pem
Creating client certificate: /home/dai/.docker/machine/certs/cert.pem
Image cache does not exist, creating it at /home/dai/.docker/machine/cache...
No default boot2docker iso found locally, downloading the latest release...
Downloading https://github.com/boot2docker/boot2docker/releases/download/v1.7.1/boot2docker.iso to /home/dai/.docker/machine/cache/boot2docker.iso...
Creating VirtualBox VM...
Creating SSH key...
Starting VirtualBox VM...
Starting VM...
To see how to connect Docker to this machine, run: docker-machine_linux-amd64 env dev
%

見ての通り、Docker ホストは VirtualBox と boot2docker を利用して作られています。

再び ls サブコマンドを実行すると、今作成した Docker ホストが表示されます。

% ./docker-machine_linux-amd64 ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM
dev             virtualbox   Running   tcp://192.168.99.100:2376
%

ps コマンドでは VirtualBox が動作していることがわかります。なお、仮想マシンイメージは ~/.docker/ ディレクトリ以下に保存されています。

% ps auxwwwf | grep '[ v]irtualbox'
dai      10522  0.0  0.0 147620 12164 ?        SN   10:14   0:06 /usr/lib/virtualbox/VBoxXPCOMIPCD
dai      10527  0.1  0.0 633448 20832 ?        SNl  10:14   0:25 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
dai       6144 16.4  0.3 1734520 103760 ?      SNl  15:50   0:29  \_ /usr/lib/virtualbox/VBoxHeadless --comment dev --startvm 535b0a81-666e-4f59-958c-24a3701324f9 --vrde config
dai       6163  0.0  0.0 141604 12456 ?        SN   15:50   0:00  \_ /usr/lib/virtualbox/VBoxNetDHCP --ip-address 192.168.99.1 --lower-ip 192.168.99.100 --mac-address 08:00:27:47:78:46 --netmask 255.255.255.0 --network HostInterfaceNetworking-vboxnet3 --trunk-name vboxnet3 --trunk-type netflt --upper-ip 192.168.99.254

env サブコマンドを使うと、指定の Docker ホストを利用するために必要な設定を環境変数の形で表示します。

% ./docker-machine_linux-amd64 env dev
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/home/dai/.docker/machine/machines/dev"
export DOCKER_MACHINE_NAME="dev"
# Run this command to configure your shell:
# eval "$(docker-machine_linux-amd64 env dev)"
%

eval を使って環境変数を設定します。これで以降の docker コマンドは Docker ホスト「dev」に対して行われます。

% eval "$(./docker-machine_linux-amd64 env dev)"
%

docker images で Docker イメージを表示します。これはローカルマシン上の Docker ではなく、Docker ホスト「dev」に対しての結果です。まだ何もありません。

% docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
%

docker run で「hello world」を表示するコンテナを作ってみましょう。繰り返しになりますが、このコンテナは Docker ホスト「dev」上で作成されます。ただし、結果はきちんとローカルマシン上に表示されます。

% docker run busybox echo hello world
Unable to find image 'busybox:latest' locally
latest: Pulling from busybox
cf2616975b4a: Pull complete
6ce2e90b0bc7: Pull complete
8c2e06607696: Already exists
busybox:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:38a203e1986cf79639cfb9b2e1d6e773de84002feea2d4eb006b52004ee8502d
Status: Downloaded newer image for busybox:latest
hello world
%

ip サブコマンドで指定の Docker ホストの IP アドレスを表示できます。

% ./docker-machine_linux-amd64 ip dev
192.168.99.100
%

docker run で nginx コンテナを作ってみましょう。もちろん、このコンテナは Docker ホスト「dev」上で作成されます。

% docker run -d -p 8000:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from nginx
902b87aaaec9: Pull complete
9a61b6b1315e: Pull complete
aface2a79f55: Pull complete
5dd2638d10a1: Pull complete
97df1ddba09e: Pull complete
56c99fa886e8: Pull complete
cd27805ea89f: Pull complete
5e95ac0e9a79: Pull complete
9f36f81a1ccb: Pull complete
4c1809b04591: Pull complete
98c9ccd75644: Pull complete
6886fb5a9b8d: Already exists
nginx:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:a2b8bef3338643176198510fec71c18ceb7834e6f7c45b039ec6e963f43b4005
Status: Downloaded newer image for nginx:latest
592a3110a72cf7d2c386f561ad35498e8a499bf38bafac4ce4fd0645884b84f7
%

curl コマンドで、Docker ホスト「dev」の nginx コンテナにアクセスしてみます。想定通りの動作になりました。

% curl $(./docker-machine_linux-amd64 ip dev):8000
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
<h1>Welcome to nginx!</h1>
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.

<em>Thank you for using nginx.</em>

Docker ホストを停止するには stop サブコマンドを使います。

% ./docker-machine_linux-amd64 stop dev
%

停止した Docker ホストを起動するには start サブコマンドを使います。表示された注意の通り、仮想マシンの IP アドレスが変わっているかもしれないので、env サブコマンドで環境変数の更新をしておきましょう。

% ./docker-machine_linux-amd64 start dev
Starting VM...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
%

まとめ

Docker Machine を使ってローカルマシンの Debian GNU/Linux 8.1 (amd64) 上に Docker ホストを構築してみました。仮想マシンを用いてローカルマシン上のリソースを直接触らないようにし、また Docker 環境を簡単に切り替えられるため、開発以外にもさまざまな用途に利用ができるでしょう。
本稿で紹介した以外に Docker Machine は多数のサブコマンドや、VirtualBox 以外のドライバの例も記載されています。是非参照してみてください。

Author

はやりの技術要素に加えて、会議の進め方・文章の書き方などの業務改善にも取り組んでいます。「Chef活用ガイド」共著のほか、Debian Official Developerもやっています。

Daisuke Higuchiの記事一覧

新規CTA