且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 CentOS 6.4(64位) 安装 docker.io

更新时间:2021-10-13 19:51:57

Docker.io 是轻量级的“容器引擎+映像仓库”,在LXC(linux轻量级容器)的基础上构建,可以运行任何应用程序。

在 CentOS 6.4(64位) 安装 docker.io

docker.io的核心层由以下几个部分组成:

1、可执行程序,“/usr/bin/docker”

2、docker.io网站上有一系列创建好的操作系统和应用程序映像

3、AUFS(另一个统一文件系统)来实现文件系统的快照,文件变化,控制只读或读写的需求。

4、LXC(Linux轻量级容器)

5、Cgroups(各种资源和命名空间的管理)

在本文写作的时候,最新版本是0.5.3,但目前开发很活跃,一些功能和特性可能在新版本中会发生变化。

按下面的步骤安装docker.io:

0、安装centos6.x

1、因为selinux和LXC有冲突,所以需要禁用


  1. [root@localhost ~]# cat /etc/selinux/config
  2. # This file controls the state of SELinux on the system.
  3. # SELINUX= can take one of these three values:
  4. # enforcing - SELinux security policy is enforced.
  5. # permissive - SELinux prints warnings instead of enforcing.
  6. # disabled - SELinux is fully disabled.
  7. SELINUX=disabled
  8. # SELINUXTYPE= type of policy in use. Possible values are:
  9. # targeted - Only targeted network daemons are protected.
  10. # strict - Full SELinux protection.
  11. SELINUXTYPE=targeted

2、配置Fedora EPEL 源


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

3、配置hop5.in源


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

4、安装docker-io


  1. [root@localhost ~]# yum install docker-io
  2. Loaded plugins: fastestmirror, security
  3. Loading mirror speeds from cached hostfile
  4. * base: centos.excellmedia.net
  5. * epel: kartolo.sby.datautama.net.id
  6. * extras: centos.excellmedia.net
  7. * updates: centos.excellmedia.net
  8. Setting up Install Process
  9. Resolving Dependencies
  10. --> Running transaction check
  11. ---> Package docker-io.x86_64 0:0.5.3-4.el6 will be installed
  12. --> Processing Dependency: lxc >= 0.8.0 for package: docker-io-0.5.3-4.el6.x86_64
  13. --> Processing Dependency: kernel-ml-aufs >= 3.10.5 for package: docker-io-0.5.3-4.el6.x86_64
  14. --> Running transaction check
  15. ---> Package kernel-ml-aufs.x86_64 0:3.10.5-3.el6 will be installed
  16. ---> Package lxc.x86_64 0:0.8.0-3.el6 will be installed
  17. --> Processing Dependency: liblxc.so.0()(64bit) for package: lxc-0.8.0-3.el6.x86_64
  18. --> Running transaction check
  19. ---> Package lxc-libs.x86_64 0:0.8.0-3.el6 will be installed
  20. --> Finished Dependency Resolution
  21. Dependencies Resolved
  22. ============================================================================================
  23. Package Arch Version Repository Size
  24. ============================================================================================
  25. Installing:
  26. docker-io x86_64 0.5.3-4.el6 hop5 1.2 M
  27. Installing for dependencies:
  28. kernel-ml-aufs x86_64 3.10.5-3.el6 hop5 33 M
  29. lxc x86_64 0.8.0-3.el6 hop5 81 k
  30. lxc-libs x86_64 0.8.0-3.el6 hop5 75 k
  31. Transaction Summary
  32. ============================================================================================
  33. Install 4 Package(s)
  34. Total download size: 34 M
  35. Installed size: 159 M
  36. Is this ok [y/N]: y
  37. Downloading Packages:
  38. (1/4): docker-io-0.5.3-4.el6.x86_64.rpm | 1.2 MB 00:03
  39. (2/4): kernel-ml-aufs-3.10.5-3.el6.x86_64.rpm | 33 MB 00:33
  40. (3/4): lxc-0.8.0-3.el6.x86_64.rpm | 81 kB 00:00
  41. (4/4): lxc-libs-0.8.0-3.el6.x86_64.rpm | 75 kB 00:00
  42. --------------------------------------------------------------------------------------------
  43. Total 896 kB/s | 34 MB 00:39
  44. Running rpm_check_debug
  45. Running Transaction Test
  46. Transaction Test Succeeded
  47. Running Transaction
  48. Installing : lxc-libs-0.8.0-3.el6.x86_64 1/4
  49. Installing : lxc-0.8.0-3.el6.x86_64 2/4
  50. Installing : kernel-ml-aufs-3.10.5-3.el6.x86_64 3/4
  51. Installing : docker-io-0.5.3-4.el6.x86_64 4/4
  52. Verifying : kernel-ml-aufs-3.10.5-3.el6.x86_64 1/4
  53. Verifying : lxc-0.8.0-3.el6.x86_64 2/4
  54. Verifying : lxc-libs-0.8.0-3.el6.x86_64 3/4
  55. Verifying : docker-io-0.5.3-4.el6.x86_64 4/4
  56. Installed:
  57. docker-io.x86_64 0:0.5.3-4.el6
  58. Dependency Installed:
  59. kernel-ml-aufs.x86_64 0:3.10.5-3.el6 lxc.x86_64 0:0.8.0-3.el6
  60. lxc-libs.x86_64 0:0.8.0-3.el6
  61. Complete!
  62. [root@localhost ~]#

