Docker 镜像与容器管理(2)


title: Docker 镜像与容器管理(2)
date: 2018-12-14 17:04:05
tags:

  • Docker
    categories: Docker
    copyright: true
    ---

Docker是基于Go语言实现的开源容器项目,Docker让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何接口,Docker诞生于2013年年初,最初发起者是dotCloud公司.Docker自开源后受到广泛的关注和讨论,目前已有多个相关项目(包括Docker三剑客、Kubernetes等),逐渐形成了围绕Docker容器的生态体系,由于Docker在业界造成的影响力实在太大,dotCloud公司后来也直接改名为Docker Inc,并专注于Docker相关技术和产品的开发.



镜像与容器简介

Docker的大部分操作都围绕着它的三大核心概念:镜像、容器、仓库而展开.因此,准确把握这三大核心概念对于掌握Docker技术尤为重要,在docker中,我们重点关注的就是镜像和容器了.因为在实际应用中,我们封装好镜像,然后通过镜像来创建容器,在容器运行我们的应用就好了.而server端掌控网络和磁盘,我们不用去关心,启动docker sever 和 docker client都是一条命令的事情.

镜像(Image): Docker镜像类似于虚拟机镜像,可以将它理解为一个只读的模板.例如,一个镜像可以包含一个基本的操作系统环境,里面仅安装了一个应用程序,可以把它称为一个镜像,镜像是创建Docker容器的基础.通过版本管理和增量的文件系统,Docker提供了一套十分简单的机制来创建和更新现有的镜像,用户甚至可以从网上下载一个已经做好的应用镜像,并直接使用.

容器(Container): Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用.容器是从镜像创建的应用运行实例.可以将其启动、开始、停止、删除,而这些容器都是彼此相互隔离的、互不可见的.可以把容器看做是一个简易版的Linux系统环境,以及运行在其中的应用程序打包而成的盒子.

镜像启动后,都是一堆layer的统一视角,唯一的却别是镜像最上面那一层是只读的,不可以修改,但是容器最上面一层是rw的,提供给用户操作.

仓库(repository): Docker仓库类似于代码仓库,它是Docker集中存放镜像文件的场所.根据所存储的镜像公开分享与否,Docker仓库可以分为公开仓库(Public)和私有仓库(Private)两种形式.目前,最大的公开仓库是官方提供的Docker Hub,其中存放了数量庞大的镜像供用户下载.国内不少云服务提供商(如网易云、阿里云等)也提供了仓库的本地源,可以提供稳定的国内访问.

管理Docker镜像

镜像是Docker三大核心概念中最为重要的,自Docker诞生之日起,镜像就是相关社区最为热门的关键词,Docker运行容器前需要本地存在对应的镜像,如果镜像没保存在本地,Docker会尝试先从默认Docker Hub仓库下载,用户也可以通过配置,使用自定义的镜像仓库.

下面例子将围绕镜像这一核心概念的具体操作,包括如何使用pull命令从Docker Hub仓库中下载镜像到本地,如何查看本地已有的镜像信息和管理镜像标签,如何在远端仓库使用search命令进行搜索和过滤,如何删除镜像标签和镜像文件,如何创建用户定制的镜像并且保存为外部文件.最后,还介绍如何往Docker Hub仓库中推送自己的镜像.

◆查询本地镜像◆

使用docker images命令可以列出本地主机上已有镜像的基本信息,还可以使用条件过滤出你想要看得到的相关镜像文件的信息.

