安装docker
[root@localhost /]# yum -y install docker-io
更改配置文件
[root@localhost /]# vi /etc/sysconfig/docker
other-args列更改为:other_args=”–exec-driver=lxc –selinux-enabled”
启动docker服务
[root@localhost /]# service docker start
Starting cgconfig service: [ OK ]
Starting docker: [ OK ]
将docker加入开机启动
[root@localhost /]# chkconfig docker on
基本信息查看
docker version:查看docker的版本号,包括客户端、服务端、依赖的Go等
[root@localhost /]# docker version
Client version: 1.0.0
Client API version: 1.12
Go version (client): go1.2.2
Git commit (client): 63fe64c/1.0.0
Server version: 1.0.0
Server API version: 1.12
Go version (server): go1.2.2
Git commit (server): 63fe64c/1.0.0
docker info :查看系统(docker)层面信息,包括管理的images, containers数等
[root@localhost /]# docker info
Containers: 16
Images: 40
Storage Driver: devicemapper
Pool Name: docker-253:0-1183580-pool
Data file: /var/lib/docker/devicemapper/devicemapper/data
Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
Data Space Used: 2180.4 Mb
Data Space Total: 102400.0 Mb
Metadata Space Used: 3.4 Mb
Metadata Space Total: 2048.0 Mb
Execution Driver: lxc-0.9.0
Kernel Version: 2.6.32-431.el6.x86_64
5 镜像的获取与容器的使用
镜像可以看作是包含有某些软件的容器系统,比如ubuntu就是一个官方的基础镜像,很多镜像都是基于这个镜像“衍生”,该镜像包含基本的ubuntu系统。再比如,hipache是一个官方的镜像容器,运行后可以支持http和websocket的代理服务,而这个镜像本身又基于ubuntu。
搜索镜像
docker search <image>:在docker index中搜索image
[root@localhost /]# docker search ubuntu12.10
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mirolin/ubuntu12.10 0
marcgibbons/ubuntu12.10 0
mirolin/ubuntu12.10_redis 0
chug/ubuntu12.10×32 Ubuntu Quantal Quetzal 12.10 32bit base i… 0
chug/ubuntu12.10×64 Ubuntu Quantal Quetzal 12.10 64bit base i… 0
下载镜像
docker pull <image> :从docker registry server 中下拉image
[root@localhost /]# docker pull chug/ubuntu12.10×64
查看镜像
docker images: 列出images
docker images -a :列出所有的images(包含历史)
docker images –tree :显示镜像的所有层(layer)
docker rmi <image ID>: 删除一个或多个image
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
chug/ubuntu12.10×64 latest 0b96c14dafcd 4 months ago 270.3 MB
[root@localhost /]# docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
chug/ubuntu12.10×64 latest 0b96c14dafcd 4 months ago 270.3 MB
<none> <none> 31edfed3bb88 4 months ago 175.8 MB
[root@localhost /]# docker images –tree
Warning: ‘–tree’ is deprecated, it will be removed soon. See usage.
└─31edfed3bb88 Virtual Size: 175.8 MB
└─0b96c14dafcd Virtual Size: 270.3 MB Tags: chug/ubuntu12.10×64:latest
[root@localhost /]# docker rmi <image ID> ….
使用镜像创建容器
[root@localhost /]# docker run chug/ubuntu12.10×64 /bin/echo hello world
hello world
交互式运行
[root@localhost /]# docker run -i -t chug/ubuntu12.10×64 /bin/bash
root@2161509ff65e:/#
查看容器
docker ps :列出当前所有正在运行的container
docker ps -l :列出最近一次启动的container
docker ps -a :列出所有的container(包含历史,即运行过的container)
docker ps -q :列出最近一次运行的container ID
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ccf3de663dc9 chug/ubuntu12.10×64:latest /bin/bash 22 hours ago Up 22 hours sharp_hypatia
[root@localhost /]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f145f184647b chug/ubuntu12.10×64:latest /bin/bash 6 seconds ago Exited (0) 3 seconds ago compassionate_galileo
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f145f184647b chug/ubuntu12.10×64:latest /bin/bash 30 seconds ago Exited (0) 26 seconds ago compassionate_galileo
f4624b42fe7e chug/ubuntu12.10×64:latest /bin/bash 2 minutes ago Exited (0) 2 minutes ago sharp_wilson
ccf3de663dc9 chug/ubuntu12.10×64:latest /bin/bash 22 hours ago Up 22 hours sharp_hypatia
9cbaa79b9703 chug/ubuntu12.10×64:latest /bin/bash 22 hours ago Exited (127) 36 minutes ago berserk_mcclintock
2161509ff65e chug/ubuntu12.10×64:latest /bin/bash 22 hours ago Exited (0) 22 hours ago backstabbing_mclean
[root@localhost /]# docker ps -q
ccf3de663dc9
再次启动容器
docker start/stop/restart <container> :开启/停止/重启container
docker start [container_id] :再次运行某个container (包括历史container)
docker attach [container_id] :连接一个正在运行的container实例(即实例必须为start状态,可以多个窗口同时attach 一个container实例)
docker start -i <container> :启动一个container并进入交互模式(相当于先start,在attach)
docker run -i -t <image> /bin/bash :使用image创建container并进入交互模式, login shell是/bin/bash
docker run -i -t -p <host_port:contain_port> :映射 HOST 端口到容器,方便外部访问容器内服务,host_port 可以省略,省略表示把 container_port 映射到一个动态端口。
注:使用start是启动已经创建过得container,使用run则通过image开启一个新的container。
删除容器
docker rm <container…> :删除一个或多个container
docker rm `docker ps -a -q` :删除所有的container
docker ps -a -q | xargs docker rm :同上, 删除所有的container
6 持久化容器与镜像
6.1 通过容器生成新的镜像
运行中的镜像称为容器。你可以修改容器(比如删除一个文件),但这些修改不会影响到镜像。不过,你使用docker commit <container-id> <image-name>命令可以把一个正在运行的容器变成一个新的镜像。
docker commit <container> [repo:tag] 将一个container固化为一个新的image,后面的repo:tag可选。
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
chug/ubuntu12.10×64 latest 0b96c14dafcd 4 months ago 270.3 MB
[root@localhost /]# docker commit d0fd23b8d3ac chug/ubuntu12.10x64_2
daa11948e23d970c18ad89c9e5d8972157fb6f0733f4742db04219b9bb6d063b
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
chug/ubuntu12.10x64_2 latest daa11948e23d 6 seconds ago 270.3 MB
chug/ubuntu12.10×64 latest 0b96c14dafcd 4 months ago 270.3 MB
http://www.server110.com/docker/201411/11105.html