zoukankan      html  css  js  c++  java
  • docker 8 docker的镜像命令

        先回顾一下容器、存储、镜像三者之间的关系。 

        我们知道docker的logo是一条大鲸鱼背上驮着集装箱。那我们对应到docker如下: 

        1)蓝色的大海里面------->宿主机系统比如我笔记本windows 7; 

        2)鲸鱼---------->docker环境(yum install docker-ce) 

        3)集装箱-------->容器实例(from 来自我们镜像模板) 

    docker images

    功能:列出本地主机上的镜像

    [root@t-docker chenzx]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    test_web            latest              58b7fb33ace5        3 days ago          959MB

    选项说明:

    REPOSITORY:表示镜像的仓库源
    tag:镜像的标签
    image id:镜像id
    created:镜像的创建时间
    size:镜像大小
    

        同一个仓库源可以拥有多个tag,代表这个仓库源的不同版本,我们使用REPOSITORY:TAG来定义不同的镜像。如果你不指定一个镜像的版本标签,例如你只使用centos,docker就默认使用docker:latest镜像

    OPTIONS说明:

        -a:列出本地所有的镜像(含中间映像层,因为镜像是分层的,-a会把所有层都显示出来)
        -q:只显示镜像id。
        --digests:显示镜像的摘要信息
        --no-trunc:显示完整的镜像信息,即IMAGE ID会显示完整 

    docker search

        功能:查找某个镜像名字 

        网站:注意,即使你配置了阿里云加速,docker search命令查找的网站也是是 https://hub.docker.com。阿里云加速只是下载时才会被用到。

     [root@t-docker chenzx]# docker search tomcat

    画外音:仓库就是存放镜像的地方。 

        -s:查找starts超过多少的镜像

    [root@t-docker chenzx]# docker search -s 30  tomcat
    Flag --stars has been deprecated, use --filter=stars=3 instead
    NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    tomcat              Apache Tomcat is an open source implementati…   1996                [OK]                
    tomee               Apache TomEE is an all-Apache Java EE certif…   55                  [OK]                
    dordoka/tomcat      Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base…   49                                      [OK
    

         -no-trunc:不减掉描述信息,即显示完整的描述信息

    [root@t-docker chenzx]# docker search -s 30 --no-trunc tomcat
    Flag --stars has been deprecated, use --filter=stars=3 instead
    NAME                DESCRIPTION                                                                                            STARS               OFFICIAL            AUTOMATED
    tomcat              Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies   1996                [OK]                
    tomee               Apache TomEE is an all-Apache Java EE certified stack where Apache Tomcat is top dog.                  55                  [OK]                
    dordoka/tomcat      Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 based docker container.                                        49                                      [OK] 

    docker pull

        功能:下载镜像 

    [root@t-docker chenzx]# docker search -s 30 tomcat
    Flag --stars has been deprecated, use --filter=stars=3 instead
    NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    tomcat              Apache Tomcat is an open source implementati…   1996                [OK]                
    tomee               Apache TomEE is an all-Apache Java EE certif…   55                  [OK]                
    dordoka/tomcat      Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base…   49                                      [OK]
    [root@t-docker chenzx]# 
    [root@t-docker chenzx]# docker pull tomcat  #相当于tomcat:latest下载最新版本
    [root@t-docker chenzx]# docker pull 192.168.0.106:5000/hello-world:v1.2  #这个表示指定仓库源下载镜像

    docker rmi

        功能:删除某个镜像 

        删除单个:docker rmi -f 镜像id 

    [root@t-docker chenzx]# docker rmi hello-world 
    Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 85296d21d05c is using its referenced image 2cb0d9787c4d
    [root@t-docker chenzx]# docker rmi -f hello-world  #如果容器正在运行会删不掉,可以加-f强制删除
    Untagged: hello-world:latest
    Untagged: hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
    Deleted: sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34
    

        删除多个:docker rmi -f 镜像1:tag 镜像2:tag,不写tag,删除的就是latest 

        删除全部:docker rmi -f $(docker images -qa)

    docker save and load(保存镜像和加载镜像)

        功能: 使用 docker save 命令可以将镜像保存为归档文件。 

                   使用 docker load  命令可以将保存的镜像加载到本地docker里面。 

         比如我们希望保存这个 tomcat 镜像。

    [root@localhost ~]# docker image ls tomcat
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    tomcat              latest              168588387c68        2 weeks ago         463MB
    

        保存镜像的命令为:

    [root@localhost ~]# docker save tomcat | gzip > tomcat.tar.gz
    

          然后我们将 alpine-latest.tar.gz 文件复制到了到了另一个机器上,可以用下面这个命令加 
    载镜像:

    [root@web]# docker load -i tomcat.tar.gz 
    13d5529fd232: Loading layer [==================================================>]  105.6MB/105.6MB
    abc3250a6c7f: Loading layer [==================================================>]  24.07MB/24.07MB
    578414b395b9: Loading layer [==================================================>]  8.005MB/8.005MB
    8be692af5632: Loading layer [==================================================>]  2.172MB/2.172MB
    699c7914defb: Loading layer [==================================================>]  3.584kB/3.584kB
    73a5184b491e: Loading layer [==================================================>]  1.536kB/1.536kB
    a6414350cc66: Loading layer [==================================================>]  316.9MB/316.9MB
    2a26f9e31825: Loading layer [==================================================>]   2.56kB/2.56kB
    8bf71984452f: Loading layer [==================================================>]  1.893MB/1.893MB
    54b63532b965: Loading layer [==================================================>]  18.76MB/18.76MB
    9a69d1970703: Loading layer [==================================================>]   2.56kB/2.56kB
    Loaded image: tomcat:latest
    [root@web]# 
    

          如果我们结合这两个命令以及 ssh 甚至 pv 的话,利用 Linux 强大的管道,我们可以写一 
    个命令完成从一个机器将镜像迁移到另一个机器,并且带进度条的功能: 
          docker save < 镜像名 > | bzip2 | pv | ssh < 用户名 >@< 主机名 'cat | docker load'

    [root@localhost ~]# docker save tomcat | bzip2 | pv |ssh root@192.168.122.135 'cat | docker load'
    
    [root@web yum.repos.d]# docker images -a
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    tomcat              latest              168588387c68        2 weeks ago         463MB
    

     安装pv

    Linux安装pv
    
    PV全名为Pipe Viewer,利用它我们可以查看到命令执行的进度。
    
    安装教程
    
    在yum.conf配置文件中添加:
    [ivarch]
    name=RPMs from ivarch.com
    baseurl=http://www.ivarch.com/programs/rpms/$basearch/
    enabled=1
    gpgcheck=1
    
    当你在安装过程中出现“Public key for pv-1.6.6-1.x86_64.rpm is not installed”时,先执行:rpm --import http://www.ivarch.com/personal/public-key.txt在执行yum install pv就能安装成功了。

    docker tag

        功能:给镜像打标签:

    [root@k8s-node3 ~]# docker images
    REPOSITORY                                    TAG                 IMAGE ID            CREATED             SIZE
    mysql                                         5.7.22              6bb891430fb6        3 months ago        372 MB
    [root@k8s-node3 ~]# docker tag mysql:5.7.22 docker-registry:5000/mysql:5.7.22
    [root@k8s-node3 ~]# docker images
    REPOSITORY                                    TAG                 IMAGE ID            CREATED             SIZE
    docker-registry:5000/mysql                    5.7.22              6bb891430fb6        3 months ago        372 MB
    mysql                                         5.7.22              6bb891430fb6        3 months ago        372 MB
    

        删除镜像的标签:

    [root@k8s-node3 ~]# docker rmi -f  docker-registry:5000/mysql:5.7.22
    Untagged: docker-registry:5000/mysql:5.7.22
    
  • 相关阅读:
    记一次渗透测试(5)
    记一次渗透实战(一)
    Spring IOC/DI
    Mysql 索引
    Mysql 存储过程
    Mysql 视图
    Mysql 用户和权限
    Mysql 事务
    Mysql 常用函数
    Mysql 子查询
  • 原文地址:https://www.cnblogs.com/charon2/p/10423807.html
Copyright © 2011-2022 走看看