您的位置:首页 > 运维架构 > Docker

CentOS系统下docker的安装配置及使用详解

2015-10-14 11:09 1066 查看
1 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

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

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

2 镜像的获取与容器的使用

镜像可以看作是包含有某些软件的容器系统,比如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.10x32 Ubuntu Quantal Quetzal 12.10 32bit base i... 0

chug/ubuntu12.10x64 Ubuntu Quantal Quetzal 12.10 64bit base i... 0

下载镜像

docker pull <image> :从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 ID>:删除一个或多个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> :开启/停止/重启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
3 持久化容器与镜像
3.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.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

3.2 持久化容器

export命令用于持久化容器

docker export <CONTAINER ID> >/tmp/export.tar



3.3 持久化镜像

Save命令用于持久化镜像

docker save 镜像ID > /tmp/save.tar



3.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

3.5 导入持久化image

删除image daa11948e23d



导入save.tar文件

[root@localhost /]# docker load < /tmp/save.tar



对image打tag

[root@localhost /]# docker tag daa11948e23d load:tag



3.6export-import与save-load的区别

导出后再导入(export-import)的镜像会丢失所有的历史,而保存后再加载(save-load)的镜像没有丢失历史和层(layer)。这意味着使用导出后再导入的方式,你将无法回滚到之前的层(layer),同时,使用保存后再加载的方式持久化整个镜像,就可以做到层回滚。(可以执行dockertag <LAYER ID> <IMAGE NAME>来回滚之前的层)。



3.7 一些其它命令

docker logs $CONTAINER_ID#查看docker实例运行日志,确保正常运行

docker inspect $CONTAINER_ID #docker inspect<image|container> 查看image或container的底层信息

docker build <path>寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image

docker build -t repo[:tag] 同上,可以指定repo和可选的tag

docker build - < <dockerfile>使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image

docker port <container> <container port>查看本地哪个端口映射到container的指定端口,其实用docker ps 也可以看到

4 一些使用技巧

4.1 docker文件存放目录

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路径下,这样我们就可以看看文件层里到底有哪些东西,利用这种层级结构可以清楚的看到文件层是如何一层一层叠加起来的。
4.2 查看root密码

docker容器启动时的root用户的密码是随机分配的。所以,通过这种方式就可以得到容器的root用户的密码了。

docker logs 5817938c3f6e 2>&1 | grep 'User: ' | tail -n1

****************************************************************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: