tjinjin's blog

インフラ要素多めの個人メモ

virtualbox・vagrant・dockerの導入メモ

新年あけましておめでとうございます。
最初の日記は目標書こうと思ったのですが、環境構築やってたので、
ブログ上でメモしておきます。

目的

windows7virtualbox・vagrant・dockerを導入する。
今までVMwarePlayerを利用してましたが、環境構築に時間もかかりsnapshotもとれなかったので、最近自分の中ではやりのvagrant/dockerを導入することにしました。

※さまざまなサイトを参考にさせていただいているので、参考サイトとして載せました。それぞれのサイトの方が詳しいと思うので、詳細知りたい方はアクセスしてみてください。

★参考サイト
http://k-holy.hatenablog.com/entry/2013/08/30/192243

Virtual Box / Vagrantの導入

VirtualBoxのインストール

公式サイトより下記をダウンロード
http://download.virtualbox.org/virtualbox/4.3.6/VirtualBox-4.3.6-91406-Win.exe

ダウンロードしたファイルをダブルクリックして、とくに設定を変更せずにインストール。Oracleのデバイスのインストールダイヤログが出ましたが気にせずインストール

②Vagrantのインストール

下記よりダウンロード
http://files.vagrantup.com/packages/a40522f5fabccb9ddabad03d836e120ff5d14093/Vagrant_1.3.5.msi
ダウンロードしたファイルをダブルクリックして、とくに設定を変更せずインストール

③Vagrant Boxファイルを指定する

centos6.4を一旦ローカルにダウンロード(追記があったので念のため)
http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box
コマンドプロンプトより下記コマンドを実行。
今回はVagrantのインストールフォルダにボックスファイル用フォルダを作成し、そこで実行してみました。

c:\HashiCorp\boxfile>vagrant init centos64_64
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.

どうやら成功したみたいなので、仮想マシンを起動します。

c:\HashiCorp\boxfile>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* The box 'centos64_64' could not be found.

centos64_64というものが見つからないらしい。
どうやらvagrant box addをたたいてインストールする必要があるらしい(そりゃそうだ)
boxファイルを指定して実行します。
ググった感じだとboxファイルはC:\Users\{YOURUSERNAME}\.vagrant.d\あたりに入れるようです。今回は試しなのでさっき作った場所を指定。

c:\HashiCorp\boxfile>vagrant box add centos64_64 c:\HashiCorp\boxfile\CentOS-6.4
-x86_64-v20130427.box
Downloading or copying the box...
Extracting box...ate: 578M/s, Estimated time remaining: --:--:--)
Successfully added box 'centos64_64' with provider 'virtualbox'!

もう一度起動させてみる。
なんかエラー出た・・・

c:\HashiCorp\boxfile>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64_64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.2.12
VirtualBox Version: 4.3
[default] Mounting shared folders...
[default] -- /vagrant

なんかエラー出てる?VirtualBoxのバージョンがあってないらしい。
とりあえず仮想マシンを確認。

c:\HashiCorp\boxfile>vagrant status
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`.

一応動いてるみたいです。ログインしてみます。
TeraTermより仮想マシンへログイン。

Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$
[vagrant@localhost /]$ which perl
/usr/bin/perl

おお動いてる!
rootでログインしたいんだがどうやるんだろ?
たぶんboxファイルのドキュメントとかでわかりそうな気はしますが、見つけられなかったです。自分で初期のboxファイルのテンプレート作っとけば何度も同じ環境作れて便利ですね。

④saharaプラグインを導入する

参考ページに従いsaharaプラグインを導入。コミット・ロールバックができるようになるらしい。
コマンドプロンプト上で

c:\HashiCorp\boxfile>vagrant plugin install sahara
Installing the 'sahara' plugin. This can take a few minutes...
Installed the plugin 'sahara (0.0.16)'!

vagrantインストールしたときにプラグインもDLしていたのか、すぐに導入できました(そういうものなのかな)
試しに/bin/lsを削除してみるw

[vagrant@localhost bin]$ sudo rm ls
[vagrant@localhost bin]$ ls
-bash: /bin/ls: No such file or directory

lsコマンド実行できなくなった(´・ω・`)
急いでrollbackを実行する