5、检查安装情况


  1. [root@localhost ~]# docker -h
  2. Usage of docker:
  3. -D=false: Debug mode
  4. -H=[unix:///var/run/docker.sock]: tcp://host:port to bind/connect to or unix://path/to/socket to use
  5. -api-enable-cors=false: Enable CORS requests in the remote api.
  6. -b="": Attach containers to a pre-existing network bridge. Use 'none' to disable container networking
  7. -d=false: Daemon mode
  8. -dns="": Set custom dns servers
  9. -g="/var/lib/docker": Path to graph storage base dir.
  10. -p="/var/run/docker.pid": File containing process PID
  11. -r=false: Restart previously running containers

6、要想docker正常运行还需要在/etc/fstab里增加cgroup文件系统


  1. [root@localhost ~]# echo "none /sys/fs/cgroup cgroup defaults 0 0" >> /etc/fstab
  2. [root@localhost ~]# mount /sys/fs/cgroup

只有重新启动才能挂载/sys/fs/cgroup(因为当前运行的内核不支持cgroup),所以上面挂载的命令也可以不执行,但系统需要重新启动。

7、重启系统,选择“3.10.5-3.el6.x86_64”内核

8、系统启动后,确认当前运行的内核


  1. [root@localhost ~]# uname -r
  2. 3.10.5-3.el6.x86_64
  3. [root@localhost ~]# grep aufs /proc/filesystems
  4. nodev aufs

9、以守护模式运行docker.io(在一个新的终端里)


  1. [root@localhost ~]# docker -d
  2. 2013/08/21 07:47:07 WARNING: Your kernel does not support cgroup swap limit.
  3. 2013/08/21 07:47:07 Listening for HTTP on /var/run/docker.sock (unix)

10、在centos6.4容器里输出hello world


  1. [root@localhost ~]# docker run centos:6.4 echo "hello world"
  2. 2013/08/21 07:48:41 POST /v1.4/containers/create
  3. 2013/08/21 07:48:41 POST /v1.4/containers/c6bc9e80097e/start
  4. 2013/08/21 07:48:41 POST /v1.4/containers/c6bc9e80097e/attach?logs=1&stderr=1&stdout=1&stream=1
  5. hello world

11、从容器里测试ping


  1. [root@localhost ~]# docker -dns '8.8.8.8' run centos:6.4 ping -c 3 yahoo.com
  2. 2013/08/21 08:02:15 POST /v1.4/containers/create
  3. 2013/08/21 08:02:15 POST /v1.4/containers/c40a1244f9bc/start
  4. 2013/08/21 08:02:15 POST /v1.4/containers/c40a1244f9bc/attach?logs=1&stderr=1&stdout=1&stream=1
  5. PING yahoo.com (98.138.253.109) 56(84) bytes of data.
  6. 64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_seq=1 ttl=48 time=323 ms
  7. 64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_seq=2 ttl=48 time=329 ms
  8. 64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_seq=3 ttl=49 time=302 ms
  9. --- yahoo.com ping statistics ---
  10. 3 packets transmitted, 3 received, 0% packet loss, time 2304ms
  11. rtt min/avg/max/mdev = 302.032/318.318/329.656/11.807 ms

常见错误:

"DNS/Networking Errors inside the docker"


  1. [root@localhost ~]# docker -dns="8.8.8.8" run centos:6.4 yum install hiphop-php
  2. 2013/08/21 07:53:05 POST /v1.4/containers/create
  3. 2013/08/21 07:53:05 POST /v1.4/containers/6d9fef14bd1a/start
  4. 2013/08/21 07:53:05 POST /v1.4/containers/6d9fef14bd1a/attach?logs=1&stderr=1&stdout=1&stream=1
  5. Loaded plugins: fastestmirror
  6. Error: Cannot retrieve repository metadata (repomd.xml) for repository: base. Please verify its path and try again
  7. Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os error was
  8. 14: PYCURL ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"

可以执行下面的命令来重置docker的运行环境,从而解决上述问题。


  1. pkill docker
  2. iptables -t nat -F
  3. ifconfig docker0 down
  4. brctl delbr docker0
  5. docker -d

原文发布时间为:2014-01-03

本文来自云栖社区合作伙伴“Linux中国”