2019-02-20 Docker镜像管理

1. 镜像是什么?

  • 一个分层存储的文件
  • 一个软件的环境
  • 一个镜像可以创建N个容器
  • 一种标准化的交付
  • 一个不包含Linux内核而又精简的Linux操作系统
    镜像不是一个单一的文件,而是有多层组成。我们可以通过docker history <ID/NAME>查看镜像中各层内容及大小,每层对应着Dockerfile中的一条指令。Docker镜像默认存储在/var/lib/docker/<storage-driver>中。

2. 镜像从哪里来?

Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。地址:https://hub.docker.com/explore
配置镜像加速器:https://www.daocloud.io/mirror
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

[root@localhost ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io    ---配置后下载镜像会加快
docker version >= 1.12
{"registry-mirrors": ["http://f1361db2.m.daocloud.io"]}
Success.
You need to restart docker to take effect: sudo systemctl restart docker 
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker pull nginx    ---下载个nginx镜像测试下

3. 镜像与容器的联系

image.png

如图,容器其实是在镜像的最上面加了一层读写层,在运行容器里文件改动时,会先从镜像里要写的文件复制到容器自己的文件系统中(读写层)。
如果容器删除了,最上面的读写层也就删除了,改动也就丢失了。所以无论多少个容器共享一个镜像,所做的写操作都是从镜像的文件系统中复制过来操作的,并不会修改镜像的源文件,这种方式提高磁盘利用率
若想持久化这些改动,可以通过docker commit将容器保存成一个新镜像。

4. 管理镜像常用命令

image.png
[root@localhost ~]# docker image ls    ---列出镜像信息
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f09fe80eb0e7        13 days ago         109MB
hello-world         latest              fce289e99eb9        7 weeks ago         1.84kB

[root@localhost ~]# docker pull nginx:1.14    ---下载nginx1.14镜像
1.14: Pulling from library/nginx
6ae821421a7d: Already exists 
7edc8c3fac48: Pull complete 
a19c4a4a77fe: Pull complete 
Digest: sha256:a82bee663ef9d49720e7b79952e122871405bd2a2b3112be8c44ff6e96dd3aa3
Status: Downloaded newer image for nginx:1.14

[root@localhost ~]# docker image rm nginx:1.14    ---删除nginx1.14镜像,可以看出把所有分层的目录一并删除
Untagged: nginx:1.14
Untagged: nginx@sha256:a82bee663ef9d49720e7b79952e122871405bd2a2b3112be8c44ff6e96dd3aa3
Deleted: sha256:1293e2b0a1af4854799d5f4a335c59f475cb68ef34e35f8e901d77971eb47315
Deleted: sha256:7016890e9376cbb07ba5ac82aa99da0183ecc886bd3307f7b0bdb08cb6d862f6
Deleted: sha256:7860b3787f080232f56aedd7d431277c2cae522773a7a04c38b158db4b7756d5

[root@localhost ~]# docker image inspect nginx    ---查看nginx镜像的详细信息
[
    {
        "Id": "sha256:f09fe80eb0e75e97b04b9dfb065ac3fda37a8fac0161f42fca1e6fe4d0977c80",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:dd2d0ac3fff2f007d99e033b64854be0941e19a2ad51f174d9240dda20d9f534"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-02-06T08:11:09.870777091Z",
        "Container": "97f09173241402d70db198f1851fe29c5b8838a1557a6d9d13611dac10cfb4cc",
        "ContainerConfig": {
            "Hostname": "97f091732414",
            "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.15.8-1~stretch",
                "NJS_VERSION=1.15.8.0.2.7-1~stretch"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:ef23a318d592d7839af9a0fb77d6282096c1be351ee92598440abd727fa65ab9",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "DockerVersion": "18.06.1-ce",
        "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.15.8-1~stretch",
                "NJS_VERSION=1.15.8.0.2.7-1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:ef23a318d592d7839af9a0fb77d6282096c1be351ee92598440abd727fa65ab9",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 109202600,
        "VirtualSize": 109202600,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/63a2167ed0245134b23888d1ec25c4c7da7dbb5c56b36bd594375149fb450416/diff:/var/lib/docker/overlay2/7d229741b9a8b4afad7e94caa5acdc6e97c8e268d835bdfab199cd9350c12396/diff",
                "MergedDir": "/var/lib/docker/overlay2/e5683ad895e15d8ab0dd2df6b552e8c39590df62ec41e9cd45ff3f7b6e4a96d0/merged",
                "UpperDir": "/var/lib/docker/overlay2/e5683ad895e15d8ab0dd2df6b552e8c39590df62ec41e9cd45ff3f7b6e4a96d0/diff",
                "WorkDir": "/var/lib/docker/overlay2/e5683ad895e15d8ab0dd2df6b552e8c39590df62ec41e9cd45ff3f7b6e4a96d0/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:0a07e81f5da36e4cd6c89d9bc3af643345e56bb2ed74cc8772e42ec0d393aee3",
                "sha256:92c15149e23b2b6b3bb21992622b4af6074d8c7cc6682ec2712c08ff1ca0fc9f",
                "sha256:6b5e2ed60418ce61aa042febfd08b691826552742b0c777de9901de6548bf40a"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

[root@localhost ~]# docker image prune    ---移除未使用的镜像,由于本测试环境没有未使用的镜像,所以释放0B空间
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

[root@localhost ~]# docker image tag  nginx nginx:v1    ---给镜像打标记
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f09fe80eb0e7        13 days ago         109MB
nginx               v1                  f09fe80eb0e7        13 days ago         109MB    ---标记成功
hello-world         latest              fce289e99eb9        7 weeks ago         1.84kB

[root@localhost ~]# docker image save nginx > nginx.tar    ---把nginx镜像保存为名为nginx.tar的归档文件
[root@localhost ~]# du -sh nginx.tar 
108M    nginx.tar
[root@localhost ~]# docker image rm nginx    ---删除原来的nginx镜像
Untagged: nginx:latest
[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v1                  f09fe80eb0e7        13 days ago         109MB
hello-world         latest              fce289e99eb9        7 weeks ago         1.84kB
[root@localhost ~]# docker image load < nginx.tar     ---导入nginx.tar中的镜像
Loaded image: nginx:latest
Loaded image: nginx:v1
[root@localhost ~]# docker images    ---导入成功
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f09fe80eb0e7        13 days ago         109MB
nginx               v1                  f09fe80eb0e7        13 days ago         109MB
hello-world         latest              fce289e99eb9        7 weeks ago         1.84kB

[root@localhost ~]# docker history nginx    ---查看nginx镜像的历史记录
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
5a3221f0137b        31 hours ago        /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           31 hours ago        /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B                  
<missing>           31 hours ago        /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           31 hours ago        /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           31 hours ago        /bin/sh -c set -x     && addgroup --system -…   56.8MB              
<missing>           31 hours ago        /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B                  
<missing>           31 hours ago        /bin/sh -c #(nop)  ENV NJS_VERSION=0.3.5        0B                  
<missing>           31 hours ago        /bin/sh -c #(nop)  ENV NGINX_VERSION=1.17.3     0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           3 days ago          /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           3 days ago          /bin/sh -c #(nop) ADD file:330bfb91168adb4a9…   69.2MB
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容