Docker 学习笔记 -Image 的相关命令

网友投稿 264 2022-10-28

Docker 学习笔记 -Image 的相关命令

继续学习入门知识。前面已经安装了dokcer,现在来看看 docker image的相关命令

[ec2-user@ip-172-16-1-4 ~]$ sudo docker image Usage: docker image COMMAND Manage images Commands: build Build an image from a Dockerfile history Show the history of an image import Import the contents from a tarball to create a filesystem image inspect Display detailed information on one or more images load Load an image from a tar archive or STDIN ls List images prune Remove unused images pull Pull an image or a repository from a registry push Push an image or a repository to a registry rm Remove one or more images save Save one or more images to a tar archive (streamed to STDOUT by default) tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

下面按顺序简单的过一遍

build是创建镜像,这个后面会有具体介绍

history 可以看见镜像的历史。docker的镜像是一个多层的结构,比如说,这个里面的结构其实都是我们在创建docker镜像到时候输入的命令

127MB [ec2-user@ip-172-16-1-4 ~]$ sudo docker image history nginx IMAGE CREATED CREATED BY SIZE COMMENT 8cf1bfb43ff5 2 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "da emon… 0B 2 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B 2 days ago /bin/sh -c #(nop) EXPOSE 80 0B 2 days ago /bin/sh -c #(nop) ENTRYPOINT ["/docker- entr… 0B 2 days ago /bin/sh -c #(nop) COPY file:0fd5fca330dc d6a7… 1.04kB 2 days ago /bin/sh -c #(nop) COPY file:1d0a4127e78a 26c1… 1.96kB 2 days ago /bin/sh -c #(nop) COPY file:e7e183879c35 719c… 1.2kB 2 days ago /bin/sh -c set -x && addgroup --syst em -… 63.3MB 2 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bus ter 0B 2 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.4.2 0B 2 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.1 9.1 0B 2 days ago /bin/sh -c #(nop) LABEL maintainer=NGIN X Do… 0B 2 days ago /bin/sh -c #(nop) CMD ["bash"] 0B 2 days ago /bin/sh -c #(nop) ADD file:6ccb3bbcc69b0 d44c… 69.2MB

import 是从一个tar文件里面导入容器的文件系统来生成一个镜像,对应的我们可以从docker container的命令里面export一个容器的文件系统到一个tar文件里面。

inspect 是一个通用的命令, 主要是显示一些常见docker对象的信息

[root@ip-172-16-1-4 ec2-user]# docker inspect --help Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...] Return low-level information on Docker objects Options: -f, --format string Format the output using the given Go template -s, --size Display total file sizes if the type is container --type string Return JSON for specified type

比如 我查看一下nginx这个镜像的信息

root@ip-172-16-1-4 ec2-user]# docker inspect nginx [ { "Id": "sha256:8cf1bfb43ff5d9b05af9b6b63983440f137c6a08320fa7592197c1474ef30241", "RepoTags": [ "nginx:latest" ], "RepoDigests": [ "nginx@sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c" ], "Parent": "", "Comment": "", "Created": "2020-07-22T03:23:26.691138735Z", "Container": "072f077fe42b39ffb92ca368fa5d79be975faa97b33bffb5b5383eb6abfb4838", "ContainerConfig": { "Hostname": "072f077fe42b", "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.19.1", "NJS_VERSION=0.4.2", "PKG_RELEASE=1~buster" ], "Cmd": [ "/bin/sh",

ls 就是显示当前有哪些镜像

[root@ip-172-16-1-4 ec2-user]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 8cf1bfb43ff5 2 days ago 132MB nginx 1.17 9beeba249f3e 2 months ago 127MB

save 和load 相对应,一个是把镜像和相关的资源打包到一个tar文件里面,一个是把这些资源再加载回来。他们的使用场景可以用于备份,或者把镜像拷贝到其他地方使用。

[root@ip-172-16-1-4 ec2-user]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 8cf1bfb43ff5 2 days ago 132MB nginx 1.17 9beeba249f3e 2 months ago 127MB [root@ip-172-16-1-4 ec2-user]# docker image save 8cf1bfb43ff5 > nginx.tar [root@ip-172-16-1-4 ec2-user]# du nginx.tar -h 131M nginx.tar

生成之后,我来尝试删除一下现有的镜像,他会提示报错,因为我有个容器关联着这个镜像

[root@ip-172-16-1-4 ec2-user]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 8cf1bfb43ff5 2 days ago 132MB nginx 1.17 9beeba249f3e 2 months ago 127MB [root@ip-172-16-1-4 ec2-user]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 34eef288fd4b nginx "/docker-entrypoint.…" 23 hours ago Up 23 hours 0.0.0.0:8080->80/tcp focused_grothendieck [root@ip-172-16-1-4 ec2-user]# docker image rm 8cf1bfb43ff5 Error response from daemon: conflict: unable to delete 8cf1bfb43ff5 (cannot be forced) - image is being used by running container 34eef288fd4b [root@ip-172-16-1-4 ec2-user]#

强制关掉正在运行的容器

[root@ip-172-16-1-4 ec2-user]# docker stop $(docker ps -a -q) 34eef288fd4b

现在就可以删除镜像了

[root@ip-172-16-1-4 ec2-user]# docker image rm 8cf1bfb43ff5 Error response from daemon: conflict: unable to delete 8cf1bfb43ff5 (must be forced) - image is being used by stopped container 34eef288fd4b [root@ip-172-16-1-4 ec2-user]# docker image rm 8cf1bfb43ff5 -f Untagged: nginx:latest Untagged: nginx@sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c Deleted: sha256:8cf1bfb43ff5d9b05af9b6b63983440f137c6a08320fa7592197c1474ef30241 [root@ip-172-16-1-4 ec2-user]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.17 9beeba249f3e 2 months ago 127MB

pull 和 push 相对,一个是下载镜像,一个是上传镜像。当我们直接调用 docker run的时候,他会尝试从本地找对应的镜像来创建容器,如果本地没有,那么会自动下载。当然我们也可以 用 pull先下载之后,在创建容器。

[root@ip-172-16-1-4 ec2-user]# docker image pull nginx Using default tag: latest latest: Pulling from library/nginx 6ec8c9369e08: Already exists d3cb09a117e5: Already exists 7ef2f1459687: Already exists e4d1bf8c9482: Already exists 795301d236d7: Already exists Digest: sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest

prune

这个单词的本意是修剪枝叶,他的作用也差不多。 有的时候,镜像可能会出现问题,导致名字变成none,这样的镜像是有问题的,prune可以直接删掉。有的时候,镜像没有连接到任何容器,为了节约空间,我们也可以把这些镜像自动批量删掉。

[root@ip-172-16-1-4 ec2-user]# docker image prune -a WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N] y Deleted Images: untagged: nginx:1.17 untagged: nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699 deleted: sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9 deleted: sha256:8fb6373b4cca3383756d7fd7843dd92f95827e5f2913609e09a9621dcddb3752 deleted: sha256:8b09841626797a03a9fe5e73aa38aeacf9ff0ce85a3004236ff35234eec3b35c deleted: sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07 Total reclaimed space: 126.8MB

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何执行音频认证测试
下一篇:Java架构设计之六步拆解 DDD
相关文章

 发表评论

暂时没有评论,来抢沙发吧~