Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目。它基于 Google 公司推出的 Go 语言实现。 项目后来加入了 Linux 基金会,遵从了 Apache 2.0 协议,项目代码在 GitHub 上进行维护。
Docker 自开源后受到广泛的关注和讨论,以至于 dotCloud 公司后来都改名为 Docker Inc。Redhat 已经在其 RHEL6.5 中集中支持 Docker;Google 也在其 PaaS 产品中广泛应用。
Docker 项目的目标是实现轻量级的操作系统虚拟化解决方案。 Docker 的基础是 Linux 容器(LXC)等技术。
在 LXC 的基础上 Docker 进行了进一步的封装,让用户不需要去关心容器的管理,使得操作更为简便。用户操作 Docker 的容器就
像操作一个快速轻量级的虚拟机一样简单。
1.下载安装
在centos上运行docker必须是64位并且内核在2.6.32-431或者更高内核版本,建议完全更新系统
[root@iZ25gswv4olZ ~]# getconf LONG_BIT ---查看位数32/64 64 [root@iZ25gswv4olZ ~]# uname -r --查看内核版本 2.6.32-431.23.3.el6.x86_64 [root@iZ25gswv4olZ ~]# yum update ---完全更新系统 [root@iZ25gswv4olZ ~]# uname -r 2.6.32-431.23.3.el6.x86_64 [root@iZ25gswv4olZ ~]# lsb_release -a ----列出所有版本信息 LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch Distributor ID: CentOS Description: CentOS release 6.5 (Final) Release: 6.5 Codename: Final
----下载Docker。
[root@iZ25gswv4olZ /]# sudo yum install http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm [root@iZ25gswv4olZ /]# sudo yum install docker-io
2.启动
现在,它已经安装好了,让我们来启动docker进程
[root@iZ25gswv4olZ ~]# service docker start
它将会输出如下的信息:
Starting cgconfig service: [ OK ] Starting docker: [ OK ]
如果我们需要开机自启动,如下:
[root@iZ25gswv4olZ ~]#chkconfig docker on
3.最新的centos镜像
现在我们确认docker是否正常工作,首先我们需要获取最新的centos镜像
[root@iZ25gswv4olZ /]# sudo docker pull centos:latest
它将会输出如下的信息:
Cannot connect to the Docker daemon. Is 'docker -d' running on this host? [root@iZ25gswv4olZ local]# docker -d
它将会输出如下的信息:
WARN[0000] You are running linux kernel version 2.6.32-431.23.3.el6.x86_64, which might be unstable running docker.
Please upgrade your kernel to 3.10.0. INFO[0000] [graphdriver] using prior storage driver "devicemapper" INFO[0000] Listening for HTTP on unix (/var/run/docker.sock) WARN[0000] Running modprobe bridge nf_nat failed with message: , error: exit status 1 FATA[0000] Error starting daemon: Error initializing network controller: Error creating default "bridge" network: operation not supported
因为阿里云默认把所有的私网地址段都加到导致docker找不到一个可用的IP段了。
[root@iZ25gswv4olZ /]# ifconfig
它将会输出如下的信息:
eth0 Link encap:Ethernet HWaddr 00:16:3E:00:18:94 inet addr:10.172.193.40 Bcast:10.172.199.255 Mask:255.255.248.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2694 errors:0 dropped:0 overruns:0 frame:0 TX packets:235 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:122753 (119.8 KiB) TX bytes:20141 (19.6 KiB) Interrupt:148 eth1 Link encap:Ethernet HWaddr 00:16:3E:00:43:32 inet addr:182.92.177.208 Bcast:182.92.179.255 Mask:255.255.252.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:473435 errors:0 dropped:0 overruns:0 frame:0 TX packets:395 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:19904390 (18.9 MiB) TX bytes:46006 (44.9 KiB) Interrupt:147 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
[root@iZ25gswv4olZ /]# docker -bip=10.172.193.40/24 -d &
它将会输出如下的信息:
[1] 1393 [root@iZ25gswv4olZ /]# Warning: '-bip' is deprecated, it will be replaced by '--bip' soon. See usage. INFO[0000] +job serveapi(unix:///var/run/docker.sock) INFO[0000] WARNING: You are running linux kernel version 2.6.32-504.23.4.el6.x86_64, which might be unstable running docker. Please upgrade your kernel to 3.8.0. INFO[0000] +job init_networkdriver() INFO[0000] Listening for HTTP on unix (/var/run/docker.sock) INFO[0000] -job init_networkdriver() = OK (0) INFO[0000] Loading containers: start. INFO[0000] Loading containers: done. INFO[0000] docker daemon: 1.5.0 a8a31ef/1.5.0; execdriver: native-0.2; graphdriver: devicemapper INFO[0000] +job acceptconnections() INFO[0000] -job acceptconnections() = OK (0)
[root@iZ25gswv4olZ /]# lxc-checkconfig
它将会输出如下的信息:
Kernel configuration not found at /proc/config.gz; searching... Kernel configuration found at /boot/config-2.6.32-504.23.4.el6.x86_64 --- Namespaces --- Namespaces: enabled Utsname namespace: enabled Ipc namespace: enabled Pid namespace: enabled User namespace: enabled Network namespace: enabled Multiple /dev/pts instances: enabled --- Control groups --- Cgroup: enabled Cgroup namespace: enabled Cgroup device: enabled Cgroup sched: enabled Cgroup cpu account: enabled Cgroup memory controller: /usr/bin/lxc-checkconfig: line 103: [: too many arguments enabled Cgroup cpuset: enabled --- Misc --- Veth pair device: enabled Macvlan: enabled Vlan: enabled File capabilities: /usr/bin/lxc-checkconfig: line 118: [: -gt: unary operator expected Note : Before booting a new kernel, you can check its configuration usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig
可以看到 Control groups 项目都是支持的,所以我们暂时忽略这个告警。
测试使用-我们需要下载最新的centos镜像
注意:由于 Aliyun 的网络直接 pull 官方镜像也不是很稳定,可选择官方标准镜像来测试
[root@iZ25gswv4olZ ~]# sudo docker pull centos:latest
它将会输出如下的信息:
INFO[0623] POST /v1.17/images/create?fromImage=centos%3Alatest INFO[0623] +job pull(centos, latest) INFO[0623] +job resolve_repository(centos) INFO[0623] -job resolve_repository(centos) = OK (0) INFO[0623] +job trust_update_base() INFO[0624] -job trust_update_base() = OK (0) Pulling repository centos 7322fbe74aa5: Download complete f1b10cd84249: Download complete c852f6d61e65: Download complete INFO[0855] +job log(pull, centos:latest, ) INFO[0855] -job log(pull, centos:latest, ) = OK (0) INFO[0855] -job pull(centos, latest) = OK (0) Status: Downloaded newer image for centos:latest
下一步,我们要确保通过如下命令可以看到镜像运行情况:
[root@iZ25gswv4olZ /]# docker images centos
它将会输出如下的信息:
INFO[3912] GET /v1.17/images/json?filter=centos INFO[3912] +job images() INFO[3912] -job images() = OK (0) REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEcentos 7 7322fbe74aa5 4 weeks ago 172.2 MB centos latest 7322fbe74aa5 4 weeks ago 172.2 MB centos centos7 7322fbe74aa5 4 weeks ago 172.2 MB
运行简单的bash shell来测试这个镜像
[root@iZ25gswv4olZ /]# docker run -i -t centos /bin/bash
它将会输出如下的信息:
INFO[0481] POST /v1.17/containers/create INFO[0481] +job create() INFO[0481] +job log(create, 523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a2266507e45c INFO[0481] -job log(create, 523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a2266507e45c INFO[0481] -job create() = OK (0) INFO[0481] POST /v1.17/containers/523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a22665 INFO[0481] +job container_inspect(523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a22665 INFO[0481] -job container_inspect(523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a22665 INFO[0481] +job attach(523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a2266507e45c1873) 07e45c1873) INFO[0481] +job allocate_interface(523e56a0b3bd09f625793a97a5a9e2a22be804326 3a97a5a9e2a22be804326d919bf4a2266507e45c1873, centos:7) INFO[0481] -job log(start, 523e5 0482] POST /v1.17/containers/523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a2266507e45 4326d919bf4a2266507e45c1873, 54, 270) = OK (0) [root@523e56a0b3bd /]# INFO[0487] POST /v1.17/containers/523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a2266507e45c1873/resize?h=23&w=88 INFO[0487] +job resize(523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a2266507e45c1873, 23, 88) INFO[0487] -job resize(523e56a0b3bd09f625793a97a5a9e2a22be804326d919bf4a2266507e45c1873, 23, 88) = OK (0)
如果正常,你会获得一个简单的bash提示,输入exit退出。
# exit