boot2dockerのインストールメモ
boot2dockerのインストールメモ
Macにboot2dockerとその上で動かすDockerイメージ(CentOS6)をインストールしてみました。その際の手順メモです。 boot2docker自体はVirtualBox上でVMとして稼働します。そのboot2docker VM上にはDockerサーバーがインストールされており各種Dockerイメージを動かすことができます。また、MacOS上にはDockerクライアントが入るのですぐにDockerの管理環境も手に入るのが良いです。
VirtualBoxのインストール以降の手順をメモっておこうと思います。
boot2dockerのインストール
■brewがインストールできているかを確認
$ brew --version
0.9.5
■dockerとboot2dockerのパッケージインストール
$ brew install docker boot2docker ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/docker-1.3. ######################################################################## 100.0% ==> Pouring docker-1.3.3.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.3.3: 9 files, 6.9M ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/boot2docker ######################################################################## 100.0% ==> Pouring boot2docker-1.3.3.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.3.3: 3 files, 7.3M
■インストールの確認
$ brew list | grep docker
boot2docker
docker
■boot2docker、dockerコマンド確認
$ boot2docker Usage: boot2docker [<options>] {help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|ip|shellinit|delete|download|upgrade|version} [<args>] $ docker -v Docker version 1.3.3, build d344625
■boot2dockerのVMイメージダウンロード
boot2dockerコマンドでVMイメージの管理ができるそうです。initコマンドではVMイメージのダウンロードが行われます。
$ boot2docker init Latest release for boot2docker/boot2docker is v1.4.1 Downloading boot2docker ISO image... Success: downloaded https://github.com/boot2docker/boot2docker/releases/download/v1.4.1/boot2docker.iso to /Users/kasshy/.boot2docker/boot2docker.iso Generating public/private rsa key pair. Your identification has been saved in /Users/kasshy/.ssh/id_boot2docker. Your public key has been saved in /Users/kasshy/.ssh/id_boot2docker.pub. (略)
■boot2dockerの起動
$ boot2docker up Waiting for VM and Docker daemon to start... ......................................oooooooooooooooooooooooooo Started. Writing /Users/kasshy/.boot2docker/certs/boot2docker-vm/ca.pem Writing /Users/kasshy/.boot2docker/certs/boot2docker-vm/cert.pem Writing /Users/kasshy/.boot2docker/certs/boot2docker-vm/key.pem To connect the Docker client to the Docker daemon, please set: export DOCKER_HOST=tcp://192.168.59.103:2376 export DOCKER_CERT_PATH=/Users/kasshy/.boot2docker/certs/boot2docker-vm export DOCKER_TLS_VERIFY=1
■起動の確認
$ boot2docker status
running
$ boot2docker ip
192.168.59.103
VirtualBoxマネージャー上でもboot2docker VMが起動していることを確認できました。止める場合はboot2docker stopコマンドで。
■.bash_profileに下記を追加
# boot2docker export DOCKER_HOST=tcp://192.168.59.103:2376 export DOCKER_CERT_PATH=/Users/kasshy/.boot2docker/certs/boot2docker-vm export DOCKER_TLS_VERIFY=1
source .bash_profileで再読み込み後、下記のコマンドでDockerサーバー側の結果が表示されればおk
$ docker version Client version: 1.3.3 Client API version: 1.15 Go version (client): go1.4 Git commit (client): d344625 OS/Arch (client): darwin/amd64 Server version: 1.4.1 Server API version: 1.16 Go version (server): go1.3.3 Git commit (server): 5bc2ff8
あとはdocker系のコマンドの補完が効くようにしておきたいと思います。
■bash-completionのインストール
$ brew install bash-completion ==> Downloading http://bash-completion.alioth.debian.org/files/bash-completion-1 ######################################################################## 100.0% ==> Patching patching file bash_completion ==> ./configure --prefix=/usr/local/Cellar/bash-completion/1.3 ==> make install ==> Caveats Add the following lines to your ~/.bash_profile: if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi Homebrew's own bash completion script has been installed to /usr/local/etc/bash_completion.d Bash completion has been installed to: /usr/local/etc/bash_completion.d ==> Summary 🍺 /usr/local/Cellar/bash-completion/1.3: 188 files, 1.1M, built in 10 seconds
■bash_profileに下記を追加
if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi
■補完が効くか確認
$ source .bash_profile $ docker r restart rm rmi run
Dockerイメージのダウンロード・管理
boot2docker、dockerの環境が整ったっぽいので、いよいよCentOSのイメージをダウンロードして起動してみたいと思います。
■CentOS6イメージのダウンロード
$ docker pull centos:centos6 centos:centos6: The image you are pulling has been verified 511136ea3c5a: Pull complete 5b12ef8fd570: Pull complete 510cf09a7986: Pull complete Status: Downloaded newer image for centos:centos6 $ docker attach diff history inspect pause restart search unpause build events images kill port rm start version commit exec import load ps rmi stop wait cp export info login pull run tag create help insert logs push save top $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos centos6 510cf09a7986 13 days ago 202.6 MB
■CentOS6の起動
起動は一瞬で完了しました。CentOSのバージョンを確認します。
$ docker run -t -i centos:centos6 /bin/bash [root@480d667d298b /]# cat /etc/redhat-release CentOS release 6.6 (Final)
■nginxのインストール
nginxなどをインストールしてみます。
root@480d667d298b /]# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm Retrieving http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm [root@480d667d298b /]# yum install nginx Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile * base: ftp.nara.wide.ad.jp * extras: ftp.nara.wide.ad.jp * updates: ftp.nara.wide.ad.jp Resolving Dependencies --> Running transaction check ---> Package nginx.x86_64 0:1.6.2-1.el6.ngx will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: nginx x86_64 1.6.2-1.el6.ngx nginx 336 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 336 k Installed size: 828 k Is this ok [y/N]: y Downloading Packages: nginx-1.6.2-1.el6.ngx.x86_64.rpm | 336 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Installing : nginx-1.6.2-1.el6.ngx.x86_64 1/1 ---------------------------------------------------------------------- Thanks for using nginx! Please find the official documentation for nginx here: * http://nginx.org/en/docs/ Commercial subscriptions for nginx are available on: * http://nginx.com/products/ ---------------------------------------------------------------------- Verifying : nginx-1.6.2-1.el6.ngx.x86_64 1/1 Installed: nginx.x86_64 0:1.6.2-1.el6.ngx Complete! root@480d667d298b /]# service nginx start Starting nginx: [ OK ]
■exitするとコンテナが終了
root@480d667d298b /]# exit exit $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 480d667d298b centos:centos6 "/bin/bash" 6 minutes ago Exited (0) 3 seconds ago mad_galileo
■コンテナイメージをcommit
nginxをインストールしたイメージをcommitする場合は下記のようにコンテナのIDとイメージ名をつけてコマンドを実行します。
$ docker commit 480d667d298b centos:nginx 93e4fec347cea1d63a413a430c27fc8cea4cca824c5c2d509fecbd0a5bc92518 $ docker images | grep nginx centos nginx 93e4fec347ce 47 seconds ago 242.9 MB
■コミットした新しいイメージで起動
Macのブラウザからnginxの画面にアクセスしたいと思います。-pオプションで、ポートを指定して起動しboot2docker VMとコンテナ上で動くCentOSのポートをマッピングして起動します。
$ docker run -p 80:80 -t -i centos:nginx /bin/bash [root@4b12eaa3695a /]# service nginx start Starting nginx: [ OK ] [root@4b12eaa3695a /]# netstat -tanp Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27/nginx
■VirtualBoxでポートフォワーディング設定
さらにMacとVM間のポートフォワーディング設定をし、MacからVM上のコンテナのnginxにアクセスできるようにします。VirtualBoxマネージャー上でboot2docker VMの設a定→ネットワーク→ポートフォワーディング設定を開きます。
例ではホストの8080ポートをVM上の80番ポートにマッピングするようにしました。これでMacOS(8080)→boot2docker VM(80)→CentOS(80)でのアクセスができるようになります。
ブラウザからlocalhost:8080にアクセスしてnginxの画面が出ることを確認できればOKです。
まとめ
CentOS6のイメージをベースに、いろいろなソフトウェアをインストールしたものを新しいコンテナイメージとしてcommitしておくと便利ですね。起動・終了も一瞬で終わるのでなかなか快適な感じです。もう少し色々いじってみたいと思います。