LoginSignup
19
19

More than 5 years have passed since last update.

boot2docker + vboxsf

Posted at

boot2dockerを使おう

まえがき

昨年秋から利用しているDockerですが,Boot2Dockerという多段仮想化(?)によりMac上でも利用できるようになりました.
そこで,新しく研究室に配属された学生にDockerを使ってもらおうと布教活動していたのですが,「難しい」と言う声が多く聞こえてきました.

何でそんなに難しいのだろうと思ったら,検索結果が「純粋なDockerのもの」と「boot2dockerのもの」と「Vagrantを併用したCoreOSのもの」,「旧いDockerのもの」と「新しいDockerのもの」が混在していて,ゼロから始めるには敷居が高かったようです.

また,上記の違いを理解しながら/bin/bashを起動しても,「それで?」と言われるのがオチだと悟りました.と言うことで,Dockerを実用的に使うためのDockerfileの例を作ろうと思いました.

今回困ったこと

「まずはWebサーバ(nginx)の入ったコンテナを立ち上げて,次にRailsとかnode.jsのような動的なコンテナを作って・・・あ,DataBaseコンテナも作るか.ファイルはコンテナとの間の共有ディレクトリに置いとけば良いや.」と考えながら作業をしていたのですが,boot2docker.isoから作成したVMでは,VirtualBoxの共有ファイルをマウントできません.Linuxのカーネルモジュールがインストールされていないのです.本来はVirtualBox GuestAddtionsの一部なので,まとめてインストールすれば良いのですが,それをするためのツールが全くと言って良いほどboot2dockerのVM上にインストールされていませんでした.

そこで,共有ディレクトリが使えるというイメージを見つけたのですが,dockerコマンドとの間でバージョンが異なり使えないことが分かりました.GuestAdditionを強引にインストールするか,新たなboot2dockerのイメージを作るか,対応しているイメージを探すか・・・堂々巡りをしながら色々と探し回って,やっと対応しているイメージを見つけることができました.
dduportal様,ありがとうございます.

dduportal/boot2docker

以下,作業記録です.

作業記録:コンソール1

上記ページのUsageに従い,まずはvagrant initからです.

suda@sahara:/Volumes/RamDisk/test$ vagrant init dduportal/boot2docker
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
suda@sahara:/Volumes/RamDisk/test$

init作業が完了したようです.
Vagrantfileが置かれていることを確認しましょう.

suda@sahara:/Volumes/RamDisk/test$ ls
Vagrantfile
suda@sahara:/Volumes/RamDisk/test$

無事にVagrantfileが置かれているので起動(up)してみます.

suda@sahara:/Volumes/RamDisk/test$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'dduportal/boot2docker' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 4243 => 4243 (adapter 1)
default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: docker
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => /Volumes/RamDisk/test
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.
suda@sahara:/Volumes/RamDisk/test$

無事に起動できたようなのでログインしてみます.


suda@sahara:/Volumes/RamDisk/test$ vagrant ssh
                        ##        .
                  ## ## ##       ==
               ## ## ## ##      ===
           /""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
           \______ o          __/
             \    \        __/
              \____\______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
boot2docker: 0.8.0
docker@boot2docker:~$

プロンプトが変わり,無事にログインできたことが分かります.
続いてMac OS X←→boot2docker間の共有ディレクトリをチェックしてみます.

docker@boot2docker:~$ mount
rootfs on / type rootfs (rw,relatime,size=456420k,nr_inodes=87336)
proc on /proc type proc (rw,relatime)
sysfs on /sys type sysfs (rw,relatime)
devpts on /dev/pts type devpts (rw,relatime,mode=600,ptmxmode=000)
tmpfs on /dev/shm type tmpfs (rw,relatime)
none on /dev/mqueue type mqueue (rw,relatime)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/sda on /mnt/sda type ext4 (rw,relatime,data=ordered)
cgroup on /sys/fs/cgroup type tmpfs (rw,relatime,mode=755)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu type cgroup (rw,relatime,cpu)
cgroup on /sys/fs/cgroup/cpuacct type cgroup (rw,relatime,cpuacct)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,relatime,freezer)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,relatime,perf_event)
/dev/sda on /mnt/sda/var/lib/docker type ext4 (rw,relatime,data=ordered)
none on /vagrant type vboxsf (rw,nodev,relatime)
docker@boot2docker:~$

