boot2docker上でnginxを立ち上げてみる


# boot2docker上に各種サーバを立ち上げるシリーズ

## まえがき

[前回](http://qiita.com/sudahiroshi/items/ecd5a81d923b6d9c2672)は，MacOS X + boot2dockerで共有フォルダを使えるようになりましたので，今回から各種サーバを立ち上げてみたいと思います．また，せっかくWebサーバを起動するので，WebのデータやログはMacOS X上のものを使いたいと思います．


簡単なネットワーク構成は以下のようになります．ここで，MacOS Xとboot2docker間（設定上はNAT）はVirtualBoxによるポートフォワードを，boot2dockerとnginx間はdockerによるポートフォワードをそれぞれ使います．
OS X - boot2docker( VirtualBox ) - nginx( docker )

## 手順1：boot2dockerの起動

まずはboot2dockerを起動します．標準のイメージではVirtualBoxの共有フォルダを使えないので，前回同様dduportal様が公開しているイメージを，Vagrantを使い起動します．


```bash:手順1
suda@sahara:~/boot2docker$ 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 => /Users/suda/boot2docker
==> 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:~/boot2docker$
```

## 手順2：VirtualBoxのポートフォワードの設定

何故かポートフォワードの設定が，boot2dockerを起動する度にクリアされるので，仕方なく毎回手動設定することにします．今回は実験のため，OS X上の8080番ポートから，boot2docker上の80番ポートにフォワードすることにします．詳しくは[こちら](http://qiita.com/hnakamur/items/3640825cf2c5d2d4a52a)を御覧ください．

また，仮想計算機名が長ったらしいですが，vagrantにより命名されたものです．変更しても問題無いと思います（が，何らかの不具合があっても感知しません）．

以下，設定と確認です．これによると，NIC 1のRule(1)に，今設定したポートフォワードが表示されています．

```bash:手順2
suda@sahara:~/boot2docker$ VBoxManage controlvm "boot2docker_default_1400232204582_96247" natpf1 "http,tcp,127.0.0.1,8080,,80"
suda@sahara:~/boot2docker$ VBoxManage showvminfo "boot2docker_default_1400232204582_96247" | grep "^NIC"
NIC 1:           MAC: 08002793FCF5, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none
NIC 1 Settings:  MTU: 0, Socket (send: 64, receive: 64), TCP Window (send:64, receive: 64)
NIC 1 Rule(0):   name = 4f9f1756-120d-4205-b097-bf0861424472, protocol = tcp, host ip = , host port = 4243, guest ip = , guest port = 4243
NIC 1 Rule(1):   name = http, protocol = tcp, host ip = 127.0.0.1, host port = 8080, guest ip = , guest port = 80
NIC 1 Rule(2):   name = ssh, protocol = tcp, host ip = 127.0.0.1, host port = 2222, guest ip = , guest port = 22
NIC 2:           disabled
NIC 3:           disabled
NIC 4:           disabled
NIC 5:           disabled
NIC 6:           disabled
NIC 7:           disabled
NIC 8:           disabled
suda@sahara:~/boot2docker$
```

もし，httpsも追加したい場合は，続けて以下のように実行してください．この例では8443→443にポートフォワードしています．natpf1の次の項目で，ポートフォワードの名前を上記設定と別の名称にしてください．ここではhttpsにしています．

```bash:httpsも設定してみる
suda@sahara:~/boot2docker$ VBoxManage controlvm "boot2docker_default_1400232204582_96247" natpf1 "https,tcp,127.0.0.1,8443,,443"
suda@sahara:~/boot2docker$
```


## 手順3：DockerfileとDockerイメージを準備する

続いてDockerfileを作ります．今回は練習ということで，[こちら](https://github.com/dockerfile/nginx)を参考にしました．Ubuntuをベースにしているので，インストール作業はapt-getで済ませています．また，共有フォルダの設定として，コンテンツ，設定ファイル，ログファイルを置くディレクトリを指定しています．

```bash:~/Docker/nginx/Dockerfile
#
# Nginx
#
#

# Pull base image.
FROM ubuntu

# Install Nginx.
RUN apt-get update
RUN apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN chown -R www-data:www-data /var/lib/nginx

# Define mountable directories
VOLUME [ "/usr/share/nginx/html", "/etc/nginx/sites-enabled", "/var/log/nginx" ]

# Define working directory.
WORKDIR /etc/nginx

# Define default command.
CMD [ "nginx" ]

# Expose ports.
EXPOSE 80
EXPOSE 443
```

ここまで来たら，Dockerイメージを作りましょう．ここで注意事項ですが，実は上記ファイルを作成した時点で「VOLUME」を「VOLUM」と恥ずかしいスペルミスしていて，以下の実行例に残っています．もう一度やり直すと，ディスク上にキャッシュされたデータが使われてしまうので，そのまま掲載します．

```bash:手順3（dockerイメージを作る)
suda@sahara:~/Docker/nginx$ docker build .
Uploading context  2.56 kB
Uploading context
Step 0 : FROM ubuntu
 ---> 99ec81b80c55
Step 1 : RUN apt-get update
 ---> Running in 7b850384dbb4
Ign http://archive.ubuntu.com trusty InRelease
Ign http://archive.ubuntu.com trusty-updates InRelease
Ign http://archive.ubuntu.com trusty-security InRelease
Get:1 http://archive.ubuntu.com trusty Release.gpg [933 B]
Get:2 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]
Get:3 http://archive.ubuntu.com trusty-security Release.gpg [933 B]
Get:4 http://archive.ubuntu.com trusty Release [58.5 kB]
Get:5 http://archive.ubuntu.com trusty-updates Release [58.5 kB]
Get:6 http://archive.ubuntu.com trusty-security Release [58.5 kB]
Get:7 http://archive.ubuntu.com trusty/main Sources [1064 kB]
Get:8 http://archive.ubuntu.com trusty/restricted Sources [5433 B]
Get:9 http://archive.ubuntu.com trusty/universe Sources [6399 kB]
Get:10 http://archive.ubuntu.com trusty/main amd64 Packages [1350 kB]
Get:11 http://archive.ubuntu.com trusty/restricted amd64 Packages [13.0 kB]
Get:12 http://archive.ubuntu.com trusty/universe amd64 Packages [5859 kB]
Get:13 http://archive.ubuntu.com trusty-updates/main Sources [34.1 kB]
Get:14 http://archive.ubuntu.com trusty-updates/restricted Sources [14 B]
Get:15 http://archive.ubuntu.com trusty-updates/universe Sources [20.0 kB]
Get:16 http://archive.ubuntu.com trusty-updates/main amd64 Packages [82.3 kB]
Get:17 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [14 B]
Get:18 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [50.1 kB]
Get:19 http://archive.ubuntu.com trusty-security/main Sources [14.7 kB]
Get:20 http://archive.ubuntu.com trusty-security/restricted Sources [14 B]
Get:21 http://archive.ubuntu.com trusty-security/universe Sources [2873 B]
Get:22 http://archive.ubuntu.com trusty-security/main amd64 Packages [47.0 kB]
Get:23 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [14 B]
Get:24 http://archive.ubuntu.com trusty-security/universe amd64 Packages [14.6 kB]
Fetched 15.1 MB in 22s (670 kB/s)
Reading package lists...
 ---> 8baf0beb0044
Removing intermediate container 7b850384dbb4
Step 2 : RUN apt-get install -y nginx
 ---> Running in ccb268b16ada
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
  libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
  libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 nginx-common nginx-core sgml-base xml-core
Suggested packages:
  libgd-tools geoip-bin fcgiwrap nginx-doc sgml-base-doc debhelper
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
  libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
  libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 nginx nginx-common nginx-core sgml-base xml-core
0 upgraded, 25 newly installed, 0 to remove and 10 not upgraded.
Need to get 5612 kB of archives.
After this operation, 19.8 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libgeoip1 amd64 1.6.0-1 [71.0 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libxau6 amd64 1:1.0.8-1 [8376 B]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/main libxdmcp6 amd64 1:1.1.1-1 [12.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libxcb1 amd64 1.10-2ubuntu1 [38.0 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ trusty/main libx11-data all 2:1.6.2-1ubuntu2 [111 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ trusty/main libx11-6 amd64 2:1.6.2-1ubuntu2 [560 kB]
Get:7 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libxml2 amd64 2.9.1+dfsg1-3ubuntu4.1 [570 kB]
Get:8 http://archive.ubuntu.com/ubuntu/ trusty/main sgml-base all 1.26+nmu4ubuntu1 [12.5 kB]
Get:9 http://archive.ubuntu.com/ubuntu/ trusty/main fonts-dejavu-core all 2.34-1ubuntu1 [1024 kB]
Get:10 http://archive.ubuntu.com/ubuntu/ trusty/main fontconfig-config all 2.11.0-0ubuntu4 [47.3 kB]
Get:11 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libfreetype6 amd64 2.5.2-1ubuntu2.1 [305 kB]
Get:12 http://archive.ubuntu.com/ubuntu/ trusty/main libfontconfig1 amd64 2.11.0-0ubuntu4 [123 kB]
Get:13 http://archive.ubuntu.com/ubuntu/ trusty/main libjpeg-turbo8 amd64 1.3.0-0ubuntu2 [104 kB]
Get:14 http://archive.ubuntu.com/ubuntu/ trusty/main libjpeg8 amd64 8c-2ubuntu8 [2194 B]
Get:15 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libjbig0 amd64 2.0-2ubuntu4.1 [26.1 kB]
Get:16 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libtiff5 amd64 4.0.3-7ubuntu0.1 [142 kB]
Get:17 http://archive.ubuntu.com/ubuntu/ trusty/main libvpx1 amd64 1.3.0-2 [556 kB]
Get:18 http://archive.ubuntu.com/ubuntu/ trusty/main libxpm4 amd64 1:3.5.10-1 [38.3 kB]
Get:19 http://archive.ubuntu.com/ubuntu/ trusty/main libgd3 amd64 2.1.0-3 [147 kB]
Get:20 http://archive.ubuntu.com/ubuntu/ trusty/main libxslt1.1 amd64 1.1.28-2build1 [145 kB]
Get:21 http://archive.ubuntu.com/ubuntu/ trusty/main geoip-database all 20140313-1 [1196 kB]
Get:22 http://archive.ubuntu.com/ubuntu/ trusty/main xml-core all 0.13+nmu2 [23.3 kB]
Get:23 http://archive.ubuntu.com/ubuntu/ trusty/main nginx-common all 1.4.6-1ubuntu3 [18.1 kB]
Get:24 http://archive.ubuntu.com/ubuntu/ trusty/main nginx-core amd64 1.4.6-1ubuntu3 [324 kB]
Get:25 http://archive.ubuntu.com/ubuntu/ trusty/main nginx all 1.4.6-1ubuntu3 [5586 B]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin:
Fetched 5612 kB in 17s (321 kB/s)
Selecting previously unselected package libgeoip1:amd64.
(Reading database ... 11518 files and directories currently installed.)
Preparing to unpack .../libgeoip1_1.6.0-1_amd64.deb ...
Unpacking libgeoip1:amd64 (1.6.0-1) ...
Selecting previously unselected package libxau6:amd64.
Preparing to unpack .../libxau6_1%3a1.0.8-1_amd64.deb ...
Unpacking libxau6:amd64 (1:1.0.8-1) ...
Selecting previously unselected package libxdmcp6:amd64.
Preparing to unpack .../libxdmcp6_1%3a1.1.1-1_amd64.deb ...
Unpacking libxdmcp6:amd64 (1:1.1.1-1) ...
Selecting previously unselected package libxcb1:amd64.
Preparing to unpack .../libxcb1_1.10-2ubuntu1_amd64.deb ...
Unpacking libxcb1:amd64 (1.10-2ubuntu1) ...
Selecting previously unselected package libx11-data.
Preparing to unpack .../libx11-data_2%3a1.6.2-1ubuntu2_all.deb ...
Unpacking libx11-data (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libx11-6:amd64.
Preparing to unpack .../libx11-6_2%3a1.6.2-1ubuntu2_amd64.deb ...
Unpacking libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libxml2:amd64.
Preparing to unpack .../libxml2_2.9.1+dfsg1-3ubuntu4.1_amd64.deb ...
Unpacking libxml2:amd64 (2.9.1+dfsg1-3ubuntu4.1) ...
Selecting previously unselected package sgml-base.
Preparing to unpack .../sgml-base_1.26+nmu4ubuntu1_all.deb ...
Unpacking sgml-base (1.26+nmu4ubuntu1) ...
Selecting previously unselected package fonts-dejavu-core.
Preparing to unpack .../fonts-dejavu-core_2.34-1ubuntu1_all.deb ...
Unpacking fonts-dejavu-core (2.34-1ubuntu1) ...
Selecting previously unselected package fontconfig-config.
Preparing to unpack .../fontconfig-config_2.11.0-0ubuntu4_all.deb ...
Unpacking fontconfig-config (2.11.0-0ubuntu4) ...
Selecting previously unselected package libfreetype6:amd64.
Preparing to unpack .../libfreetype6_2.5.2-1ubuntu2.1_amd64.deb ...
Unpacking libfreetype6:amd64 (2.5.2-1ubuntu2.1) ...
Selecting previously unselected package libfontconfig1:amd64.
Preparing to unpack .../libfontconfig1_2.11.0-0ubuntu4_amd64.deb ...
Unpacking libfontconfig1:amd64 (2.11.0-0ubuntu4) ...
Selecting previously unselected package libjpeg-turbo8:amd64.
Preparing to unpack .../libjpeg-turbo8_1.3.0-0ubuntu2_amd64.deb ...
Unpacking libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Selecting previously unselected package libjpeg8:amd64.
Preparing to unpack .../libjpeg8_8c-2ubuntu8_amd64.deb ...
Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
Selecting previously unselected package libjbig0:amd64.
Preparing to unpack .../libjbig0_2.0-2ubuntu4.1_amd64.deb ...
Unpacking libjbig0:amd64 (2.0-2ubuntu4.1) ...
Selecting previously unselected package libtiff5:amd64.
Preparing to unpack .../libtiff5_4.0.3-7ubuntu0.1_amd64.deb ...
Unpacking libtiff5:amd64 (4.0.3-7ubuntu0.1) ...
Selecting previously unselected package libvpx1:amd64.
Preparing to unpack .../libvpx1_1.3.0-2_amd64.deb ...
Unpacking libvpx1:amd64 (1.3.0-2) ...
Selecting previously unselected package libxpm4:amd64.
Preparing to unpack .../libxpm4_1%3a3.5.10-1_amd64.deb ...
Unpacking libxpm4:amd64 (1:3.5.10-1) ...
Selecting previously unselected package libgd3:amd64.
Preparing to unpack .../libgd3_2.1.0-3_amd64.deb ...
Unpacking libgd3:amd64 (2.1.0-3) ...
Selecting previously unselected package libxslt1.1:amd64.
Preparing to unpack .../libxslt1.1_1.1.28-2build1_amd64.deb ...
Unpacking libxslt1.1:amd64 (1.1.28-2build1) ...
Selecting previously unselected package geoip-database.
Preparing to unpack .../geoip-database_20140313-1_all.deb ...
Unpacking geoip-database (20140313-1) ...
Selecting previously unselected package xml-core.
Preparing to unpack .../xml-core_0.13+nmu2_all.deb ...
Unpacking xml-core (0.13+nmu2) ...
Selecting previously unselected package nginx-common.
Preparing to unpack .../nginx-common_1.4.6-1ubuntu3_all.deb ...
Unpacking nginx-common (1.4.6-1ubuntu3) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../nginx-core_1.4.6-1ubuntu3_amd64.deb ...
Unpacking nginx-core (1.4.6-1ubuntu3) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.4.6-1ubuntu3_all.deb ...
Unpacking nginx (1.4.6-1ubuntu3) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up libgeoip1:amd64 (1.6.0-1) ...
Setting up libxau6:amd64 (1:1.0.8-1) ...
Setting up libxdmcp6:amd64 (1:1.1.1-1) ...
Setting up libxcb1:amd64 (1.10-2ubuntu1) ...
Setting up libx11-data (2:1.6.2-1ubuntu2) ...
Setting up libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Setting up libxml2:amd64 (2.9.1+dfsg1-3ubuntu4.1) ...
Setting up sgml-base (1.26+nmu4ubuntu1) ...
Setting up fonts-dejavu-core (2.34-1ubuntu1) ...
Setting up fontconfig-config (2.11.0-0ubuntu4) ...
Setting up libfreetype6:amd64 (2.5.2-1ubuntu2.1) ...
Setting up libfontconfig1:amd64 (2.11.0-0ubuntu4) ...
Setting up libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
Setting up libjbig0:amd64 (2.0-2ubuntu4.1) ...
Setting up libtiff5:amd64 (4.0.3-7ubuntu0.1) ...
Setting up libvpx1:amd64 (1.3.0-2) ...
Setting up libxpm4:amd64 (1:3.5.10-1) ...
Setting up libgd3:amd64 (2.1.0-3) ...
Setting up libxslt1.1:amd64 (1.1.28-2build1) ...
Setting up geoip-database (20140313-1) ...
Setting up xml-core (0.13+nmu2) ...
Setting up nginx-common (1.4.6-1ubuntu3) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up nginx-core (1.4.6-1ubuntu3) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up nginx (1.4.6-1ubuntu3) ...
Processing triggers for libc-bin (2.19-0ubuntu6) ...
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
 ---> 8ca9b35ead69
Removing intermediate container ccb268b16ada
Step 3 : RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
 ---> Running in 0eea2056e6a5
 ---> b1a3804f092a
Removing intermediate container 0eea2056e6a5
Step 4 : RUN chown -R www-data:www-data /var/lib/nginx
 ---> Running in 465ace9e9e1c
 ---> fcee77b2c028
Removing intermediate container 465ace9e9e1c
Step 5 : VOLUM [ "/usr/share/nginx/html", "/etc/nginx/sites-enabled", "/var/log/nginx" ]
# Skipping unknown instruction VOLUM
Step 6 : WORKDIR /etc/nginx
 ---> Running in 7057e29b4d1e
 ---> ad14bb459b60
Removing intermediate container 7057e29b4d1e
Step 7 : CMD [ "nginx" ]
 ---> Running in d9e5dbd25c29
 ---> 53008d634493
Removing intermediate container d9e5dbd25c29
Step 8 : EXPOSE 80
 ---> Running in be51998070fa
 ---> e2ae6ec99d2d
Removing intermediate container be51998070fa
Step 9 : EXPOSE 443
 ---> Running in ecb7821ce31e
 ---> 7d7800f60c8b
Removing intermediate container ecb7821ce31e
Successfully built 7d7800f60c8b
suda@sahara:~/Docker/nginx$
```

長いのでチェックする気も失せますが，最後の「Successfully built 7d7800f60c8b」は重要です．後々，このイメージを起動する際のキーワードになります．なお，本来はきちんと名前を付けるものです．

※以下，上記ミスを修正した状態で実行しているとお考えください．

## 手順4：dockerを起動してみる

はやる気持ちを抑えて，まずは共有フォルダ内にディレクトリとテスト用のHTMLファイルを置いておきます．なお，前回から引き続き，MacOS XのVagrantを起動したフォルダ（~/boot2docker）を，VirtualBoxの/vagrantにマウントしています．ですので，こちらにディレクトリとファイルを作成しておきます．

```bash:手順4：ディレクトリとテスト用のhtmlファイルを用意する
suda@sahara:~/boot2docker$ ls
Vagrantfile
suda@sahara:~/boot2docker$ mkdir www
suda@sahara:~/boot2docker$ mkdir log
suda@sahara:~/boot2docker$ ls -F
Vagrantfile	log/		www/
suda@sahara:~/boot2docker$ cp ~/test.html www

```

ここまで来たら起動するのみです．以下の例では，コンテンツを置くディレクトリ（/usr/share/nginx/html）とログディレクトリ（/var/log/nginx）をマウントさせています．また，お試しが主目的なので「-d」オプションは付けていません．必要に応じて付けてください．


```bash:手順4の続き：Docker上でnginxを起動する
suda@sahara:~/Docker/nginx$ docker run -v /vagrant/www:/usr/share/nginx/html -v /vagrant/log:/var/log/nginx -p 80:80 7d7800f60c8b
```

後はMacOS X上のWebブラウザからアクセスできれば設定完了です．具体的には[http://localhost:8080/](http://localhost:8080/)にアクセスしてみてください．なお，ポート番号はVirtualBoxのポートフォワードの設定により，決定されます．

このとき，nginxの仕様なのか，存在しないはずのindex.htmlにアクセスしても「Welcome to nginx!」と表示されてビックリしました．


## 手順5：ログファイルの確認とトラブルシューティング方法

サーバを起動したからには，きちんとログを確認しましょう．こちらもVagrantを起動したディレクトリになります．

```bash:ログの確認
suda@sahara:~/boot2docker$ cd log
suda@sahara:~/boot2docker/log$ ls -l
total 8
-rw-r--r--  1 suda  staff  802  5 19 18:54 access.log
-rw-r--r--  1 suda  staff    0  5 19 18:49 error.log
suda@sahara:~/boot2docker/log$ cat access.log
10.0.2.2 - - [19/May/2014:09:45:57 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"
10.0.2.2 - - [19/May/2014:09:46:25 +0000] "GET /index.html HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"
10.0.2.2 - - [19/May/2014:09:50:26 +0000] "GET /test.html HTTP/1.1" 200 117 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"
suda@sahara:~/boot2docker/log$ cat error.log
suda@sahara:~/boot2docker/log$
```

きちんと設定が完了していれば良いのですが，うまくいかない場合は以下のようにbashを起動して原因を究明しましょう．その場合は，手順4で起動したdockerを終了させた後に，以下のようにbashを起動することになります．

経験上，Dockerを使う限り，ネットワークの設定か共有フォルダの設定が誤りやすいところです．参考までに，docker上でmountコマンドを実行すると，「none」で始まる行が存在します．これが共有フォルダ機能によってマウントされたものです．

```bash:bashを使って原因を救命する場合の実行例
suda@sahara:~/Docker/nginx$ docker run -v /vagrant/www:/usr/share/nginx/html -v /vagrant/log:/var/log/nginx -p 80:80 -t -i 7d7800f60c8b /bin/bash
root@16777ad81035:/etc/nginx# mount
none on / type aufs (rw,relatime,si=bb82befb7d80377a)
tmpfs on /dev type tmpfs (rw,nosuid,mode=755)
proc on /proc type proc (ro,relatime)
sysfs on /sys type sysfs (ro,relatime)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
/dev/sda on /.dockerinit type ext4 (ro,relatime,data=ordered)
/dev/sda on /etc/resolv.conf type ext4 (ro,relatime,data=ordered)
/dev/sda on /etc/hostname type ext4 (ro,relatime,data=ordered)
/dev/sda on /etc/hosts type ext4 (ro,relatime,data=ordered)
none on /usr/share/nginx/html type vboxsf (rw,nodev,relatime)
none on /var/log/nginx type vboxsf (rw,nodev,relatime)
devpts on /dev/console type devpts (rw,relatime,mode=600,ptmxmode=000)
tmpfs on /proc/kcore type tmpfs (rw,nosuid,mode=755)
root@16777ad81035:/etc/nginx# nginx -v
nginx version: nginx/1.4.6 (Ubuntu)
root@16777ad81035:/etc/nginx#
```


## まとめ

Dockerでbashを起動してコマンドラインに順番にコマンドを入力→終了→コンテナ化も良いのですが，せっかくDockerを使うのだから，他の人がすぐに起動できるようにDockerfileを作って公開しましょう．Dockerfileの書式が分かりづらいかもしれませんが，基本的には入力するコマンドを順に並べたものです．

と，言うわけで，順次各種サーバのDockerfileを公開していきたいと思います．