はじめに
本ブログでは、仮想化ソフトウエアとしてVagrantを取り上げています。しかし、Vagrantは気軽に仮想マシンを作成できる反面、起動の遅さについては、Vagrantのリリース当初から指摘されています。今回紹介するDockerは、Vagrantと同じ仮想化ソフトウエアですが、軽量な仮想化環境として人気を集めています。
2014年07月02日 20時36分
本ブログでは、仮想化ソフトウエアとしてVagrantを取り上げています。しかし、Vagrantは気軽に仮想マシンを作成できる反面、起動の遅さについては、Vagrantのリリース当初から指摘されています。今回紹介するDockerは、Vagrantと同じ仮想化ソフトウエアですが、軽量な仮想化環境として人気を集めています。
Dockerについては、アプリ開発者もインフラ管理者も知っておきたいDockerの基礎知識で紹介されていますので、参考にして下さい。
Dockerは、VMware ESXServerに代表されるハイパーバイザー型の仮想化ソフトウエアでは無く、コンテナ型と言われる仮想化ソフトウエアです。Dockerでは、仮想マシンをコンテナとして管理実行できるため、同一サーバ上で複数の異なるコンテナ(仮想マシン)を実行することができます。
今回は、Vagrant上のCentOS にDockerをインストールしますが、CoreOSをサーバハードウェアにインストールするとDockerをハイパーバイザー型のように使用することもできます。
作業の前提条件は、下記のとおりです。
| ソフトウエア | バージョン |
|---|---|
| OS | Windows 7 Ultimate 32bit |
| Cygwin | Setup Version 2.831 |
| VirtualBox | 4.3.12 r93733 |
| Vagrant | 1.6.3 |
Vagrantのシェルプロビジョニングを利用して、Dockerをインストールします。
Vagrantfileを作成します。
5行目では、ローカル環境に作成済みのCentOS 6.5(64bit)のBoxイメージを指定しています。もし、ローカル環境にBoxイメージが無い場合、こちらから、CentOSのBoxイメージをダウンロードできるようにbox_urlを変更して下さい。
20行目以降では、EPELリポジトリのインストールと、EPELリポジトリからdockerパッケージをインストールしています。
$ vi Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "docker"
config.vm.box_url = "file:///export/vagrant/CentOS-6.5-x86_64-minimal.box"
config.vm.hostname = "docker.vagrantup.local"
config.vm.provider :virtualbox do |vb|
vb.gui = false
vb.customize ["modifyvm", :id, "--memory", "512"]
vb.customize ["modifyvm", :id, "--nictype1", "82540EM"]
end
config.vm.provision :shell, inline: <<-EOC
# restart syslog service
service rsyslog stop
cp /dev/null /var/log/messages
service rsyslog start
# intall epel repository
rpm --import http://ftp.riken.jp/Linux/fedora/epel/RPM-GPG-KEY-EPEL-6
rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
sed -i 's/^enabled.*$/enabled = 0/' /etc/yum.repos.d/epel.repo
# install docker
yum -y install --enablerepo=epel docker-io
chkconfig docker on
service docker start
EOC
end
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'docker' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Adding box 'docker' (v0) for provider: virtualbox
default: Downloading: file://D:/export/vagrant/CentOS-6.5-x86_64-minimal.box
default:
==> default: Successfully added box 'docker' (v0) for 'virtualbox'!
==> default: Importing base box 'docker'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: docker_default_1404298265968_6743
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> 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: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
default: Warning: Connection timeout. Retrying...
default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
GuestAdditions 4.3.12 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Setting hostname...
==> default: Mounting shared folders...
default: /script => D:/HOME/punio/vagrant/script
default: /vagrant => D:/HOME/punio/vagrant/docker
==> default: Running provisioner: shell...
default: Running: inline script
==> default: システムロガーを停止中:
==> default: [
==> default: OK ]
==> default: システムロガーを起動中:
==> default: [
==> default: OK ]
==> default: http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm を取得中
==> default: 準備中...
==> default: ##################################################
==> default: epel-release
==> default: ####
==> default: ###
==> default: ###
==> default: ########################################
==> default: Loaded plugins: fastestmirror
==> default: Loading mirror speeds from cached hostfile
==> default: * base: mirror.fairway.ne.jp
==> default: * epel: ftp.tsukuba.wide.ad.jp
==> default: * extras: mirror.fairway.ne.jp
==> default: * updates: mirror.fairway.ne.jp
==> default: Setting up Install Process
==> default: Resolving Dependencies
==> default: --> Running transaction check
==> default: ---> Package docker-io.x86_64 0:1.0.0-3.el6 will be installed
==> default: --> Processing Dependency: xz for package: docker-io-1.0.0-3.el6.x86_64
==> default: --> Processing Dependency: lxc for package: docker-io-1.0.0-3.el6.x86_64
==> default: --> Processing Dependency: libcgroup for package: docker-io-1.0.0-3.el6.x86_64
==> default: --> Running transaction check
==> default: ---> Package libcgroup.x86_64 0:0.40.rc1-5.el6_5.1 will be installed
==> default: ---> Package lxc.x86_64 0:0.9.0-2.el6 will be installed
==> default: --> Processing Dependency: liblxc.so.0()(64bit) for package: lxc-0.9.0-2.el6.x86_64
==> default: ---> Package xz.x86_64 0:4.999.9-0.3.beta.20091007git.el6 will be installed
==> default: --> Running transaction check
==> default: ---> Package lxc-libs.x86_64 0:0.9.0-2.el6 will be installed
==> default: --> Finished Dependency Resolution
==> default:
==> default: Dependencies Resolved
==> default:
==> default: ================================================================================
==> default: Package Arch Version Repository Size
==> default: ================================================================================
==> default: Installing:
==> default: docker-io x86_64 1.0.0-3.el6 epel 4.5 M
==> default: Installing for dependencies:
==> default: libcgroup x86_64 0.40.rc1-5.el6_5.1 updates 125 k
==> default: lxc x86_64 0.9.0-2.el6 epel 78 k
==> default: lxc-libs x86_64 0.9.0-2.el6 epel 116 k
==> default: xz x86_64 4.999.9-0.3.beta.20091007git.el6 base 137 k
==> default:
==> default: Transaction Summary
==> default: ================================================================================
==> default: Install 5 Package(s)
==> default: Total download size: 4.9 M
==> default: Installed size: 25 M
==> default: Downloading Packages:
==> default: --------------------------------------------------------------------------------
==> default: Total 1.7 MB/s | 4.9 MB 00:02
==> default: Running rpm_check_debug
==> default: Running Transaction Test
==> default: Transaction Test Succeeded
==> default: Running Transaction
==> default: Warning: RPMDB altered outside of yum.
Installing : libcgroup-0.40.rc1-5.el6_5.1.x86_64 1/5
==> default:
Installing : lxc-libs-0.9.0-2.el6.x86_64 2/5
==> default:
Installing : lxc-0.9.0-2.el6.x86_64 3/5
==> default:
Installing : xz-4.999.9-0.3.beta.20091007git.el6.x86_64 4/5
==> default:
Installing : docker-io-1.0.0-3.el6.x86_64 5/5
==> default:
Verifying : xz-4.999.9-0.3.beta.20091007git.el6.x86_64 1/5
==> default:
Verifying : docker-io-1.0.0-3.el6.x86_64 2/5
==> default:
Verifying : lxc-libs-0.9.0-2.el6.x86_64 3/5
==> default:
Verifying : libcgroup-0.40.rc1-5.el6_5.1.x86_64 4/5
==> default:
Verifying : lxc-0.9.0-2.el6.x86_64 5/5
==> default:
==> default:
==> default: Installed:
==> default: docker-io.x86_64 0:1.0.0-3.el6
==> default:
==> default: Dependency Installed:
==> default: libcgroup.x86_64 0:0.40.rc1-5.el6_5.1
==> default: lxc.x86_64 0:0.9.0-2.el6
==> default: lxc-libs.x86_64 0:0.9.0-2.el6
==> default: xz.x86_64 0:4.999.9-0.3.beta.20091007git.el6
==> default: Complete!
==> default: Starting cgconfig service:
==> default: [ OK ]
==> default: Starting docker:
==> default: [ OK ]
以上で、Dockerサーバを構築できます。