SEワンタンの独学備忘録

IT関連の独学した内容や資格試験に対する取り組みの備忘録

【Docker】入門⑪docker-machine@VirtualBox環境構築と基本コマンド

久しぶりのDocker。やるか迷ったけどDocker-Machineについて少しいじくります。
Docker-Machineを使用できるようにするまでに紆余曲折、うまくいかないこともいろいろありましたがちゃんとできる環境が見つかりました。

Docker-Machine

Docker-Machineについて

Docker-Machineはクラウド環境や仮想環境などにDockerの実行環境を構築するコマンドラインツールです。

実行環境の構築なので、コンテナを直接管理するわけではありません。
※少し勘違いしていました。。

イメージとしてはVirtualBox、AWS、GCPなどの指定した環境に対してDockerを使用できる最小OSの仮想マシンを構築します。

f:id:wantanBlog:20200719223839p:plain

OSのイメージは「boot2docker.iso」が使用されるようです。

・参考

[root@localhost cache]# ll /root/.docker/machine/cache
合計 57344
-rw-------. 1 root root 58720256  7月 17 23:14 boot2docker.iso

構築イメージ

以下のような環境に構築しました。
仮想環境のネスト構造になっています。

f:id:wantanBlog:20200720224110p:plain

ホストマシン(PC)はWindows10、そこにVMware Workstation PlayerをインストールしてCentOS7(仮想マシン)を構築。
そのCentOS7にDocker-machineVirtualBoxをインストールする。その状況でDocker-machineを起動してVirtualBoxに対して実行環境(仮想マシン)を生成する。

・VMware Workstation Player
www.wantanblog.com

・VirtualBox@CentOS7
以下はGCEにインストールしていますが、CentOS7であれば後半手順をほぼそのままで実行できます。
www.wantanblog.com

Docker-machine@CentOS7インストール

CentOS7にDocker-Machineをインストールします。
当該環境にDocker本体が入っていることを前提とします。

公式ドキュメント
docs.docker.jp


・インストールコマンド
以下のコマンドで一発インストールできます。

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

バージョン確認ができればOK。

[root@localhost wantan]# docker-machine --version
docker-machine version 0.7.0, build a650a40

Docker Machine 環境の構築

Virtual Box上で動かしてみる

公式の以下を参考にしました。
Docker Machine をローカル VM で始めるには — Docker-docs-ja 17.06 ドキュメント

VirtualBox上にDocker-machineでdefaultという名前のコンテナを生成します。
CentOS7で以下のコマンドを実行。

docker-machine create --driver virtualbox default

・出力結果

[root@localhost wantan]# docker-machine create --driver virtualbox default
Running pre-create checks...
Creating machine...
(default) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/m                                       achine/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

実行後以下のコマンドで生成できていることを確認します。

[root@localhost wantan]# docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12

基本コマンド

基本的なコマンドだけ軽く確認します。

実行環境の作成

実行環境の作成にはcreateコマンドを使用します。
前述の構築にも使用しています。

docker-machine create --driver virtualbox default

--driverオプションで実行環境を構築する場所を指定します。
今回はvirtualboxに環境を構築しているためシンプルになりますが、他の環境に構築を行う場合には別途指定などが必要になります。

実行環境の一覧表示

■環境の一覧表示(ls)

docker-machine ls

■環境のURL表示(url)

docker-machine url default

・実行結果例

tcp://192.168.99.100:2376

■環境の状態表示

docker-machine status default

・実行結果例

Running
実行環境の起動/停止

■環境の起動

docker-machine start default

■環境の停止

docker-machine stop default

■環境の再起動

docker-machine restart default
実行環境へ接続

構築した実行環境にSSH接続を行う。

docker-machine ssh default

・実行結果

[root@localhost cache]# docker-machine ssh default
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@default:~$
ファイル授受

scpコマンドと同じ要領で使えばいい。
以下の例ではdefaultマシンからカレントディレクトリにファイルをコピーする。

docker-machine scp default:/home/docker/test.txt .
実行環境の存在確認

■環境のip確認

docker-machine ip default

・結果確認

[root@localhost ~]# docker-machine ip default
192.168.99.100

■環境の詳細情報表示
普通のdockerコマンドでも使用するinspectなのでわりと覚えやすい。

docker-machine inspect default
実行環境の削除

普通の削除コマンド。

docker-machine rm default


ーーーーーーーーーーーーーーー

実行環境でコンテナ起動するぐらいまではやりたいけどあんまり深くはできなそう。。