• Docker 笔记(二)--镜像


    Docker 笔记(二)–镜像

    记录Docker 安装操作记录,便于查询。

    参考

    1. 链接: Docker 入门到实战教程(三)镜像和容器
    2. 链接: docker中daemon.json各配置详解
    3. 链接: docker容器/etc/docker/daemon.json配置文件详解
    4. 链接: docker官方文档

    环境

    Centos 7.9

    操作

    1.查看命令

    // 查看全部命令
    [root@centos7-18 image]# docker command --help 
    
    • 1
    • 2
    // 查看某一个命令,例如查看run
    [root@centos7-18 image]# docker run --help 
    
    • 1
    • 2

    2.查询镜像

    // 查询nginx命令
    [root@centos7-18 image]# docker search nginx
    NAME                               DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
    nginx                              Official build of Nginx.                         19234     [OK]       
    unit                               Official build of NGINX Unit: Universal Web …   17        [OK]       
    nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                   133
    nginx/nginx-ingress                NGINX and  NGINX Plus Ingress Controllers fo…   86                   
    nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter for NGINX and NGIN33                                                      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • Name:源名称
    • DESCRIPTION:描述
    • STARS:星(点赞)数量
    • OFFICIAL:是否官方发布
    • AUTOMATED:是否自动构建

    3.拉取镜像

    // 默认拉取最新版
    [root@centos7-18 image]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    a2abf6c4d29d: Pull complete 
    a9edb18cadd1: Pull complete 
    589b7251471a: Pull complete 
    186b1aaa4aa6: Pull complete 
    b4df32aa5a72: Pull complete 
    a0bcbecc962e: Pull complete 
    Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Status: Downloaded newer image for nginx:latest
    docker.io/library/nginx:latest                                                     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.查看镜像

    4.1. 查看所有镜像

    [root@centos7-18 image]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    nginx        latest    605c77e624dd   22 months ago   141MB
    redis        latest    7614ae9453d1   23 months ago   113MB                                                 
    
    • 1
    • 2
    • 3
    • 4
    • REPOSITORY:表示镜像的仓库源
    • TAG:标签
    • IMAGE ID:ID
    • CREATED:创建时间
    • SIZE:大小
      4.2. 查看镜像详细信息
    [root@centos7-18 image]# docker inspect nginx
    [
        {
            "Id": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85",
            "RepoTags": [
                "nginx:latest"
            ],
            "RepoDigests": [
                "nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31"
            ],
            "Parent": "",
            "Comment": "",
            "Created": "2021-12-29T19:28:29.892199479Z",
            "Container": "ca3e48389f7160bc9d9a892d316fcbba459344ee3679998739b1c3cd8e56f7da",
            "ContainerConfig": {
                "Hostname": "ca3e48389f71",
                "Domainname": "",
                "User": "",
                "AttachStdin": false,
                "AttachStdout": false,
                "AttachStderr": false,
                "ExposedPorts": {
                    "80/tcp": {}
                },
                "Tty": false,
                "OpenStdin": false,
                "StdinOnce": false,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                    "NGINX_VERSION=1.21.5",
                    "NJS_VERSION=0.7.1",
                    "PKG_RELEASE=1~bullseye"
                ],
                "Cmd": [
                    "/bin/sh",
                    "-c",
                    "#(nop) ",
                    "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
                ],
                "Image": "sha256:82941edee2f4d17c55563bb926387c3ae39fa1a99777f088bc9d3db885192209",
                "Volumes": null,
                "WorkingDir": "",
                "Entrypoint": [
                    "/docker-entrypoint.sh"
                ],
                "OnBuild": null,
                "Labels": {
                    "maintainer": "NGINX Docker Maintainers "
                },
                "StopSignal": "SIGQUIT"
            },
            "DockerVersion": "20.10.7",
            "Author": "",
            "Config": {
                "Hostname": "",
                "Domainname": "",
                "User": "",
                "AttachStdin": false,
                "AttachStdout": false,
                "AttachStderr": false,
                "ExposedPorts": {
                    "80/tcp": {}
                },
                "Tty": false,
                "OpenStdin": false,
                "StdinOnce": false,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                    "NGINX_VERSION=1.21.5",
                    "NJS_VERSION=0.7.1",
                    "PKG_RELEASE=1~bullseye"
                ],
                "Cmd": [
                    "nginx",
                    "-g",
                    "daemon off;"
                ],
                "Image": "sha256:82941edee2f4d17c55563bb926387c3ae39fa1a99777f088bc9d3db885192209",
                "Volumes": null,
                "WorkingDir": "",
                "Entrypoint": [
                    "/docker-entrypoint.sh"
                ],
                "OnBuild": null,
                "Labels": {
                    "maintainer": "NGINX Docker Maintainers "
                },
                "StopSignal": "SIGQUIT"
            },
            "Architecture": "amd64",
            "Os": "linux",
            "Size": 141479488,
            "VirtualSize": 141479488,
            "GraphDriver": {
                "Data": {
                    "LowerDir": "/var/lib/docker/overlay2/8b0be5030647522bf7b1b39db844d8c650bd8133a00ac05b330879de438c0abb/diff:/var/lib/docker/overlay2/563d515cbd4227114e7adeb014737ce26c3238a9e54135141519c5870470a0e0/diff:/var/lib/docker/overlay2/690c4f6a17cb8cf1aabe042511f57c198281895336138ac474117c60a879cc30/diff:/var/lib/docker/overlay2/3de8af079b18dd20b726da9ce7a91836810e1effd694cc39cccfc63925626d2c/diff:/var/lib/docker/overlay2/b04d78c79c57a4b680bf3f66c56a33b683fd632182f630835ec67d3a400ff965/diff",
                    "MergedDir": "/var/lib/docker/overlay2/fd8c77083cd9b2c0c9bd36ecf8ade515986d4d9cf536abea70ecb0440fed2907/merged",
                    "UpperDir": "/var/lib/docker/overlay2/fd8c77083cd9b2c0c9bd36ecf8ade515986d4d9cf536abea70ecb0440fed2907/diff",
                    "WorkDir": "/var/lib/docker/overlay2/fd8c77083cd9b2c0c9bd36ecf8ade515986d4d9cf536abea70ecb0440fed2907/work"
                },
                "Name": "overlay2"
            },
            "RootFS": {
                "Type": "layers",
                "Layers": [
                    "sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f",
                    "sha256:e379e8aedd4d72bb4c529a4ca07a4e4d230b5a1d3f7a61bc80179e8f02421ad8",
                    "sha256:b8d6e692a25e11b0d32c5c3dd544b71b1085ddc1fddad08e68cbd7fda7f70221",
                    "sha256:f1db227348d0a5e0b99b15a096d930d1a69db7474a1847acbc31f05e4ef8df8c",
                    "sha256:32ce5f6a5106cc637d09a98289782edf47c32cb082dc475dd47cbf19a4f866da",
                    "sha256:d874fd2bc83bb3322b566df739681fbd2248c58d3369cb25908d68e7ed6040a6"
                ]
            },
            "Metadata": {
                "LastTagTime": "0001-01-01T00:00:00Z"
            }
        }
    ]                                               
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118

    4.3. 查看镜像中间层

    [root@centos7-18 image]# docker images -a                                              
    
    • 1

    如果不同镜像之间有复用的中间层,这里就会看见一些没有镜像名和标签的镜像,很多镜像依赖这些中间层,所以中间层镜像是不能随意删除的。
    4.4. 查看镜像中间层

    [root@centos7-18 image]# docker images -a                                              
    
    • 1

    5.删除镜像

    5.1. 删除镜像

    // 删除镜像
    [root@centos7-18 image]# docker rmi hello-world
    // 强制删除镜像  
    [root@centos7-18 image]# docker rmi -f hello-world                                         
    
    • 1
    • 2
    • 3
    • 4

    5.2. 删除虚悬镜像

    [root@centos7-18 image]# docker image prune                                      
    
    • 1

    docker images (不加-a)显示的没有名称和标签的镜像叫虚悬镜像,没有实际用途,可以清除。

    6.导出镜像

    [root@centos7-18 ~]# docker save -o hello-word.tar hello-world
    [root@centos7-18 ~]# ls
    anaconda-ks.cfg  hello-word.tar
    
    • 1
    • 2
    • 3
    • -o 参数后面接着输出文件名。
    • hello-world是需要导出的镜像名。

    7.导入镜像

    [root@centos7-18 ~]# docker load -i hello-word.tar 
    e07ee1baac5f: Loading layer [==================================================>]  14.85kB/14.85kB
    Loaded image: hello-world:latest
    
    • 1
    • 2
    • 3

    8.修改镜像名和TAG

    [root@centos7-18 ~]# docker tag hello-world:latest hello-world:1.0
    [root@centos7-18 ~]# docker tag hello-world:latest hello-demo:1.0
    [root@centos7-18 ~]# docker images
    REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
    nginx         latest    605c77e624dd   22 months ago   141MB
    redis         latest    7614ae9453d1   23 months ago   113MB
    hello-demo    1.0       feb5d9fea6a5   2 years ago     13.3kB
    hello-world   1.0       feb5d9fea6a5   2 years ago     13.3kB
    hello-world   latest    feb5d9fea6a5   2 years ago     13.3kB
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    9.镜像存储位置

    9.1. 存储位置

    • linux保存在“/var/lib/docker/”;
    • windows保存在“C:\ProgramData\DockerDesktop”
    • Mac保存在“com.docker.docker/Data/vms/0/”

    9.2. 修改位置
    修改镜像存储与运行加载目录

    [root@centos7-18 ~]# vim /etc/docker/daemon.json
    
    {
    	"data-root": "/data/var/lib/docker",
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    [root@centos7-18 docker]# systemctl daemon-reload 
    [root@centos7-18 docker]# systemctl restart docker
    
    • 1
    • 2
  • 相关阅读:
    正则表达式常用语法解析
    pytorch中detach()函数以及data属性的区别+梯度求导计算
    量化:概率统计基础
    使用canvas绘制时钟
    java计算机毕业设计吉他库存管理MyBatis+系统+LW文档+源码+调试部署
    【NodeJs篇】http模块
    盒模型(非要让我凑满五个字标题)
    HTML 灵活布局(display: flex)
    深入MNN:开源深度学习框架的介绍、安装与编译指南
    【C++】使用ort推理yolov10
  • 原文地址:https://blog.csdn.net/shijin741231/article/details/134428742