[ Docker CE 対応 Update ] Vagrant + CentOS7 で最新 Docker 環境をつくるメモ

# Vagrant + CentOS7 で最新 Docker 環境をつくるメモ

ref. https://docs.docker.com/engine/installation/linux/centos/

## History:

- 2017/05/31
    - CentOS 7.3 対応
    - Docker CE が動くように更新
    - bootstrap 変更
    - /etc/docker/daemon.json を置けと手順にあったのでローカルから転送する 

## 目的:

* 自分の備忘録
* 手元で出来るだけ手間をかけずに docker 環境を作りたい
* 手抜きなのでより精査したい方はよしなに…
  * ~~eth1 が上がってないとか、ip_forwarding が有効でないとかあまりにも杜撰だったので加筆修正しましたm(_ _)m (2016/06/18 20:52)~~

## 構成:

* Mac OS X El Capitan
* vagrant 1.8.1

```shell-session
% vagrant --version
Vagrant 1.9.4
```

* Virtual Box

```shell-session
% /Applications/VirtualBox.app/Contents/MacOS/VBoxHeadless --version
Oracle VM VirtualBox Headless Interface 5.1.22
(C) 2008-2017 Oracle Corporation
All rights reserved.

5.1.22r115126
```

* CentOS7 Box (yum update 後)

```shell-session
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
```

## 1. 準備

### 1-1. Vagrant File & bootstrap.sh

```shell-session
% mkdir -p ~/vagrant/docker/centos7
% cd ~/vagrant/docker/centos7
% vagrant init centos/7 -m
% ls -al Vagrantfile
```
```shell-session
% cat << 'EOF' > bootstrap.sh
#!/usr/bin/env bash

grep 'NM_CONTROLLED=no' /etc/sysconfig/network-scripts/ifcfg-eth1 && sed -i -e 's/NM_CONTROLLED=no/NM_CONTROLLED=yes/' /etc/sysconfig/network-scripts/ifcfg-eth1
grep 'BOOTPROTO=none' /etc/sysconfig/network-scripts/ifcfg-eth1 && sed -i -e 's/BOOTPROTO=none/BOOTPROTO=static/' /etc/sysconfig/network-scripts/ifcfg-eth1

cat /etc/sysconfig/network-scripts/ifcfg-eth1

service network restart

ipforward=`sudo sysctl -a | grep "net.ipv4.ip_forward =" | awk -F " = " '{print $2}'`
ipforward_count=`grep 'net.ipv4.ip_forward' /etc/sysctl.conf | wc -l`

if [ $ipforward = "0" ] && [ $ipforward_count = "1" ];
then
  sed -i -e 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/' /etc/sysctl.conf
elif [ $ipforward = "0" ] && [ $ipforward_count = "0" ];
then
  echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
else
  echo "already configured"
fi

cat /etc/sysctl.conf

sysctl -p /etc/sysctl.conf

sudo mkdir /etc/docker && \
sudo chown root:root /home/vagrant/daemon.json && \
sudo mv -iv /home/vagrant/daemon.json /etc/docker/. && \
sudo yum -y update && \
sudo yum -y install yum-utils device-mapper-persistent-data lvm2 && \
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo && \
sudo yum makecache fast && \
sudo yum -y install docker-ce && \
sudo systemctl start docker && \
sudo docker run hello-world && \
sudo systemctl enable docker.service

EOF
cat bootstrap.sh
```
```shell-session
% IPADDR=172.28.128.100
% echo $IPADDR

% cat << EOF > daemon.json
{
  "storage-driver": "devicemapper"
}
EOF
cat daemon.json

% cat << EOF > Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "centos/7"
  config.vm.network "private_network", ip: "$IPADDR"
  config.vm.provision "file", source: "./daemon.json", destination: "~/daemon.json"
  config.vm.provision :shell, path: "bootstrap.sh"
end
EOF
cat Vagrantfile
```

## 2. Box の起動

### 2-1. OS 起動

```shell-session
% vagrant up
```

### 2-2. Docker 正常性確認(その1)

* bootstrap の中に組み込んでた hello-world コンテナの出力

```shell-session
==> default: Hello from Docker.
==> default: This message shows that your installation appears to be working correctly.
==> default:
==> default: To generate this message, Docker took the following steps:
==> default:  1. The Docker client contacted the Docker daemon.
==> default:  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
==> default:  3. The Docker daemon created a new container from that image which runs the
==> default:     executable that produces the output you are currently reading.
==> default:  4. The Docker daemon streamed that output to the Docker client, which sent it
==> default:     to your terminal.
==> default:
==> default: To try something more ambitious, you can run an Ubuntu container with:
==> default:  $ docker run -it ubuntu bash
==> default:
==> default: Share images, automate workflows, and more with a free Docker Hub account:
==> default:  https://hub.docker.com
==> default:
==> default: For more examples and ideas, visit:
==> default:  https://docs.docker.com/engine/userguide/
```

### 2-3. Docker 正常性確認(その2)

* ログイン確認

```shell-session
% vagrant ssh
```

```shell-session
[vagrant@localhost ~]$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
80c7f5bcebb4        hello-world         "/hello"            21 seconds ago      Exited (0) 21 seconds ago                       jovial_nobel
```
```shell-session
[vagrant@localhost ~]$ sudo docker info
Containers: 1
 Running: 0
 Paused: 0
 Stopped: 1
Images: 1
Server Version: 17.03.1-ce
Storage Driver: devicemapper
(snip)
```

### 2-4. OS 正常性確認

```shell-session
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

[vagrant@localhost ~]$ ip a
[vagrant@localhost ~]$ ip a s eth0
[vagrant@localhost ~]$ ip a s eth1
[vagrant@localhost ~]$ ip route show

[vagrant@localhost ~]$ nmcli con show
[vagrant@localhost ~]$ nmcli dev status

[vagrant@localhost ~]$ sudo systemctl status docker.service

[vagrant@localhost ~]$ sudo sysctl -a | grep "net.ipv4.ip_forward ="
net.ipv4.ip_forward = 1
```

## 3. 最初のコンテナの起動

```shell-session
[vagrant@localhost ~]$ sudo docker run -i --name ubuntu_test01 -t ubuntu:trusty /bin/bash
root@a733da598aa0:/#
```

* exit するとホストのプロンプトに戻れますが、コンテナも終了します
* コンテナを起動させたまま、ホストのプロンプトに戻るには以下のキーアサインでデタッチします

```
Ctrl 押したまま p -> q
```

* コンテナが起動したままか確認します

```shell-session
[vagrant@localhost ~]$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a733da598aa0        ubuntu:trusty       "/bin/bash"         6 minutes ago       Up 6 minutes                            ubuntu_test01
```

* 再度コンテナ内に入って作業したいときはアタッチします

```shell-session
[vagrant@localhost ~]$ sudo docker attach ubuntu_test01
root@a733da598aa0:/#

root@a733da598aa0:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"
```