c:\HashiCorp\boxfile>vagrant sandbox rollback
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
[vagrant@localhost /]$ which ls
alias ls='ls --color=auto'
        /bin/ls

おお!復活したwこれでrm -rf / を間違って実行しても安心ですねw

あとは参考ページある通り、IPアドレス変更をして一旦終わり。いま思えばboxfileのフォルダの切り方失敗しましたね。仮に複数仮想マシン作ったときに
Vagrantfileが競合しそう。違うフォルダで仮想マシン作ればいいかな。管理上面倒になりそうです。

dockerを導入する。

いまいちvagrantとdockerの使いわけとかわかってないですが、参考になりそうなスライドがあったのでメモ。
★参考サイト
https://speakerdeck.com/smagch/vagrant-ansible-sosite-docker-an-da-falsezhan-ihakorekarada

dockerっていうのはboxファイルの代わりみたいなものなのかな?ホストを意識しない仮想環境みたいな感じでしょうか。

①git(msysgit)のインストール

まずは、gitのクライアントがないと面倒そうなのでインストール
★参考サイト
http://www.02.246.ne.jp/~torutk/git/gitonwindows.html
安定してそうな1.8.4をダウンロード
https://msysgit.googlecode.com/files/Git-1.8.4-preview20130916.exe

サイトに従ってやればとりあえず問題なさそう。プロンプトからはgitコマンドをたたけない?パスの問題か?
よくわからないが、git用のCUIを利用しサイトを参考にdockerを試します。

★参考サイト
http://apatheia.info/blog/2013/06/17/docker/
http://blog.kvhasia.com/ja/tech-insights/introducing-docker-a-tool-to-bring-linux-container-to-your-life/

②dockerの導入

$ git clone https://github.com/dotcloud/docker.git
$ cd docker
$ vagrant up --provider virtualbox

Vagrant上でubuntu仮想マシンを立ち上げてそのうえでDockerを動かす感じみたいですね。エラーが出ましたが、ubuntuのboxファイルがないのでダウンロードからやっているみたい。簡単なコマンドでboxのダウンロードから起動までやってくれてすごく便利ですね(時間はかかりますが)

Teratermよりubuntuへログイン
ポートは起動時に出るので、それで接続(自分の環境では2200だった)
IPは127.0.0.1でいける。

③イメージファイルのダウンロード

Dockerfileを作成し、イメージをダウンロード

vagrant@precise64:~$ cat Dockerfile
FROM ubunt
vagrant@precise64:~$ docker build .
Uploading context 64.19 MB
Uploading context
Step 1 : FROM ubuntu
Pulling repository ubuntu
8dbd9e392a96: Pulling image (precise) from ubuntu, endpoint: https://cdn-registry-1.docker.io/v1/
8dbd9e392a96: Download complete
b750fe79269d: Download complete
27cf78414709: Download complete
 ---> 8dbd9e392a96
Successfully built 8dbd9e392a96
vagrant@precise64:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              12.04               8dbd9e392a96        8 months ago        128 MB
ubuntu              latest              8dbd9e392a96        8 months ago        128 MB
ubuntu              precise             8dbd9e392a96        8 months ago        128 MB
ubuntu              12.10               b750fe79269d        9 months ago        175.3 MB
ubuntu              quantal             b750fe79269d        9 months ago        175.3 MB

docker buildで複数のubuntuイメージをダウンロードできました。buildコマンドを利用するDockerfileよりダウンロードが行えるみたいです。他にもdocker searchでイメージを検索しdocker pullで任意のイメージをダウンロードできるみたいです。試しにdocker search centosを実行すると