/vagrantにvboxsf(VirtualBox Shared Fileか?)がマウントされていることが分かります.
VirtualBoxを起動して確かめてみたところ,Mac OS X上のVagrantfileが置かれているディレクトリが,boot2docker上の/vagrantにマウントされているようです.
実際にファイルが有ることを確認します.

docker@boot2docker:~$ ls /vagrant
Vagrantfile
docker@boot2docker:~$

作業記録:コンソール2

別のコンソールからdockerを起動します.
まずはバージョンのチェックです.

suda@sahara:/Volumes/Macintosh HD 2/boot2docker$ docker version
Client version: 0.11.1
Client API version: 1.11
Go version (client): go1.2.1
Git commit (client): fb99f99
Server version: 0.11.1
Server API version: 1.11
Git commit (server): fb99f99
Go version (server): go1.2.1
Last stable version: 0.11.1
suda@sahara:/Volumes/Macintosh HD 2/boot2docker$

続いて,ubuntuのイメージからbashを起動します.

suda@sahara:/Volumes/Macintosh HD 2/boot2docker$ docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu' locally
Pulling repository ubuntu
3db9c44f4520: Download complete
74fe38d11401: Download complete
99ec81b80c55: Download complete
a7cf8ae4e998: Download complete
5e019ab7bf6d: Download complete
316b678ddf48: Download complete
511136ea3c5a: Download complete
5e66087f3ffe: Download complete
4d26dd3ebc1c: Download complete
d4010efcfd86: Download complete
e2aa6665d371: Download complete
6cfa4d1f33fb: Download complete
ef519c9ee91a: Download complete
f10ebce2c0e1: Download complete
02dae1c13f51: Download complete
07302703becc: Download complete
e7206bfc66aa: Download complete
cf8dc907452c: Download complete
cb12405ee8fa: Download complete
f0ee64c4df74: Download complete
82cdea7ab5b5: Download complete
2209cbf9dcd3: Download complete
5dbd9cb5a02f: Download complete
root@f3d143984796:/#

プロンプトも変わり,無事にdocker上のbashが起動することが確認できました.
一旦,bashを終了させて,boot2dockerの共有ディレクトリを/mntにマウントさせつつbashを起動してみます.

root@f3d143984796:/# exit
suda@sahara:/Volumes/Macintosh HD 2/boot2docker$ docker run -v /vagrant:/mnt -i -t ubuntu /bin/bash
root@8f8f858c8520:/# ls /mnt
Vagrantfile
root@8f8f858c8520:/#

無事にVagrantfileが有りました.

作業記録:新たにファイルを作ってみる

きちんとファイル共有が機能していることを確認するために,Mac OS X側で新たなファイルを作成してみます.

suda@sahara:/Volumes/RamDisk/test$ touch testfile
suda@sahara:/Volumes/RamDisk/test$ ls
Vagrantfile testfile
suda@sahara:/Volumes/RamDisk/test$

続いて,docker側でファイルを確認してみます.

root@8f8f858c8520:/# ls /mnt
Vagrantfile testfile
root@8f8f858c8520:/#

無事にファイルが増えていることを確認できました.

結論

標準的なboot2dockerのイメージでVirtualBoxのファイル共有に対応してくれれば良いのですが,素のdockerを使う場合には無駄になるので悩ましいところですね.
また,VMWare上にboot2dockerを立ち上げたい人がいるかもしれないと考えると,標準イメージがどれか一つの仮想環境に特化してしまうのもつまらないですし.

boot2dockerは,多段仮想化(?)ゆえ,ちょっとしたコツが必要ですが,ノウハウが溜まってくれば使いやすくなるはずなので,どんどん使っていこうと思います.

「たぶん,他の良いやり方があるんだろうな・・・」と考えつつ,同じ問題で困っている人の助けになればと思います.

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