Vagrantを使いつつ、Dockerに移行してみる

半年前、ここでこれとかこれでVagrantについて書いたけど途中で書かなくなってしまい、今度は「Docker、今やるしかないじゃない」の気分になったので、飽きるまでDockerについて書いてみます。

OSC 2016で今さら聞けない人のためのDocker超入門を聞いて、「へー」とか「ほー」とか思ったレベルのDocker初心者なので、軽いところからじわじわやっていきます。

ベースのOSを探していたら「CentOS Atomic Host」というDockerに特化していて、かつ、CentOSと相性が良さそうなOSがあったので、今回はVagrantでCentOS Atomic Hostを入れて、その上にDockerを使ってCentOSのコンテナを立てる、を試してみようと思います。

VagrantのImageをダウンロードする・・・が

CentOS Atomic HostのVagrant Image(box)はhttps://wiki.centos.org/SpecialInterestGroup/Atomic/Download/からダウンロードできる。

できるんだけど、今回はboxなし状態でVagrant Cloudからboxが勝手に落ちてきて、initしてupするところも試してみる。

インストールされているVagrantの確認

今まではWindowsだったけど、今回はMacでやってみる。

$ vagrant --version
Vagrant 1.6.5
$ vagrant plugin list
vagrant-login (1.0.1, system)
vagrant-omnibus (1.4.1)
vagrant-share (1.1.1, system)

※ 古すぎるため、後々問題が発生します。

2016/02/28現在、Vagrantは1.8.1、VirtualBoxは5.0.14が最新です。

Vagrant Cloudのアカウントを作る

Vagrant Cloud

アカウントを持ってない場合は、https://atlas.hashicorp.com/account/newでアカウントを作成する。

次に、ターミナルからVagrant Cloudにログインしてみる。

$ vagrant login
In a moment we'll ask for your username and password to Vagrant Cloud.
After authenticating, we will store an access token locally. Your
login details will be transmitted over a secure connection, and are
never stored on disk locally.

If you don't have a Vagrant Cloud account, sign up at vagrantcloud.com

Username or Email: XXXX
Password (will be hidden):
You're now logged in!

おk。

vagrant init → vagrant up

通常のinitをする前と同じように、Vagrantfileを設置するディレクトリを作成し、移動してから次のコマンドを入力。

$ vagrant init centos/atomic-host && vagrant up --provider virtualbox
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.
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/atomic-host' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
・
[省略]
・
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in the
'poweroff' state. Please verify everything is configured
properly and try again.

If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve.
For example, if you're using VirtualBox, run `vagrant up` while the
VirtualBox GUI is open.

エラーで起動できませんでした。
GUIを見ながらupしてもよくわからず。

ここでVagrantのバージョンが1.6.5、VirtualBoxのバージョンが4.3というとても古い環境だったことに気付いてしまったので、速攻でVagrantは1.8.1、VirtualBoxは5.0.14にしてみる。
すでにいくつか仮想環境は作ってあったけど、過去は振り返らない。

再びバージョン確認とプラグイン確認。
ついでにsaharaもインストールしてスナップショットとロールバックが使えるようにしておく。
vagrant-vbguestもインストールしたかもしれないが、勢いでやったので覚えてない。

$ vagrant --version
Vagrant 1.8.1
$ vagrant plugin list
sahara (0.0.17)
vagrant-omnibus (1.4.1)
vagrant-share (1.1.5, system)
vagrant-vbguest (0.11.0)

プラグインが減った気がするが、vagrant loginコマンドは使えたので、vagrant-loginは本体に組み込まれた模様。
saharaもインストールしたので、これで何でもやり放題な環境ができた。

upする前に、コメントアウトされているネットワーク設定のコメントを解除しておく。
up済みの場合は一度haltで落としておく。

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

もう一発upしてみる。

% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/atomic-host' 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: 22 (guest) => 2222 (host) (adapter 1)
==> 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:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
No installation found.
The guest's platform ("atomic") is currently not supported, will try generic Linux method...
Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
mount: /dev/loop0 is write-protected, mounting read-only
Installing Virtualbox Guest Additions 5.0.14 - guest version is unknown
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.14 Guest Additions for Linux............
VirtualBox Guest Additions installer
Copying additional installer modules ...
ln: failed to create symbolic link ‘/usr/bin/VBoxClient’: Read-only file system
ln: failed to create symbolic link ‘/usr/bin/VBoxControl’: Read-only file system
ln: failed to create symbolic link ‘/usr/sbin/vbox-greeter’: Read-only file system
ln: failed to create symbolic link ‘/usr/sbin/VBoxService’: Read-only file system
ln: failed to create symbolic link ‘/lib64/VBoxGuestAdditions’: Read-only file system
ln: failed to create symbolic link ‘/lib64/VBoxOGLarrayspu.so’: Read-only file system
ln: failed to create symbolic link ‘/lib64/VBoxOGLcrutil.so’: Read-only file system
ln: failed to create symbolic link ‘/lib64/VBoxOGLerrorspu.so’: Read-only file system
ln: failed to create symbolic link ‘/lib64/VBoxOGLfeedbackspu.so’: Read-only file system
ln: failed to create symbolic link ‘/lib64/VBoxOGLpackspu.so’: Read-only file system
ln: failed to create symbolic link ‘/lib64/VBoxOGLpassthroughspu.so’: Read-only file system
ln: failed to create symbolic link ‘/lib64/VBoxOGL.so’: Read-only file system
ln: failed to create symbolic link ‘/usr/lib/VBoxGuestAdditions’: Read-only file system
ln: failed to create symbolic link ‘/usr/share/VBoxGuestAdditions’: Read-only file system
ln: failed to create symbolic link ‘/usr/src/vboxguest-5.0.14’: Read-only file system
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[ OK ]
Building the VirtualBox Guest Additions kernel modules
The gcc utility was not found. If the following module compilation fails then
this could be the reason and you should try installing it.

