docker.docx
《docker.docx》由会员分享,可在线阅读,更多相关《docker.docx(9页珍藏版)》请在冰点文库上搜索。
docker
docker安装与启动
1.1安装docker
[root@localhost /]# yum -y install docker-io
1.2更改配置文件
[root@localhost /]# vi /etc/sysconfig/docker
other-args列更改为:
other_args="--exec-driver=lxc--selinux-enabled"
1.3启动docker服务
[root@localhost /]# service docker start
Starting cgconfig service:
[ OK ]
Starting docker:
[ OK ]
将docker加入开机启动
[root@localhost /]# chkconfig docker on
基本信息查看
dockerversion:
查看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
dockerinfo:
查看系统(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
1.4镜像的获取与容器的使用
镜像可以看作是包含有某些软件的容器系统,比如ubuntu就是一个官方的基础镜像,很多镜像都是基于这个镜像“衍生”,该镜像包含基本的ubuntu系统。
再比如,hipache是一个官方的镜像容器,运行后可以支持http和websocket的代理服务,而这个镜像本身又基于ubuntu。
1.4.1搜索镜像
dockersearch:
在dockerindex中搜索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.10x32 Ubuntu Quantal Quetzal 12.10 32bit base i... 0
chug/ubuntu12.10x64 Ubuntu Quantal Quetzal 12.10 64bit base i... 0
1.4.2下载镜像
dockerpull:
从dockerregistryserver中下拉image
[root@localhost /]# docker pull chug/ubuntu12.10x64
1.4.3查看镜像
dockerimages:
列出images
dockerimages-a:
列出所有的images(包含历史)
dockerimages--tree :
显示镜像的所有层(layer)
dockerrmi :
删除一个或多个image
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
chug/ubuntu12.10x64 latest 0b96c14dafcd 4 months ago 270.3 MB
[root@localhost /]# docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
chug/ubuntu12.10x64 latest 0b96c14dafcd 4 months ago 270.3 MB
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.10x64:
latest
[root@localhost /]# docker rmi ....
1.4.4使用镜像创建容器
[root@localhost /]# docker run chug/ubuntu12.10x64 /bin/echo hello world
hello world
交互式运行
[root@localhost /]# docker run -i -t chug/ubuntu12.10x64 /bin/bash
root@2161509ff65e:
/#
1.4.5查看容器
dockerps:
列出当前所有正在运行的container
dockerps-l:
列出最近一次启动的container
dockerps-a:
列出所有的container(包含历史,即运行过的container)
dockerps-q:
列出最近一次运行的containerID
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ccf3de663dc9 chug/ubuntu12.10x64:
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.10x64:
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.10x64:
latest /bin/bash 30 seconds ago Exited (0) 26 seconds ago compassionate_galileo
f4624b42fe7e chug/ubuntu12.10x64:
latest /bin/bash 2 minutes ago Exited (0) 2 minutes ago sharp_wilson
ccf3de663dc9 chug/ubuntu12.10x64:
latest /bin/bash 22 hours ago Up 22 hours sharp_hypatia
9cbaa79b9703 chug/ubuntu12.10x64:
latest /bin/bash 22 hours ago Exited (127) 36 minutes ago berserk_mcclintock
2161509ff65e chug/ubuntu12.10x64:
latest /bin/bash 22 hours ago Exited (0) 22 hours ago backstabbing_mclean
[root@localhost /]# docker ps -q
ccf3de663dc9
1.4.6再次启动容器
dockerstart/stop/restart:
开启/停止/重启container
dockerstart[container_id]:
再次运行某个container(包括历史container)
dockerattach[container_id]:
连接一个正在运行的container实例(即实例必须为start状态,可以多个窗口同时attach一个container实例)
dockerstart-i:
启动一个container并进入交互模式(相当于先start,在attach)
dockerrun-i-t/bin/bash:
使用image创建container并进入交互模式,loginshell是/bin/bash
dockerrun-i-t-pcontain_port>:
映射HOST端口到容器,方便外部访问容器内服务,host_port可以省略,省略表示把container_port映射到一个动态端口。
注:
使用start是启动已经创建过得container,使用run则通过image开启一个新的container。
1.4.7删除容器
dockerrm:
删除一个或多个container
dockerrm`dockerps-a-q`:
删除所有的container
dockerps-a-q|xargsdockerrm:
同上,删除所有的container
1.5持久化容器与镜像
1.5.1通过容器生成新的镜像
运行中的镜像称为容器。
你可以修改容器(比如删除一个文件),但这些修改不会影响到镜像。
不过,你使用dockercommit命令可以把一个正在运行的容器变成一个新的镜像。
dockercommit[repo:
tag]将一个container固化为一个新的image,后面的repo:
tag可选。
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
chug/ubuntu12.10x64 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.10x64 latest 0b96c14dafcd 4 months ago 270.3 MB
1.5.2持久化容器
export命令用于持久化容器
dockerexport>/tmp/export.tar
1.5.3持久化镜像
Save命令用于持久化镜像
dockersave镜像ID>/tmp/save.tar
1.5.4导入持久化container
删除container 2161509ff65e
导入export.tar文件
[root@localhost /]# cat /tmp/export.tar | docker import - export:
latest
af19a55ff0745fb0a68655392d6d7653c29460d22d916814208bbb9626183aaa
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
export latest af19a55ff074 34 seconds ago 270.3 MB
chug/ubuntu12.10x64_2 latest daa11948e23d 20 minutes ago 270.3 MB
chug/ubuntu12.10x64 latest 0b96c14dafcd 4 months ago 270.3 MB
1.5.5导入持久化image
删除image daa11948e23d
导入save.tar文件
[root@localhost /]# docker load < /tmp/save.tar
对image打tag
[root@localhost /]# docker tag daa11948e23d load:
tag
1.5.6export-import与save-load的区别
导出后再导入(export-import)的镜像会丢失所有的历史,而保存后再加载(save-load)的镜像没有丢失历史和层(layer)。
这意味着使用导出后再导入的方式,你将无法回滚到之前的层(layer),同时,使用保存后再加载的方式持久化整个镜像,就可以做到层回滚。
(可以执行dockertag来回滚之前的层)。
1.5.7一些其它命令
dockerlogs$CONTAINER_ID#查看docker实例运行日志,确保正常运行
dockerinspect$CONTAINER_ID#dockerinspect查看image或container的底层信息
dockerbuild寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image
dockerbuild-trepo[:
tag]同上,可以指定repo和可选的tag
dockerbuild-<使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image
dockerport查看本地哪个端口映射到container的指定端口,其实用dockerps也可以看到
1.6一些使用技巧
1.6.1docker文件存放目录
Docker实际上把所有东西都放到/var/lib/docker路径下了。
[root@localhost docker]# ls -F
containers/ devicemapper/ execdriver/ graph/ init/ linkgraph.db repositories-devicemapper volumes/
containers目录当然就是存放容器(container)了,graph目录存放镜像,文件层(filesystemlayer)存放在graph/imageid/layer路径下,这样我们就可以看看文件层里到底有哪些东西,利用这种层级结构可以清楚的看到文件层是如何一层一层叠加起来的。
1.6.2查看root密码
docker容器启动时的root用户的密码是随机分配的。
所以,通过这种方式就可以得到容器的root用户的密码了。
docker logs 5817938c3f6e 2>&1 | grep 'User:
' | tail -n1