vagrant@precise64:~$ docker search centos
NAME                                     DESCRIPTION                                     STARS     OFFICIAL   TRUSTED
dustin/centos                            Old CentOS is old.                              0
chentoo/centos                           centos 5.6 x86_64 image                         0
hnakamur/centos                          CentOS 6.5 x86_64 base image                    0
tutum/centos                             CentOS Docker image with SSH access             5
blalor/centos                            Bare-bones base CentOS 6.5 image                0                    [OK]
tianon/centos                            CentOS 5 and 6, created using rinse instea...   2
backjlack/centos                         This repository contains the following ima...   0
tenforward/centos-i386                   CentOS 6 32bit image                            0
mrunalp/centos-jbossas                   CentOS 6/Open JDK 7/Jboss AS 7 image.           0
tchap/centos-epel                        The official centos image having EPEL adde...   0
jathanism/centos-python                  docker run -i -t -h myhostname jathanism/c...   0
tutum/centos-6.4                         CentOS 6.4 image with SSH access. Check th...   1                    [OK]
poklet/centos-java                       A docker build of the latest Centos releas...   0                    [OK]
hansode/centos-6-x86_64                  * centos-6.5 minimal * openssh openssh-cli...   1
centos                                                                                   13
slantview/centos-chef-solo               CentOS 6.4 with chef-solo.                      0
mizzy/centos-4.8-i386                    CentOS 4.8 32bit core image                     0
calebcase/salt-master-centos             A Salt Master installed on CentOS.              0
zenfractal/centos-cdh4-hdfs              CentOS image with Cloudera's CDH4 HDFS pac...   0
gerardorochin/centos-development-tools   Clean CentOS 6.4-x86_64 base and installed...   0
hansode/centos-6.5-x86_64                * centos-6.5 minimal * openssh openssh-cli...   1
hansode/centos-6.4-x86_64                * centos-6.4 minimal * openssh openssh-cli...   0
xujinzheng/centos-erlang-kerl            Erlang R16B02 and kerl on CentOS release 6...   0

というようにいろいろ出てきてどれがいいのかわかりません。TRUSTEDにOKと書いてあるものが信頼性の高いものなのかもしれません。ググったところcentosというものが公式みたいです。
下記がwikiです。
https://github.com/dotcloud/docker/wiki/Public-docker-images#centos-64-base-image


★参考サイト
http://apatheia.info/blog/2013/06/17/docker/
http://zinrai.github.io/blog/html/2013/11/28/ubuntu_docker.html

④コンテナを作成する

vagrant@precise64:~$ docker run -i -t -d --name ubuntu01 ubuntu:12.04 /bin/bash
067de2413a7a2edd45389beb21252933f89c7e5c1f975f5148a2cb7e1d73e9f1

起動していることを確認します。

vagrant@precise64:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED
067de2413a7a        ubuntu:12.04        /bin/bash           6 seconds ago

コンテナが作成されたみたいです。runコマンドに-dオプションをつけると、backgroundでコンテナを動かすようです。runコマンドのオプションについてはdocker runをたたけば分かります。

⑤コンテナにログインする

次は実際に端末にログインしてみます。

vagrant@precise64:~$ docker run -i -t ubuntu:12.04 /bin/bash
root@4c1c644f289e:/#

ログインできました。
pingをインストールしてみます。

root@4c1c644f289e:/# apt-get install iputils-ping
Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
  iputils-ping
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 56.1 kB of archives.
After this operation, 143 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main iputils-ping amd64 3:20101006-1ubuntu1 [56.1 kB]
Fetched 56.1 kB in 1s (47.9 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package iputils-ping.
(Reading database ... 7545 files and directories currently installed.)
Unpacking iputils-ping (from .../iputils-ping_3%3a20101006-1ubuntu1_amd64.deb) ...
Setting up iputils-ping (3:20101006-1ubuntu1) ...

うまくいきました。
dockerを使うとすぐに環境を作れるので、検証用環境とか作るのに便利そうですね!DockerFileに必要なソフトをインストールするように指定できるみたいなので、ここをどんどん編集していけばいいのかも。

オリジナルのイメージファイルを作りたい場合はcommitコマンドでイメージの作成が可能みたいです。早めに自分用のイメージファイルを作っておきたいですね。

次はchef・puppetあたりも試してみたいと思います。

★参考サイト
http://blog.n-z.jp/blog/2013-12-13-docker-custom-base-image.html