1.06.2014

Vagrant: Creating CentOS 6.5 Docker Host

Vagrant: CentOS 6.5 Docker 母艦の建造

 

Vagrant 1.4 で Docker へのプロビジョニング対応 (Vagrant 1.4 - Vagrant) が行われたことで
ローカルマシン(VirtualBox)上での Docker ホストOS の構築はより一層簡単になった。

尚、Ubuntu ホストであればこちらの手順通り、公式リポジトリをそのまま使えばよい。

 

事前準備

 

環境
  • ローカルマシン: Mac OS X 10.9.1
    • Vagrant: 1.4.2 (確認は「vagrant -v」)
    • VirtualBox: 4.3.6
  • Docker ホスト (母艦): CentOS 6.5
    • Docker: 0.7.2 (確認は「docker -v」)
  • Docker コンテナ: CentOS 6.4

 

母艦の立ち上げ

基本的には適当なディレクトリに Vagrantfile を作成して、「vagrant up」するだけ。

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "opscode-centos65"
  config.vm.box_url = "http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box"

  config.vm.provider :virtualbox do |vb|
    vb.name = "docker1"
    vb.customize ["modifyvm", :id, "--memory", 1024]
  end

  config.vm.network :private_network, ip: "192.168.33.10"

  config.vm.provision :docker do |d|
    d.pull_images "centos:6.4"
  end
end
  • 8行目: OpsCode 提供の CentOS 6.5 box をダウンロードして利用
  • 11-12行目: VirtualBox の VM に docker1 という名前を付け、メモリを少し増やす
  • 15行目: Docker コンテナへ直接アクセスしたいので、静的アドレスを割り振った方が便利
    (ネットワークアドレスは VirtualBox の仮想ネットワークアダプタの設定に依存。
    デフォルトでは host-only ネットワークが 192.168.33.0/24 として構築されている)
  • 17-19行目: Docker のプロビジョニング設定。ここでは centos:6.4 のイメージを pull している。
    これを記述することで、Docker 自体のインストール、サービスへの登録が自動的に行われる。

 

仮想マシンの作成

単に「vagrant up」でもよいが、ゴミの消去も含めたシェルスクリプトを書いて実行した。

#!/bin/bash


### Clean.
vagrant destroy --force

### Create VirtualBox image and boot.
vagrant up

### Print status.
vagrant status

実行結果は以下のとおり。プロビジョニングの工程で Docker のインストールが行われている。

[default] VM not created. Moving on...
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'opscode-centos65'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[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] Machine booted and ready!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] Running provisioner: docker...
[default] Installing Docker (latest) onto machine...
[default] Pulling Docker images...
[default] -- Image: centos:6.4
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

ちなみに、vagrant-vbguest プラグインがインストールされている場合には以下のようなビルドエラーが発生した。

Building the OpenGL support module[FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions[  OK  ]
Installing the Window System drivers[FAILED]
(Could not find the X.Org or XFree86 Window System.)
An error occurred during installation of VirtualBox Guest Additions 4.3.6. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.

X Window はインストールされていないので無視してよさそうだが、OpenGL のほうは少し気になる。

 

母艦への接続

「vagrant ssh」で母艦に接続した後、docker コンテナを立ち上げて
/etc/redhat-release ファイルの中身を参照してみた。

$ vagrant ssh
Last login: Thu Dec  5 05:57:05 2013 from 10.0.2.2
[vagrant@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              6.4                 539c0211cd76        9 months ago        300.6 MB
[vagrant@localhost ~]$ sudo docker run centos:6.4 /bin/cat /etc/redhat-release
CentOS release 6.4 (Final)

Cent OS 6.4 のコンテナが起動したことが確認できた。

 

 

 

References

 

Related Posts

0 件のコメント:

コメントを投稿