The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.
The missing package can be probably installed with
yum install kernel-devel-3.10.0-327.10.1.el7.x86_64

Building the main Guest Additions 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]
(Invalid Guest Additions configuration found)
An error occurred during installation of VirtualBox Guest Additions 5.0.14. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /Users/syan/dev/Vagrant/centos_atomic-host/ => /home/vagrant/sync

長いけど、とりあえずCentOS Atomic Hostが無事起動した様子。

sshで入って、アップデートをかけてみる。

[vagrant@localhost ~]$ sudo rpm-ostree upgrade
Updating from: centos-atomic-host:centos-atomic-host/7/x86_64/standard

1 metadata, 0 content objects fetched; 313 B transferred in 5 seconds
No upgrade available.

特になさそう。

Dockerでコンテナを立てる

では、CentOSのDocker Imageを引っぱってみます。
バージョン指定なしで、そのOSの最新バージョンのimageが引っぱれる。

[vagrant@localhost ~]$ sudo docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 1987 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8... 15 [OK]
・
[省略]
・
[vagrant@localhost ~]$ sudo docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ... latest: Pulling from library/centos
47d44cb6f252: Pull complete
6fdebd7b0eb5: Pull complete
a63aae4d216f: Pull complete
bb3d629a7cbc: Pull complete
Digest: sha256:381f21e4c7b3724c6f420b2bcfa6e13e47ed155192869a2a04fa10f944c78476
Status: Downloaded newer image for docker.io/centos:latest

[vagrant@localhost ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/centos latest bb3d629a7cbc 2 days ago 196.6 MB

こんな記事もあるので、私もコンテナを立てつつ最速でhttpdを立ててみる。

[vagrant@localhost ~]$ sudo docker run --privileged -h httpd -d -p 80:80 --name httpd centos /sbin/init
e2124b00668ec41a2f50a6e38b054c2e57e1d1ee9da38ac68b543ba8d8b31b4d
[vagrant@localhost ~]$ sudo docker exec -it httpd /bin/bash
[root@httpd /]# yum -y install httpd
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos for package: httpd-2.4.6-40.el7.centos.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-40.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================
 Package                  Arch               Version                         Repository        Size
====================================================================================================
Installing:
 httpd                    x86_64             2.4.6-40.el7.centos             base             2.7 M
Installing for dependencies:
 apr                      x86_64             1.4.8-3.el7                     base             103 k
 apr-util                 x86_64             1.5.2-6.el7                     base              92 k
 centos-logos             noarch             70.0.6-3.el7.centos             base              21 M
 httpd-tools              x86_64             2.4.6-40.el7.centos             base              82 k
 mailcap                  noarch             2.1.41-2.el7                    base              31 k

Transaction Summary
====================================================================================================
Install  1 Package (+5 Dependent packages)

Total download size: 24 M
Installed size: 31 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/apr-1.4.8-3.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for apr-1.4.8-3.el7.x86_64.rpm is not installed
(1/6): apr-1.4.8-3.el7.x86_64.rpm                                            | 103 kB  00:00:03
(2/6): httpd-tools-2.4.6-40.el7.centos.x86_64.rpm                            |  82 kB  00:00:04
(3/6): mailcap-2.1.41-2.el7.noarch.rpm                                       |  31 kB  00:00:02
(4/6): apr-util-1.5.2-6.el7.x86_64.rpm                                       |  92 kB  00:00:05
(5/6): centos-logos-70.0.6-3.el7.centos.noarch.rpm                           |  21 MB  00:00:28
(6/6): httpd-2.4.6-40.el7.centos.x86_64.rpm                                  | 2.7 MB  00:00:46
----------------------------------------------------------------------------------------------------
Total                                                               538 kB/s |  24 MB  00:00:46
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-2.1511.el7.centos.2.10.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-3.el7.x86_64                                                           1/6
warning: Failed to open SELinux handle.
  Installing : apr-util-1.5.2-6.el7.x86_64                                                      2/6
  Installing : httpd-tools-2.4.6-40.el7.centos.x86_64                                           3/6
  Installing : centos-logos-70.0.6-3.el7.centos.noarch                                          4/6
  Installing : mailcap-2.1.41-2.el7.noarch                                                      5/6
  Installing : httpd-2.4.6-40.el7.centos.x86_64                                                 6/6

なぜかhttpdのインストールが終わらず(´・ω:;.:…

…とりあえずコンテナたったし、まぁいいか。
続く・・・かもしれないし、もう書かないかもしれない。