Docker環境をMacにインストールする

VPSも借りているし、これまで避けてきたが、MacにDocker環境を構築する

Dockerのインストール

Homebrewでインストールする

$ brew install docker
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/docker-1.5.0.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring docker-1.5.0.yosemite.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completion has been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
    /usr/local/Cellar/docker/1.5.0: 9 files, 7.2M

バージョン確認

$ docker -v
Docker version 1.5.0, build a8a31ef

boot2dockerのインストール

Homebrewでインストールする

$ brew install boot2docker
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/boot2docker-1.5.0.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring boot2docker-1.5.0.yosemite.bottle.tar.gz
==> Caveats
To have launchd start boot2docker at login:
    ln -sfv /usr/local/opt/boot2docker/*.plist ~/Library/LaunchAgents
Then to load boot2docker now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.boot2docker.plist
==> Summary
    /usr/local/Cellar/boot2docker/1.5.0: 3 files, 7.3M

バージョン確認

$ boot2docker -v
Boot2Docker-cli version: v1.5.0
Git commit: ccd9032
Usage: boot2docker [<options>] {help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|ip|shellinit|delete|download|upgrade|version} [<args>]

Boot2Docker VMの作成

$ boot2docker init
Latest release for boot2docker/boot2docker is v1.5.0
Downloading boot2docker ISO image...
Success: downloaded https://github.com/boot2docker/boot2docker/releases/download/v1.5.0/boot2docker.iso
    to /Users/user/.boot2docker/boot2docker.iso
Generating public/private rsa key pair.
Your identification has been saved in /Users/user/.ssh/id_boot2docker.
Your public key has been saved in /Users/user/.ssh/id_boot2docker.pub.
The key fingerprint is:
...

Boot2Docker VMの起動

$ boot2docker up
Waiting for VM and Docker daemon to start...
...................................oooooooooooooooooooooooooooooo
Started.
Writing /Users/user/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/user/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/user/.boot2docker/certs/boot2docker-vm/key.pem

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://192.168.59.104:2376
    export DOCKER_CERT_PATH=/Users/user/.boot2docker/certs/boot2docker-vm
    export DOCKER_TLS_VERIFY=1

Dockerクライアントとデーモンを接続するための設定(~/.bash_profileに追記)

$ export DOCKER_HOST=tcp://$(boot2docker ip 2>/dev/null):2376
$ export DOCKER_CERT_PATH=/Users/user/.boot2docker/certs/boot2docker-vm
$ export DOCKER_TLS_VERIFY=1    

Boot2Docker VMへSSH接続する

$ boot2docker ssh
                        ##        .
                  ## ## ##       ==
               ## ## ## ##      ===
           /""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
           \______ o          __/
             \    \        __/
              \____\______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.5.0, build master : a66bce5 - Tue Feb 10 23:31:27 UTC 2015
Docker version 1.5.0, build a8a31ef
docker@boot2docker:~$ 

開発環境用イメージの作成

CentOS7のDockerイメージをダウンロード

docker@boot2docker:~$ docker pull centos:centos7
511136ea3c5a: Pull complete 
5b12ef8fd570: Pull complete 
dade6cb4530a: Pull complete 
centos:centos7: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.

Status: Downloaded newer image for centos:centos7

Dockerイメージの一覧確認

docker@boot2docker:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos7             dade6cb4530a        4 days ago          210.1 MB

名前(devel)を付けてCentOS7イメージからコンテナを起動
開発ツールをグループインストール

docker@boot2docker:~$ docker run -d -t -i --name devel centos:centos7 /bin/bash
28547ee3afb43094eb9c0934bae5422793e8e810c5d36ffa2a29022895a9ae04
docker@boot2docker:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
28547ee3afb4        centos:centos7      "/bin/bash"         11 seconds ago      Up 10 seconds                           devel               
docker@boot2docker:~$ docker attach devel

[root@28547ee3afb4 /]# yum -y groupinstall "Development tools"
...
Complete!
[root@752fd88d7446 /]# CTRL-p CTRL-q

開発用イメージの作成

docker@boot2docker:~$ docker commit devel catrio:centos7devel
a33b0a461820d7d3baa80abe55bb91b846e48a112e214caaec338c133376bf4c
docker@boot2docker:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
catrio              centos7devel        a33b0a461820        18 seconds ago      727.5 MB
centos              centos7             dade6cb4530a        5 days ago          210.1 MB