在Redhat/Centos下安裝Docker(不升級內核)

零、博客背景linux

   因爲內核版本問題,最初僅ubuntu能夠較好的支持Docker。不過,因爲Redhat系列OS(REHL、Centos)是目前主流的Linux服務器操做系統,因此令Redhat系列OS支持Docker頗有必要。目前Docker和Redhat已經展開深刻合做,並在2013年年末推出了能夠在Redhat系列OS上運行的Docker0.7。git

   目前有一些博客介紹瞭如何在centos上安裝Docker,例如http://www.yanjiuyanjiu.com/blog/20131025/。可是這些博客都是針對老版本的Docker,安裝方法是在升級操做系統內核版本的基礎上完成。問題是,咱們不能夠隨意升級生產環境的操做系統內核版本,並且Docker0.7的主旨就是:Docker使用者能夠在不升級內核的前提下,在Redhat環境這使用Docker。所以,這裏撰寫一篇博客,介紹如何在Redhat/Centos環境下,安裝新版本的Docker。github


1、禁用selinuxdocker

   因爲Selinux和LXC有衝突,因此須要禁用selinux。編輯/etc/selinux/config,設置兩個關鍵變量。    ubuntu

SELINUX=disabled
SELINUXTYPE=targeted


2、配置Fedora EPEL源centos

sudo yum install http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm


3、添加hop5.repo源
api

cd /etc/yum.repos.d
sudo wget http://www.hop5.in/yum/el6/hop5.repo


4、安裝Dockerbash

sudo yum install docker-io

  圖1是yum安裝過程當中的截圖,能夠發現安裝的軟件只有docker和lxc相關包,沒有內核包,例如kernel-ml-aufs。服務器

wKioL1Lc1frT0gOXAAFfLfyxcvc660.jpg

圖1 yum install docker-io輸出截圖cors


5、初步驗證docker

  輸入docker -h,若是有以下輸出,就證實docker在形式上已經安裝成功。

# docker -h
Usage of docker:
  -D=false: Enable debug mode
  -H=[]: Multiple tcp://host:port or unix://path/to/socket to bind in daemon mode, single connection otherwise
  -api-enable-cors=false: Enable CORS headers in the remote API
  -b="": Attach containers to a pre-existing network bridge; use 'none' to disable container networking
  -bip="": Use this CIDR notation address for the network bridge's IP, not compatible with -b
  -d=false: Enable daemon mode
  -dns=[]: Force docker to use specific DNS servers
  -g="/var/lib/docker": Path to use as the root of the docker runtime
  -icc=true: Enable inter-container communication
  -ip="0.0.0.0": Default IP address to use when binding container ports
  -iptables=true: Disable docker's addition of iptables rules
  -p="/var/run/docker.pid": Path to use for daemon PID file
  -r=true: Restart previously running containers
  -s="": Force the docker runtime to use a specific storage driver
  -v=false: Print version information and quit


6、手動掛載cgroup

   在redhat/centos環境中運行docker、lxc,須要手動從新掛載cgroup。

   咱們首選禁用cgroup對應服務cgconfig。

sudo service cgconfig stop # 關閉服務
sudo chkconfig cgconfig off # 取消開機啓動

   而後掛載cgroup,能夠命令行掛載

mount -t cgroup none /cgroup # 僅本次有效

   或者修改配置文件,編輯/etc/fstab,加入

none /cgroup cgroup defaults 0 0 # 開機後自動掛載,一直有效

7、調整lxc版本

   Docker0.7默認使用的是lxc-0.9.0,該版本lxc在redhat上不能正常運行(具體狀況可參見本人另外一篇博客《Redhat下Docker、LXC、CGroup的協同配置》),須要調整lxc版本爲lxc-0.7.5或者lxc-1.0.0Beta2。前者能夠經過lxc網站(http://sourceforge.net/projects/lxc/files/lxc/)下載,後者須要在github上下載最新的lxc版本(https://github.com/lxc/lxc,目前版本是lxc-1.0.0Beta2)。

這裏特別說明一點,因爲Docker安裝絕對路徑/usr/bin/lxc-xxx調用lxc相關命令,因此須要將lxc-xxx安裝到/usr/bin/目錄下。


8、啓動docker服務

sudo service docker start # 啓動服務
sudo chkconfig docker on  # 開機啓動


9、試運行

sudo docker run -i -t ubuntu /bin/echo hello world

  初次執行此命令會先拉取鏡像文件,耗費必定時間。最後應當輸出hello world。


參考

Docker官方文檔 http://docs.docker.io/en/latest/installation/rhel/

http://www.yanjiuyanjiu.com/blog/20131025/

http://www.oschina.net/translate/nstalling-dockerio-on-centos-64-64-bit

相關文章
相關標籤/搜索