Vagrant CentOS7 上でdockerはじめてみた
Vagrantbox.es
http://www.vagrantbox.es/
CentOS 7 x64 (Minimal, Shrinked, Guest Additions 4.3.26) (Monthly updates)
https://github.com/holms/vagrant-centos7-box/releases/download/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box
↑のboxで起動しただけの初期状態からdockerをインストールする。
■dockerを探す
[vagrant@localhost ~]$ yum search docker Loaded plugins: fastestmirror ・・・・・ ============================================================================================================ N/S matched: docker ============================================================================================================ cockpit-docker.x86_64 : Cockpit user interface for Docker containers docker-logrotate.x86_64 : cron job to run logrotate on docker containers docker-python.x86_64 : An API client for docker written in Python docker-registry.noarch : Registry server for Docker docker-registry.x86_64 : Registry server for Docker docker-selinux.x86_64 : SELinux policies for Docker docker.x86_64 : Automates deployment of containerized applications docker-devel.x86_64 : A golang registry for global request variables (source libraries) Name and summary matches only, use "search all" for everything.
■インストール
[root@localhost ~]# yum install docker [root@localhost ~]# systemctl status docker.service docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled) Active: inactive (dead) Docs: http://docs.docker.com
■いざ起動
[root@localhost ~]# systemctl start docker.service Job for docker.service failed. See 'systemctl status docker.service' and 'journalctl -xn' for details.
動かない。。
[root@localhost ~]# systemctl status docker.service docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled) Active: failed (Result: exit-code) since Fri 2015-06-26 03:37:05 UTC; 4min 2s ago Docs: http://docs.docker.com Process: 2467 ExecStart=/usr/bin/docker -d $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY (code=exited, status=1/FAILURE) Main PID: 2467 (code=exited, status=1/FAILURE) Jun 26 03:36:05 localhost.localdomain systemd[1]: Starting Docker Application Container Engine... Jun 26 03:36:05 localhost.localdomain docker[2467]: time="2015-06-26T03:36:05Z" level=info msg="+job serveapi(unix:///var/run/docker.sock)" Jun 26 03:36:05 localhost.localdomain docker[2467]: time="2015-06-26T03:36:05Z" level=info msg="Listening for HTTP on unix (/var/run/docker.sock)" Jun 26 03:37:05 localhost.localdomain docker[2467]: time="2015-06-26T03:37:05Z" level=fatal msg="Shutting down daemon due to errors: error intializing graphdriver: exit status 1" Jun 26 03:37:05 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE Jun 26 03:37:05 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine. Jun 26 03:37:05 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
■以下のページを発見。
CentOS7でDockerを動かす場合
http://shimakaze.hatenablog.com/entry/2015/04/02/103449
■とりあえず「libdevmapper」インストール。
# yum -y install libdevmapper.so.1.02 [root@localhost ~]# systemctl start docker.service Job for docker.service failed. See 'systemctl status docker.service' and 'journalctl -xn' for details. [root@localhost ~]# systemctl status docker.service docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled) Active: failed (Result: exit-code) since Fri 2015-06-26 03:45:26 UTC; 14s ago Docs: http://docs.docker.com Process: 2680 ExecStart=/usr/bin/docker -d $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY (code=exited, status=1/FAILURE) Main PID: 2680 (code=exited, status=1/FAILURE) Jun 26 03:45:26 localhost.localdomain systemd[1]: Starting Docker Application Container Engine... Jun 26 03:45:26 localhost.localdomain docker[2680]: time="2015-06-26T03:45:26Z" level=info msg="+job serveapi(unix:///var/run/docker.sock)" Jun 26 03:45:26 localhost.localdomain docker[2680]: time="2015-06-26T03:45:26Z" level=info msg="Listening for HTTP on unix (/var/run/docker.sock)" Jun 26 03:45:26 localhost.localdomain docker[2680]: time="2015-06-26T03:45:26Z" level=fatal msg="Shutting down daemon due to errors: error intializing graphdriver: Error running DeleteDevice dm_task_run failed" Jun 26 03:45:26 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE Jun 26 03:45:26 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine. Jun 26 03:45:26 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
まだ動かない。。
■調べて以下のページをみつける。
[1.6.0][graphdriver] prior storage driver “devicemapper” failed: error intializing graphdriver #13008
https://github.com/docker/docker/issues/13008
[root@localhost lib]# pwd /var/lib [root@localhost lib]# rm -rf docker/
とりあえず消してみた。
[root@localhost ~]# systemctl start docker.service Job for docker.service failed. See 'systemctl status docker.service' and 'journalctl -xn' for details. [root@localhost ~]# systemctl status docker.service -l docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled) Active: failed (Result: exit-code) since Fri 2015-06-26 03:51:46 UTC; 47s ago Docs: http://docs.docker.com Process: 15073 ExecStart=/usr/bin/docker -d $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY (code=exited, status=1/FAILURE) Main PID: 15073 (code=exited, status=1/FAILURE) Jun 26 03:51:45 localhost.localdomain systemd[1]: Starting Docker Application Container Engine... Jun 26 03:51:46 localhost.localdomain docker[15073]: time="2015-06-26T03:51:46Z" level=info msg="+job serveapi(unix:///var/run/docker.sock)" Jun 26 03:51:46 localhost.localdomain docker[15073]: time="2015-06-26T03:51:46Z" level=info msg="Listening for HTTP on unix (/var/run/docker.sock)" Jun 26 03:51:46 localhost.localdomain docker[15073]: time="2015-06-26T03:51:46Z" level=fatal msg="Shutting down daemon due to errors: error intializing graphdriver: Unable to take ownership of thin-pool (centos-docker--pool) that already has used data blocks" Jun 26 03:51:46 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE Jun 26 03:51:46 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine. Jun 26 03:51:46 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
動かない・・・。
■一旦dockerを消す。
[root@localhost ~]# yum remove docker
■とりあえずupdateする。
[root@localhost ~]# yum update
■もう一回いれてみる。
[root@localhost ~]# yum install docker [root@localhost ~]# systemctl start docker [root@localhost ~]# systemctl status docker docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled) Active: active (running) since Fri 2015-06-26 03:57:52 UTC; 17s ago Docs: http://docs.docker.com Main PID: 1027 (docker) CGroup: /system.slice/docker.service └─1027 /usr/bin/docker -d --selinux-enabled Jun 26 03:57:51 localhost.localdomain docker[1027]: time="2015-06-26T03:57:51Z" level=warning msg="Docker could not enable SELinux on the host system" Jun 26 03:57:51 localhost.localdomain docker[1027]: time="2015-06-26T03:57:51Z" level=info msg="+job init_networkdriver()" Jun 26 03:57:52 localhost.localdomain docker[1027]: time="2015-06-26T03:57:52Z" level=info msg="-job init_networkdriver() = OK (0)" Jun 26 03:57:52 localhost.localdomain docker[1027]: time="2015-06-26T03:57:52Z" level=info msg="Loading containers: start." Jun 26 03:57:52 localhost.localdomain docker[1027]: time="2015-06-26T03:57:52Z" level=info msg="Loading containers: done." Jun 26 03:57:52 localhost.localdomain docker[1027]: time="2015-06-26T03:57:52Z" level=info msg="docker daemon: 1.6.2 ba1f6c3/1.6.2; execdriver: native-0.2; graphdriver: devicemapper" Jun 26 03:57:52 localhost.localdomain docker[1027]: time="2015-06-26T03:57:52Z" level=info msg="+job acceptconnections()" Jun 26 03:57:52 localhost.localdomain docker[1027]: time="2015-06-26T03:57:52Z" level=info msg="-job acceptconnections() = OK (0)" Jun 26 03:57:52 localhost.localdomain docker[1027]: time="2015-06-26T03:57:52Z" level=info msg="Daemon has completed initialization" Jun 26 03:57:52 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
動いた・・・!
先に「libdevmapper.so.1.02」がはいってないといけなかったのか?
■動いたので自動起動に。
[root@localhost ~]# systemctl enable docker ln -s '/usr/lib/systemd/system/docker.service' '/etc/systemd/system/multi-user.target.wants/docker.service'
■Dockerイメージの検索
[root@localhost ~]# docker search centos INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/centos The official build of CentOS. 1087 [OK] docker.io docker.io/tutum/centos Centos image with SSH access. For the root... 13 [OK] docker.io docker.io/blalor/centos Bare-bones base CentOS 6.5 image 9 [OK] docker.io docker.io/torusware/speedus-centos Always updated official CentOS docker imag... 6 [OK] docker.io docker.io/million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 4 [OK] docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.6 x86_64 / EPEL Repo. / OpenSSH... 3 [OK] docker.io docker.io/layerworx/centos A general CentOS 6 image with the EPEL6 an... 2 [OK] docker.io docker.io/akroh/centos Centos 6 container that has been updated w... 0 [OK] docker.io docker.io/nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 0 [OK] docker.io docker.io/pdericson/centos Docker image for CentOS 0 [OK]
■Dockerイメージのダウンロード
とりあえず公式のcentosを取得する
[root@localhost ~]# docker pull docker.io/centos Trying to pull repository docker.io/centos ... 7322fbe74aa5: Download complete f1b10cd84249: Download complete c852f6d61e65: Download complete Status: Downloaded newer image for docker.io/centos:latest
■ダウンロードしたイメージの確認
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE docker.io/centos latest 7322fbe74aa5 7 days ago 172.2 MB
■Dockerコンテナの作成
docker runでコンテナの新規作成と実行の両方が行われる
[root@localhost ~]# docker run -it --name centos1 docker.io/centos:latest /bin/bash Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning. [root@f89ddd90d0fd /]#
実行された。
docker run の主なオプション
-i:コンテナーの標準入力を開く。/bin/bashなどでコンテナーを操作する際に指定
-t:tty(端末デバイス)を確保する。/bin/bashなどでコンテナーを操作する際に指定
-d:バックグラウンドで実行する(後述)。既定では、コンテナーをフォアグラウンドで実行するため、Webサーバーやアプリケーションサーバーなど常時実行するコンテナーで指定
-p {ホストのポート番号}:{コンテナーのポート番号}:Dockerサーバーのホストとポートマッピングを構成
■コンテナにapache入れてみる。
[root@f89ddd90d0fd /]# yum install httpd
■一旦コンテナ停止
[root@f89ddd90d0fd /]# exit exit
[Ctrl]+[d]キーでbashプロセスを終了できる。
コンテナを停止することなく、元のシェルに戻したい場合は[Ctrl-P]+[Ctrl-q]でコンテナのTTYを抜けられる。
■停止中のコンテナも含めてコンテナの状態を確認する
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f89ddd90d0fd docker.io/centos:latest "/bin/bash" 9 minutes ago Exited (0) 3 minutes ago centos1
■コンテナの起動
[root@localhost ~]# docker start centos1 centos1 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f89ddd90d0fd docker.io/centos:latest "/bin/bash" 10 minutes ago Up 19 seconds centos1
■コンテナへの再接続
[root@localhost ~]# docker attach centos1 Enter押す [root@f89ddd90d0fd /]#
■[Ctrl-P]+[Ctrl-q]でTTY抜けてみる。
[root@f89ddd90d0fd /]# [root@localhost ~]#
■コンテナの停止
[root@localhost ~]# docker stop centos1 centos1
■コンテナの再起動
※コンテナのプロセスを強制終了して再度スタート
[root@localhost ~]# docker restart centos1 centos1
■コンテナのコミット
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f89ddd90d0fd docker.io/centos:latest "/bin/bash" About an hour ago Exited (0) 7 seconds ago centos1 [root@localhost ~]# docker commit -a MOGUMAGU -m "CentOS7 installed Apache" centos1 localrepo:withApache 74fe551af11fbdbe8e5e4849fcd4dd580ce7057d80e976ceeaaf8b9750954756
保存したイメージの確認
[root@localhost ~]# docker images localrepo REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE localrepo withApache 74fe551af11f 44 seconds ago 268.4 MB
イメージの作成履歴を見る
[root@localhost ~]# docker images -t Warning: '-t' is deprecated, it will be removed soon. See usage. └─f1b10cd84249 Virtual Size: 0 B └─c852f6d61e65 Virtual Size: 172.2 MB └─7322fbe74aa5 Virtual Size: 172.2 MB Tags: docker.io/centos:latest └─74fe551af11f Virtual Size: 268.4 MB Tags: localrepo:withApache
■コンテナの削除
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f89ddd90d0fd docker.io/centos:latest "/bin/bash" About an hour ago Exited (0) 5 minutes ago centos1 [root@localhost ~]# docker rm centos1 centos1 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]#
※保管が必要ならあらかじめdocker commitでイメージを保管しておくこと
■さっき作ったコンテナから起動
[root@localhost ~]# docker run -it --name centos2 localrepo:withApache /bin/bash Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning. [root@5cdc79690cee /]# [root@5cdc79690cee /]# yum info httpd Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: www.ftp.ne.jp * extras: www.ftp.ne.jp * updates: www.ftp.ne.jp Installed Packages Name : httpd Arch : x86_64 Version : 2.4.6 Release : 31.el7.centos Size : 9.4 M Repo : installed From repo : base Summary : Apache HTTP Server URL : http://httpd.apache.org/ License : ASL 2.0 Description : The Apache HTTP Server is a powerful, efficient, and extensible : web server.
apache入ってる。
[Ctrl-P]+[Ctrl-q]でTTY抜ける
[root@5cdc79690cee ~]# [root@localhost ~]#
コンテナの起動状態を確認。
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5cdc79690cee localrepo:withApache "/bin/bash" About a minute ago Up About a minute centos2
動いてる。
■webサーバとして起動する例
先ほどコミットしたlocalrepo:withApacheをベースにwebサーバを起動しブラウザから確認する。
[root@localhost ~]# docker run -d --name webserver --expose=80 --publish 80:80 localrepo:withApache /usr/sbin/httpd -D FOREGROUND
上記でホストのipでブラウザアクセスしたらapacheの初期画面が表示される。
状態はこちら
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6fd53d6fd905 localrepo:withApache "/usr/sbin/httpd -D 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp webserver 5cdc79690cee localrepo:withApache "/bin/bash" 46 minutes ago Exited (0) 8 minutes ago centos2
実際のコンテンツなどはホストとボリュームを共有して保持しておくほうがいいみたい。