zoukankan      html  css  js  c++  java
  • Docker03 Docker镜像及操作

    目录

    一、搜索及拉取镜像

    1.1 搜索镜像

    1.1.1 通过命令搜索

    docker search <imagesName>
    
    # 案例:搜索Nginx镜像
    ```powershell
    docker search Nginx
    

    使用-f参数实现定制返回信息:

    1. is-automated=(true|false) # 是否是自动构建
    2. is-offical=(true|false) # 是否是官方构建
    3. stars= # stars数目大于多少
    # 搜索stars超过20并且是官方构建的Ubuntu镜像
    docker search -f stars=20 -f is-offical=true Nginx
    

    --limit int

            限制显示数量

    1.1.2 web搜索

            主页地址

    1.2 查看镜像信息

    1.2.1 查看镜像简单信息

    docker images Ubuntu:14.04
    

    1.2.2 查看镜像详细信息

    docker inspect Ubuntu:14.04
    

    1.3 拉取镜像

    1.3.1 语法

    # 如果没有指定tag,默认拉取latest标签,但是latest意味着可能不稳定
    docker pull <imagesName:tag>
    
    # 对于私有仓库,可以执行镜像地址
    docker pull localhost:5000<imagesName:tag>
    

    1.3.2 案例

    docker pull Ubuntu:14.04
    

    二、创建镜像

    2.1 通过Dockerfile创建镜像

    2.2.1 简单案例

            详情见6。

    2.2.1 与GitHub连接自动创建镜像

            后面会单独写一章,目前只是简单介绍一下思路:

    1. 登陆docker hub后,在个人头像的下拉菜单下找到settings
    2. 找到Linked Acounts & Services,找打Link GitHub,连接Docker Hub与个人Git Hub
    3. 回到docker hub主页,在Create下拉菜单下找到创建自动构建,后根据提示创建,创建后,当Git Hub上对应仓库有改动时,Docker Hub会自动构建镜像

    2.2 通过docker commit提交容器为镜像

            commit命令把当前容器打包为镜像,这样提交的镜像就会保存容器内的数据,而且第三方无法获得镜像的Dockerfile,也就无法再构建一个完全一模一样的镜像出来,所以不推荐使用这种方式。但是有时候需要使用commit来保存容器。

    2.2.1 docker commit参数

    -a : 添加作者信息,方便维护
    -c :修改Dockerfile指令,目前支持的有CMD | ENTRYPOINT | ENV | EXPOSE | LABEL | ONBUILD | USER | VOLUME | WORKDIR
    -m :类似git commit -m 提交修改信息
    -p :暂停正在提交(commit)操作
    

    2.2.2 案例

    # 启动一个容器
    docker run -d --name=test hello
    
    # 提交镜像,镜像名称是gupan/test
    docker commit test gupan/test
    
    # 运行提交的镜像
    docker run --rm gupan/test
    

    三、导入和导出镜像

            两台主机之间传输镜像,有时候,并不希望发布镜像到互联网上,而是自己搭建的私有镜像仓库,需要就需要镜像的导入和导出

    3.1 镜像导出到本地文件系统

    3.1.1 基本语法

    docker save -o <导出镜像路径> <镜像名>
    

    案例:

    [gupan@localhost hello]$ docker save -o hello.tar hello 
    [gupan@localhost hello]$ ls
    Dockerfile  hello.tar
    [gupan@localhost hello]$ 
    

    3.2 导入本地文件到镜像

    3.2.1 基本语法

    # 导入镜像时不必指定镜像名称
    
    docker load -i <本地文件名>
    或
    docker load < <本地文件名>
    

    案例

    docker load -i hello.tar
    

    四、发布镜像

    4.1 发布镜像到Docker Hub

    1. docker login登陆到Docker Hub
    [gupan@localhost hello]$ docker login
    Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head .com to create one.
    Username: gupan
    Password: 
    Login Succeeded
    [gupan@localhost hello]$ 
    
    1. 用docker push推送镜像
    docker push gupan/test
    

    案例

    [gupan@localhost hello]$ docker push gupan/test
    The push refers to repository [docker.io/gupan/test]
    cd7100a72410: Pushed 
    latest: digest: sha256:74c51f21459aeb63edd0709f3092a7391ed902db518e560a8f21645d09fc59bf size: 528
    [gupan@localhost hello]$ 
    

    推送成功案例

    五、删除镜像

    5.1 删除本地镜像

    5.1.1 命令

    docker rmi <镜像名:标签>
    # 如果没有指定镜像的标签,那么会删除latest标签
    # 正在使用的镜像删除时会报错
    # 可以使用-f来强制删除镜像
    

    5.1.2 案例

    docker rmi test
    

    5.1.3 使用技巧

    # 删除所有未打gupan标签的镜像
    docker rmi $(docker images -q -f gupan=true)
    
    # 删除所有镜像
    docker rmi $(docker images -q)
    

    5.2 删除仓库镜像

            目前没有命令来进行操作,但是可以通不过API和可视化界面来操作

    六、镜像内容以及存储方式详解

    6.1 镜像内容

            解压得到的hello.tar镜像,得到内容如下:

    [gupan@localhost example]$ tar -xvf  hello.tar 
    
    # 解压内容如下
    [gupan@localhost example]$ ll
    total 4324
    -rw-r--r--. 1 gupan gupan    1748 May 10 20:25 4b1c2e073c2341eb099e2ca2041d59f379d5eba3c0fff40bfccdb86fe72d68c0.json
    drwxr-xr-x. 2 gupan gupan      47 May 10 20:25 613c5db5c3a9ab4520b4902c745d4ed55950d341edbd601401efd743ead5ee4b
    -rw-r--r--. 1 gupan gupan     201 Jan  1  1970 manifest.json
    -rw-r--r--. 1 gupan gupan      88 Jan  1  1970 repositories
    [gupan@localhost example]$ 
    
    

            可以看到解压的得到了乱码一样的文件夹和json格式的数据。一个镜像文件包含数据以及必要的元数据,其中哪些乱码一样的文件夹就是数据(也叫层,每一层都是由依据Dockerfile指令生成,一个镜像就是不停的在上一层的基础上叠加上去的),而元数据以json文件保存。json文件描述了数据之间的关系以及容器的配置信息。

    6.2 Docker本地如何存储镜像

            Docker本地镜像存储在 /var/lib/docker 文件夹下

    [root@localhost ~]# cd /var/lib/docker/
    [root@localhost docker]# ll
    total 0
    drwx------. 2 root root 23 May 10 18:29 builder
    drwx--x--x. 3 root root 19 May 10 18:29 containerd
    drwx------. 3 root root 77 May 10 22:10 containers
    drwx------. 5 root root 50 May 10 20:00 devicemapper # 存放容器信息
    drwx------. 3 root root 25 May 10 18:29 image  # 存储镜像信息
    drwxr-x---. 3 root root 18 May 10 18:29 network #存放网络信息
    drwx------. 4 root root 30 May 10 18:29 plugins
    drwx------. 2 root root  6 May 10 18:29 runtimes
    drwx------. 2 root root  6 May 10 18:29 swarm # 存放集群信息
    drwx------. 2 root root  6 May 10 22:22 tmp
    drwx------. 2 root root  6 May 10 18:29 trust
    drwx------. 2 root root 24 May 10 18:29 volumes # 存放数据卷信息
    [root@localhost docker]# 
    

            本地存储的镜像数据与层数据在image文件夹中分开存储,imagedb存储了本地全部镜像的元数据,layerdb存储了本地镜像的全部镜像层

    使用docker inspect hello查看hello镜像的信息:

            Docker首先通过iamge的元数据得知layer的全部ID,再根据元数据梳理出顺序,最后使用联合挂载技术还原容器启动所需要的rootfs和基本配置信息。运行的容器实际就像是在这些镜像层之上再建立一个新的层

    6.3 联合挂载介绍

            联合挂载会把多个目录挂载到同一个目录下,并对外显示这些目录的整合形态(Docker的AUFS就是一种联合文件系统)。

    注意写时复制机制对镜像文件大小的影响

  • 相关阅读:
    【SSH网上商城项目实战15】线程、定时器同步首页数据(类似于CSDN博客定期更新排名)
    【SSH网上商城项目实战14】商城首页UI的设计
    Spring工具类:WebApplicationContextUtils
    多线程技术: 两个线程交替打印奇数和偶数
    常见的几种异常类型Exception
    【SSH网上商城项目实战13】Struts2实现文件上传功能
    【SSH网上商城项目实战12】添加和更新商品功能的实现
    【SSH网上商城项目实战11】查询和删除商品功能的实现
    【SSH网上商城项目实战10】商品类基本模块的搭建
    如何防止通过IP地址访问Tomcat管理页面
  • 原文地址:https://www.cnblogs.com/gupan/p/9022382.html
Copyright © 2011-2022 走看看