docker安装
docker安装与启动
安装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
将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
镜像的获取与容器的使用
镜像可以看作是包含有某些软件的容器系统,比如ubuntu就是一个官方的基础镜像,很多镜像都是基于这个镜像“衍生”,该镜像包含基本的ubuntu系统。再比如,hipache是一个官方的镜像容器,运行后可以支持http和websocket的代理服务,而这个镜像本身又基于ubuntu。
搜索镜像
docker search :在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.10x32 Ubuntu Quantal Quetzal 12.10 32bit base i... 0
chug/ubuntu12.10x64 Ubuntu Quantal Quetzal 12.10 64bit base i... 0
下载镜像
docker pull :从docker registry server 中下拉image
[root@localhost /]# docker pull chug/ubuntu12.10x64
查看镜像
- docker images: 列出images
- docker images -a: 列出所有的images(包含历史)
- docker images –tree:显示镜像的所有层(layer)
- docker rmi : 删除一个或多个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
<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.10x64:latest
[root@localhost /]# docker rmi <image ID>
使用镜像创建容器
[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:/#
查看容器
- 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.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
再次启动容器
- docker start/stop/restart :开启/停止/重启container
- docker start [container_id] :再次运行某个container (包括历史container)
- docker attach [container_id] :连接一个正在运行的container实例(即实例必须为start状态,可以多个窗口同时attach 一个container实例)
- docker start -i :启动一个container并进入交互模式(相当于先start,在attach)
- docker run -i -t /bin/bash :使用image创建container并进入交互模式, login shell是/bin/bash
- docker run -i -t -p :映射 HOST 端口到容器,方便外部访问容器内服务,host_port 可以省略,省略表示把 container_port 映射到一个动态端口。注:使用start是启动已经创建过得container,使用run则通过image开启一个新的container。
删除容器
- docker rm