[root@localhost ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

上面信息的参数解释,在列出的信息中可以看到以下几个字段信息:

REPOSITORY=来自于哪个仓库,比如hello-world仓库.
TAG=镜像标签信息,latest表示不同版本信息.
IMAGE ID=镜像唯一ID号,此处唯一.
CREATED=创建时间信息,镜像最后的更新时间.
SIZE=镜像大小,优秀的镜像往往体积都较小,hello-world很优秀.

其中镜像的ID信息十分重要,它唯一标识了镜像.在使用镜像ID的时候,一般可以使用该ID的前若干个字符组成的可区分串来替代完整的ID,比如后期我们要删除一个镜像时无需写出全部镜像ID.

TAG信息用来标记来自同一个仓库的不同镜像,例如ubuntu仓库中有多个镜像,通过TAG信息来区分发行版本,包括13.04、14.04、16.04等标签.

镜像大小信息只是表示该镜像的逻辑体积大小,实际上由于相同的镜像层本地只会存储一份,物理上占用的存储空间会小于各镜像的逻辑体积之和.

实例1: 通过使用-a --all=true|false参数,列出所有的镜像文件(包括临时文件),默认为否.因为我这里只有一个镜像所以只有这一个啦.

[root@localhost ~]# docker images --all=true

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

实例2: 通过使用--digests=true|false,列出镜像的数字摘要值,默认为否.

[root@localhost ~]# docker images --digests=true

REPOSITORY          TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE
hello-world         latest              sha256:0add3ace90ecb4adbf7777e9a   4ab4c602aa5e        3 months ago        1.84kB

实例3: 通过使用--quiet=true|false,仅输出ID信息,默认为否.

[root@localhost ~]# docker images -q
4ab4c602aa5e

[root@localhost ~]# docker images --quiet=false

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

◆查询网络镜像◆

使用docker search命令可以搜索远端仓库中共享的镜像,默认搜索官方仓库中的镜像.用法为docker search TERM.

实例1: 使用search搜索一个Centos镜像文件.

[root@localhost ~]# docker search centos

NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5048                [OK]            
ansible/centos7-ansible            Ansible on Centos7                              119                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86…   102                
......

实例2: 仅显示自动创建的镜像,默认为否.

[root@localhost ~]# docker search --automated=true centos
Flag --automated has been deprecated, use --filter=is-automated=true instead
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ansible/centos7-ansible           Ansible on Centos7                              119                                     [OK]
jdeathe/centos-ssh                CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86…   102                                     [OK]
consol/centos-xfce-vnc            Centos container with "headless" VNC session…   73                                      [OK]
imagine10255/centos6-lnmp-php56   centos6-lnmp-php56                              48                                      [OK]

实例3: 搜索所有自动创建的评价为1+的带nginx关键字的镜像.

[root@localhost ~]# docker search --automated -s 3 nginx

Flag --automated has been deprecated, use --filter=is-automated=true instead
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1488                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   663    

◆拉镜像到本地◆

可以使用docker pull命令直接从Docker Hub镜像源来下载镜像,该命令的格式为docker pull NAME[:TAG].其中NAME是镜像仓库的名称,TAG是镜像的标签,通常情况下,描述一个镜像需要包括"名称+标签".

实例: 通过pull命令获取一个Centos系统镜像.

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

[root@localhost ~]# docker pull centos

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        8 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

◆给镜像加标签◆

为了方便在后续工作中使用特定镜像,还可以使用docker tag命令来为本地镜像任意添加新的标签.

实例: 为Centos镜像添加一个新的mycentos:latest镜像标签.

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        8 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

[root@localhost ~]# docker tag centos:latest mycentos:latest

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        8 days ago          202MB
mycentos            latest              1e1148e4cc2c        8 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

上图可看到,当再次使用docker images列出本地主机上镜像信息,可以看到多了一个拥有mycentos:latest标签的镜像,细心的你可能注意到,这些mycentos:latest镜像的ID跟centos:latest完全一致,它们实际上指向同一个镜像文件,只是别名不同而已.docker tag命令添加的标签实际上起到了类似链接的作用.

◆查询镜像详情◆

使用docker inspect命令可以获取该镜像的详细信息,包括制作者、适应架构、各层的数字摘要等.

[root@localhost ~]# docker inspect hello-world

[
    {
        "Id": "sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f",
        "RepoTags": [
            "hello-world:latest"
        ],
        "RepoDigests": [
            "hello-world@sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788"
        ],
.....

上面的输出有很多,只不过我这里简单显示了,如果我们只要其中一项内容时,可以使用参数-f来指定你要打印的数据,例如下面我们来演示一下获取当前镜像的Id这个字段的数据.

[root@localhost ~]# docker inspect -f {{".Id"}} hello-world

sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f

◆查询镜像分层◆

既然镜像文件由多个层组成,那么怎么知道各个层的内容具体是什么呢?这时候可以使用history子命令,该命令将列出各层的创建信息.注意过长的命令被自动截断了,可以使用前面提到的--no-trunc选项来输出完整命令.

[root@localhost ~]# docker history centos:latest

IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
1e1148e4cc2c        8 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>           8 days ago          /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B
<missing>           8 days ago          /bin/sh -c #(nop) ADD file:6f877549795f4798a…   202MB

◆删除指定镜像◆

使用docker rmi命令可以删除镜像,其中IMAGE可以为标签或ID,如果要强制删除可加-f这个选项.

删除镜像: 通过rmi命令删除mycentos这个标签.

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        8 days ago          202MB
mycentos            latest              1e1148e4cc2c        8 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

[root@localhost ~]# docker rmi mycentos:latest
Untagged: mycentos:latest

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        8 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

强制删除: 强制删除系统全部镜像.

[root@localhost ~]# docker rmi -f $(docker images -q)

Deleted: sha256:1e1148e4cc2c148c6890a18e3b2d2dde41a6745ceb4e5fe94a923d811bf82ddb
Deleted: sha256:071d8bd765171080d01682844524be57ac9883e53079b6ac66707e192ea25956
Untagged: hello-world:latest
Untagged: hello-world@sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Deleted: sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f

◆镜像导入导出◆

导出操作: 通过save 镜像ID >导出centos镜像.

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        8 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

[root@localhost ~]# docker save 1e1148e4cc2c > /root/centos.tar

[root@localhost ~]# ls
centos.tar

导入操作: 通过load < 文件名导入centos镜像.

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

[root@localhost ~]# docker load < centos.tar

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              1e1148e4cc2c        8 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

◆镜像命令速查◆

[root@localhost ~]# docker info                       #查询守护进程的系统资源设置
[root@localhost ~]# docker search                     #仓库镜像的查询
[root@localhost ~]# docker pull                       #仓库镜像的下载
[root@localhost ~]# docker images                     #本地镜像的查询
[root@localhost ~]# docker rmi                        #本地镜像的删除
[root@localhost ~]# docker rmi -f $(docker images -q) #强制删除全部镜像(Image)
[root@localhost ~]# docker rmi -f <image id>          #强制删除指定镜像(Image)
[root@localhost ~]# docker history 镜像名              #查询镜像的分层
[root@localhost ~]# docker save 镜像ID > /root/*.tar  #镜像的导出
[root@localhost ~]# docker load < /root/*.tar         #镜像的导入


管理Docker容器

容器是Docker的另一个核心概念,简单来说,容器是镜像的一个运行实例.所不同的是,镜像是静态的只读文件,而容器带有运行时需要的可写文件层.如果认为虚拟机是模拟运行的一整套操作系统和跑在上面的应用,那么Docker容器就是独立运行的一个应用,以及它们必需的运行环境.

下面的例子将具体介绍围绕容器的重要操作,包括创建一个容器、启动容器、终止一个容器、进入容器内执行操作、删除容器和通过导入导出容器来实现容器迁移等.

◆创建容器◆

从现在开始,忘掉臃肿的虚拟机吧,对容器进行操作就跟直接操作应用一样简单、快速.Docker容器实在太轻量级了,用户可以随时创建或删除容器.

新建容器: 可以使用docker create命令新建一个容器,使用docker create命令新建的容器处于停止状态,可以使用docker start命令来启动它.

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        9 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

[root@localhost ~]# docker create -it centos:latest
23c881ac33c526e60811978a418be92c6a022c106e6d59d989fb7b932dc3473a

[root@localhost ~]# docker start 23c881ac33c5
23c881ac33c5

新建并启动: 除了创建容器后通过start命令来启动,也可以直接新建并启动容器.所需要的命令主要为docker run,等价于先执行docker create命令,再执行docker start命令.

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        9 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

[root@localhost ~]# docker run centos:latest

守护态运行: 更多的时候,需要让Docker容器在后台以守护态Daemonized形式运行.此时,可以通过添加-d参数来实现.

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        9 days ago          202MB
hello-world         latest              4ab4c602aa5e        3 months ago        1.84kB

[root@localhost ~]# docker run -itd ubuntu:latest
540fd59ee8899a38c4302d83549bd113ad159064ec41c9475a773cbc0fd2dfb8

[root@localhost ~]# docker run -d centos:latest /bin/sh
505a728a2bed9e96b3e4615c4e528bd55285a856dc201bb50d4ed5c9e0a52566

[root@localhost ~]# docker run -d centos:latest /bin/sh -c "echo hello"
6c8fc14a6637928442b768bee0b2d3af800464192e7fce295f39ccdd91b73572

◆终止容器◆

可以使用docker stop来终止一个运行中的容器,也可以使用docker kill命令干掉一个容器.

stop终止容器: 指定通过stop终止一个容器.

[root@localhost ~]# docker stop 540fd59ee889
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
540fd59ee889        ubuntu:latest       "/bin/bash"         6 minutes ago       Up 6 minutes                            festive_liskov

[root@localhost ~]# docker stop 540fd59ee889
540fd59ee889

kill终止容器:docker kill命令会直接发送SIGKILL信号来强行终止容器.

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
84da1ad9f06c        centos:latest       "/bin/bash"         33 seconds ago      Up 32 seconds                           hungry_bhabha

[root@localhost ~]# docker kill 84da1ad9f06c
84da1ad9f06c

◆进入容器◆

在使用-d参数时,容器启动后会进入后台,用户无法看到容器中的信息,也无法进行操作.
这个时候如果需要进入容器进行操作,有多种方法,包括使用官方的attach或exec命令,以及第三方的nsenter工具等.下面分别介绍一下.

attach进入容器: attach是Docker自带的命令,下面我们使用它来进入容器的内部吧.

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
540fd59ee889        ubuntu:latest       "/bin/bash"         About a minute ago   Up About a minute                       festive_liskov
300560ca1c88        centos:latest       "/bin/bash"         3 minutes ago        Up 3 minutes                            ecstatic_raman

[root@localhost ~]# docker attach 300560ca1c88
[root@300560ca1c88 /]#

但是使用attach命令有时候并不方便,当多个窗口同时用attach命令连到同一个容器的时候,所有窗口都会同步显示.当某个窗口因命令阻塞时,其他窗口也无法执行操作了,接着下面的命令就更好一些了.

exec进入容器: Docker从1.3.0版本起提供了一个更加方便的exec命令,可以在容器内直接执行任意命令.

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
540fd59ee889        ubuntu:latest       "/bin/bash"         3 minutes ago       Up 3 minutes                            festive_liskov

[root@localhost ~]# docker exec -it 540fd59ee889 /bin/bash
root@540fd59ee889:/#

可以看到,一个bash终端打开了,在不影响容器内其他应用的前提下,用户可以很容易与容器进行交互,通过指定-it参数来保持标准输入打开,并且分配一个伪终端.通过exec命令对容器执行操作是最为推荐的方式.

◆删除容器◆

可以使用docker rm命令来删除处于终止或退出状态的容器.

rm 删除容器: 通过rm -f命令强制删除一个容器.

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
fa6110bdb3df        centos:latest       "/bin/bash"         3 seconds ago       Up 2 seconds                            eager_mirzakhani

[root@localhost ~]# docker rm -f fa6110bdb3df
fa6110bdb3df

◆命令速查◆

[root@localhost ~]# docker run                      #容器的创建或启动
[root@localhost ~]# docker run --restart=always     #设置容器的自启动

[root@localhost ~]# docker ps                       #运行中的容器的查询
[root@localhost ~]# docker ps --no-trunc            #查看容器状态
[root@localhost ~]# docker start/stop               #容器启动/关闭
[root@localhost ~]# docker stop $(docker ps -a -q)  #停止所有运行中的容器(Container)
[root@localhost ~]# docker rm $(docker ps -a -q)    #删除全部容器(Container)

[root@localhost ~]# docker start/stop 镜像名         #通过容器别名启动/停止
[root@localhost ~]# docker inspect 镜像名            #查看容器所有基本信息
[root@localhost ~]# docker logs 镜像名               #查看容器日志
[root@localhost ~]# docker stats 镜像名              #查看容器所占用的系统资源

[root@localhost ~]# docker exec 容器名 容器内执行的命令#容器执行命令
[root@localhost ~]# docker exec -it 容器名 /bin/bash  #登入容器的bash
[root@localhost ~]# docker run -it 容器名 /bin/bash   #进入一个镜像


猜你喜欢

转载自www.cnblogs.com/LyShark/p/10872311.html