docker 是一个开源的应用容器引擎,主要利用 linux 内核 namespace 实现沙盒隔离,用cgroup 实现资源限制。
Docker 支持三种不同的镜像层次存储的 drivers:
aufs、devicemapper、btrfs ;
- Aufs:
AUFS (AnotherUnionFS) 是一种 Union FS, 简单来说就是支持将不同目录挂载到同一个虚
拟 文 件 系 统 下 (unite several directories into a single virtual filesystem) 的 文 件 系 统 。 Aufs
driver 是 docker 最早支持的 driver,但是 aufs 只是 linux 内核的一个补丁集而且不太可以会
被合并加入到 linux 内核中。但是由于 aufs 是唯一一个 storage driver 可以实现容器间共享可
执行及可共享的运行库, 所以当你跑成千上百个拥有相同程序代码或者运行库时时候,aufs
是个相当不错的选择。
- Device Mapper:
Device mapper 是 Linux 2.6 内核中提供的一种从逻辑设备到物理设备的映射框架机制,在
该机制下,用户可以很方便的根据自己的需要制定实现存储资源的管理策略 ( 详
见:http://www.ibm.com/developerworks/cn/linux/l-devmapper/index.html) 。
Device mapper driver 会创建一个 100G 的简单文件包含你的镜像和容器。每一个容器被限
制 在 10G 大 小 的 卷 内 。 ( 如 果 想 要 调 整 , 参考 :http://jpetazzo.github.io/2014/01/29/docker-device-mapper-resize/ 。 中 文 译 文 :
http://zhumeng8337797.blog.163.com/blog/static/100768914201452405120107/ )
你可以在启动 docker daemon 时用参数-s 指定 driver:
docker -d -s devicemapper ;
- Btrfs:
Btufs driver 在 docker build 可以很高效。但是跟 devicemapper 一样不支持设备间共享存储(文档里是 does not share executable memory between devices)。
在 没 有 aufs 支 持 的 linux 发 行 版 本 上 (CentOS,opensuse 等 ) 安 装 docker 可 能 就 使 用 了devicemapper driver。
安装
[[email protected] docker]# rpm -ivh docker-engine-1.10.3-1.el7.centos.x86_64.rpmwarning: docker-engine-1.10.3-1.el7.centos.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 2c52609d: NOKEYPreparing... ################################# [100%]Updating / installing... 1:docker-engine-1.10.3-1.el7.centos################################# [100%][[email protected] docker]# systemctl start docker
装载镜像
[[email protected] docker]# docker load -i ubuntu.tar [[email protected] docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu latest 07c86167cdc4 14 months ago 187.9 MB
创建容器
[[email protected] docker]# docker run -it ubuntu #-it交互模式 [email protected]:/# [[email protected] docker]# [[email protected] docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES910dbd82e909 ubuntu "/bin/bash" 18 seconds ago Up 13 seconds adoring_lovelace
查询一个命令属于哪个包
[[email protected] docker]# which brctl /sbin/brctl[[email protected] docker]# rpm -qf /sbin/brctlbridge-utils-1.5-9.el7.x86_64
因为docker默认网络模式为桥接模式,可以用桥接管理命令查看
[[email protected] docker]# brctl showbridge name bridge id STP enabled interfacesdocker0 8000.02428cec69f4 no veth5b3bf12
docker 退出容器模式为crtl+p+q,如果用exit,将关闭容器,即不在后台运行
[[email protected] docker]# docker run -it ubuntu[email protected]:/# exitexit[[email protected] docker]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES910dbd82e909 ubuntu "/bin/bash" 8 minutes ago Up 8 minutes adoring_lovelace[[email protected] docker]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5aace2fb8630 ubuntu "/bin/bash" 18 seconds ago Exited (0) 12 seconds ago stoic_saha910dbd82e909 ubuntu "/bin/bash" 8 minutes ago Up 8 minutes
删除容器
[[email protected] docker]# docker rm 5a5a[[email protected] docker]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES910dbd82e909 ubuntu "/bin/bash" 11 minutes ago Up 10 minutes adoring_lovelace
连接一个容器,查看桥接ip地址,ping
[email protected]:/# [[email protected] docker]# [[email protected] docker]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc4d727868a0b ubuntu "/bin/bash" 26 seconds ago Up 22 seconds vm1910dbd82e909 ubuntu "/bin/bash" 12 minutes ago Up 12 minutes adoring_lovelace[[email protected] docker]# docker attach vm1 [email protected]:/# ip addr show8: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff inet 172.17.0.3/16 scope global eth0[email protected]:/# ping 172.17.0.1PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.199 ms
在真机可以查看docker桥接网段
[[email protected] ~]# ip addr3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 02:42:8c:ec:69:f4 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0
容器的停止和删除
[[email protected] docker]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc4d727868a0b ubuntu "/bin/bash" 7 minutes ago Up 7 minutes vm1910dbd82e909 ubuntu "/bin/bash" 19 minutes ago Up 19 minutes adoring_lovelace[[email protected] docker]# docker stop 9191[[email protected] docker]# docker rm 9191[[email protected] docker]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc4d727868a0b ubuntu "/bin/bash" 7 minutes ago Up 7 minutes vm1[[email protected] docker]#
创建容器为后台关闭模式
[[email protected] docker]# docker run -d ubuntude3211baa651006b40a48d6765e1c7428779e1a5b3806b379f15700971071046[[email protected] docker]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESde3211baa651 ubuntu "/bin/bash" 17 seconds ago Exited (0) 12 seconds ago determined_brown
停止所有容器,删除所有容器
[[email protected] ~]# docker stop `docker ps -aq`fd799738de8fc7b5d3645fc6736d7669bb13[[email protected] ~]# docker rm `docker ps -aq`fd799738de8fc7b5d3645fc6736d7669bb13
删除镜像
[[email protected] ~]# docker rmi ubuntuUntagged: ubuntu:latestDeleted: sha256:07c86167cdc4264926fa5d2894e34a339ad27f730e8cc81a16cd21b7479e8eacDeleted: sha256:0aaccda2aadfc70ab2248437568fd17f4e8860cf612cc4b7e154b97222dccf91Deleted: sha256:220d2912ab1dbae4cb19007b1181991c51d7e9e71d83894f0ace0528df383eb3Deleted: sha256:9dcfe19e941956c63860afee1bec2e2318f6fbd336bc523094ed609a9c437a01Deleted: sha256:cc77a2e3d72c7dcb24ed23a23479e3b90ad64e3a91b2f0ce2320580979076d44
docker cp 本地文件到容器
[[email protected] docker]# docker cp /etc/passwd vm2:/mnt/[[email protected] docker]# docker attach vm2[email protected]:/# cd /mnt/[email protected]:/mnt# lspasswd
Inspect changes on a container‘s filesystem
[[email protected] mnt]# docker diff vm2C /mntA /mnt/passwdC /rootA /root/.bash_history
Show the history of an image
[[email protected] mnt]# docker history ubuntuIMAGE CREATED CREATED BY SIZE COMMENT07c86167cdc4 14 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B 220d2912ab1d 14 months ago /bin/sh -c sed -i ‘s/^#\s*\(deb.*universe\)$/ 1.895 kB cc77a2e3d72c 14 months ago /bin/sh -c echo ‘#!/bin/sh‘ > /usr/sbin/polic 194.5 kB c8fa7cdceff3 14 months ago /bin/sh -c #(nop) ADD file:b9504126dc55908988 187.7 MB
nginx
创建容器端口映射:直接访问本地的端口,即可实现对容器的访问 注意:映射端口不能和本地冲突!!!
[[email protected] mnt]# docker run -d --name web2 -p 8000:80 nginxf942d148a0b48148eaf2dd4f0b46f872587d4435c16ae45fbb0914ae2768c959[[email protected] mnt]# netstat -antlp |grep 8000tcp6 0 0 :::8000 :::* LISTEN 3166/docker-proxy
[[email protected] Desktop]$ curl 172.25.88.15:8000<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif;
[[email protected] mnt]# cat /proc/partitions major minor #blocks name 8 0 8388608 sda 8 1 512000 sda1 8 2 7875584 sda2 253 0 6991872 dm-0 253 1 839680 dm-1 7 0 104857600 loop0 7 1 2097152 loop1 253 2 104857600 dm-2 253 3 10485760 dm-3 253 4 10485760 dm-4