zoukankan      html  css  js  c++  java
  • Docker 镜像管理

    什么是镜像

    简单说,Docker镜像是一个不包含Linux内核而又精简的Linux操作系统。

    国内加速源

    默认是国外的源,下载会慢,可以国内的源提供下载速度:需要重启下docker

    curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://04be47cf.m.daocloud.io
    ======
    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://z6t3hb9x.mirror.aliyuncs.com"]
    }
    EOF
    ======
    #重启docker
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    

    工作原理

    当我们启动一个新的容器时,Docker会加载只读镜像,并在其之上添加一个读写层,并将镜像中的目录复制一份到

    /var/lib/docker/aufs/mnt/容器ID为目录下,我们可以使用chroot进入此目录。如果运行中的容器修改一个已经存在的文件,

    那么会将该文件从下面的只读层复制到读写层,只读层的这个文件就会覆盖,但还存在,这就实现了文件系统隔离,当删除容器后,读写层的数据将会删除,只读镜像不变。

    镜像文件存储结构

    docker相关文件存放在:/var/lib/docker目录下

    /var/lib/docker/aufs/diff # 每层与其父层之间的文件差异

    /var/lib/docker/aufs/layers/ # 每层一个文件,记录其父层一直到根层之间的ID,大部分文件的最后一行都已,表示继承来自同一层

    /var/lib/docker/aufs/mnt # 联合挂载点,从只读层复制到最上层可读写层的文件系统数据

    在建立镜像时,每次写操作,都被视作一种增量操作,即在原有的数据层上添加一个新层;所以一个镜像会有若干个层组成。

    每次commit提交就会对产生一个ID,就相当于在上一层有加了一层,可以通过这个ID对镜像回滚

    查看镜像

    [root@localhost ~]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    docker.io/hello-world latest 05a3bd381fc2 5 weeks ago 1.84
    

    选项说明:
    REPOSTITORY:表示镜像的仓库源
    TAG:镜像的标签
    IMAGE ID:镜像ID
    CREATED:镜像创建时间
    SIZE:镜像大小
    同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSTITORY:TAG 来定义不同的镜像。

    搜索镜像

    [root@localhost ~]# docker search centos  ##搜索方式docker search [镜像名字] (下载次数) (官方) (自动化)
    INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    docker.io docker.io/centos The official build of CentOS. 3734 [OK] 
    docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 102 [OK]
    

    --automated :只列出 automated build类型的镜像;
    --no-trunc :显示完整的镜像描述;
    -s :列出收藏数不小于指定值的镜像。

    docker search -s 10 tomcat
    docker search --automated tomcat
    docker search -s 10 --no-trunc tomcat
    

    下载镜像

    [root@linux-node1 ~]# docker pull centos:7    ##下载指定版本镜像
    7: Pulling from library/centos
    af4b0a2388c6: Pull complete 
    Digest: sha256:2671f7a3eea36ce43609e9fe7435ade83094291055f1c96d9d1d1d7c0b986a5d
    Status: Downloaded newer image for centos:7
    [root@linux-node1 ~]# docker pull nginx      ##下载nginx镜像
    Using default tag: latest
    latest: Pulling from library/nginx
    Digest: sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d
    Status: Image is up to date for nginx:latest
    [root@linux-node1 ~]# docker images          ##查看现有镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              7                   ff426288ea90        6 days ago          207MB
    nginx               latest              3f8a4339aadd        2 weeks ago         108MB
    

    导出镜像

    ## docker save -o [镜像名称] [镜像]
    
    [root@linux-node1 ~]# docker save -o centos.tar.gz centos
    [root@linux-node1 ~]# ls
    anaconda-ks.cfg  centos.tar.gz   #需要将docker导出为tar.gz,后面为镜像名称
    [root@linux-node1 ~]# docker save centos > /opt/centos.tar.gz
    [root@linux-node1 ~]# ls /opt/
    centos.tar.gz
    [root@linux-node1 ~]# docker image save nginx >nginx.tar.gz
    [root@linux-node1 ~]# du -sh nginx.tar.gz 
    108M	nginx.tar.gz
    

    导入镜像

    [root@linux-node1 ~]#docker load --input centos.tar 
    #使用input导入
    [root@linux-node1 ~]# docker load < nginx.tar #使用重定向导入
    [root@linux-node1 ~]# docker load < /opt/centos.tar.gz #导入本地镜像到docker镜像库
    

    删除镜像

    docker删除可以使用docker rmi 后面加上docker的ID

    [root@linux-node1 ~]# docker images            ##查看镜像
    REPOSITORY TAG IMAGE ID CREATED SIZE
    docker.io/nginx latest abf312888d13 2 weeks ago 181.5 MB
    docker.io/centos latest 0584b3d2cf6d 6 weeks ago 196.5 MB
    [root@linux-node1 ~]# docker rmi abf312888d13  ##删除镜像 提示:如果镜像已经创建了一个容器,那么将无法进行删除
    Untagged: docker.io/nginx:latest
    Deleted: sha256:abf312888d132e461c61484457ee9fd0125d666672e22f972f3b8c9a0ed3f0a1
    Deleted: sha256:21976f5d6f037350c076d6974b2ac97777c962869d5511855170dc5c926d5aac
    Deleted: sha256:f9719c3716279b47727a51595d1482506be469c581383cb529f4005ba9bf2aeb
    Deleted: sha256:fe4c16cbf7a4c70a5462654cf2c8f9f69778db280f235229bd98cf8784e878e4
    [root@linux-node1 ~]# docker images            ##查看镜像
    REPOSITORY TAG IMAGE ID CREATED SIZE
    docker.io/centos latest 0584b3d2cf6d 6 weeks ago 196.5 MB
    

    显示某个镜像的历史

    可以看到镜像的每一次
    -H :以可读的格式打印镜像大小和日期,默认为true;
    --no-trunc :显示完整的提交记录;
    -q :仅列出提交记录ID。

    [root@linux-node1 ~]# docker history nginx
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    3f8a4339aadd        2 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                 
    <missing>           2 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL [SIGTERM]         0B                 
    
  • 相关阅读:
    SQLAlchemy
    Redis
    Django框架详解
    nginx负载均衡(反向代理)
    Python 字典 fromkeys()方法
    docker容器的使用
    keepalived原理
    学习区块链必读的6本书
    MATLAB基础入门笔记
    思念是一种美丽的孤独
  • 原文地址:https://www.cnblogs.com/syavingcs/p/14009991.html
Copyright © 2011-2022 走看看