docker 镜像

时间:2022-07-22
本文章向大家介绍docker 镜像,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

docker镜像

docker镜像类似与虚拟机镜像,可以将它理解为一个面向docker引擎的只读模板,包含了文件系统。

一个镜像可以只包含一个完整的操作系统环境,例如centos,ubuntu等,镜像也可以是安装了nginx,apache,mysql等服务的镜像,镜像是创建docker容器的基础,通过版本管理和增量的文件系统。

获取镜像

可以使用docker pull IMAGE_NAME:[TAG]来从网上拉取镜像,默认如果不写tag,那么就会拉取最新的(latest)的镜像到本地。

1 2 3 4 5 6 7 8 9 10

[root@localhost ~]# docker pull ubuntu #相当与docker pull registry.hup.docker.com/ubuntu:latest Using default tag: latest latest: Pulling from library/ubuntu 22dc81ace0ea: Pull complete 1a8b3c87dba3: Pull complete 91390a1c435a: Pull complete 07844b14977e: Pull complete b78396653dae: Pull complete Digest: sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6 Status: Downloaded newer image for ubuntu:latest

查看镜像信息

使用docker imagesdocker image ls来查看

1 2 3

[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest f975c5035748 Less than a second ago 112MB

对上面的信息一次介绍

  • REPOSITORY:来自与哪个仓库
  • TAG:镜像的标签信息,用于标记来自同一个仓库的不同镜像,例如ubuntu:14.04和ubuntu:14.10等
  • IMAGE ID:镜像的ID号(唯一)
  • CREATED:创建时间
  • SIZE:镜像大小

给镜像打标签

1 2 3 4 5 6

[root@localhost ~]# docker tag ubuntu:latest www.devilf.cc/ubuntu [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest f975c5035748 Less than a second ago 112MB www.devilf.cc/ubuntu latest f975c5035748 Less than a second ago 112MB

使用docker inspect来查看镜像的详细信息

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

[root@localhost ~]# docker inspect ubuntu [ { "Id": "sha256:f975c50357489439eb9145dbfa16bb7cd06c02c31aa4df45c77de4d2baa4e232", "RepoTags": [ "ubuntu:latest", "www.devilf.cc/ubuntu:latest" ], "RepoDigests": [ "ubuntu@sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6" ], "Parent": "", "Comment": "", "Created": "2018-03-06T22:17:26.531075062Z", "Container": "6e8eb576ec0f7564a85c0fbd39824e0e91c031aa0019c56c5f992449e88d1142", "ContainerConfig": { "Hostname": "6e8eb576ec0f", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD ["/bin/bash"]" ], "ArgsEscaped": true, "Image": "sha256:66126c48f804cc6ea441ce48bd592d4c6535b95e752af4d2596f5dbe66cdd209", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "DockerVersion": "17.06.2-ce", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "ArgsEscaped": true, "Image": "sha256:66126c48f804cc6ea441ce48bd592d4c6535b95e752af4d2596f5dbe66cdd209", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 112434063, "VirtualSize": 112434063, "GraphDriver": { "Data": { "DeviceId": "6", "DeviceName": "docker-253:0-34439856-bfbcc4e8776dd50fe15a50d79004bbdce0f78cb2607b74e95291c58ef4be795a", "DeviceSize": "10737418240" }, "Name": "devicemapper" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:a94e0d5a7c404d0e6fa15d8cd4010e69663bd8813b5117fbad71365a73656df9", "sha256:88888b9b1b5b7bce5db41267e669e6da63ee95736cb904485f96f29be648bfda", "sha256:52f389ea437ebf419d1c9754d0184b57edb45c951666ee86951d9f6afd26035e", "sha256:52a7ea2bb533dc2a91614795760a67fb807561e8a588204c4858a300074c082b", "sha256:db584c622b50c3b8f9b8b94c270cc5fe235e5f23ec4aacea8ce67a8c16e0fbad" ] }, "Metadata": { "LastTagTime": "2018-03-04T22:36:07.543131988+08:00" } } ]

docker inspect返回的是一个JSON格式的消息,如果我们只要其中一项内容,我们可以使用-f参数来指定,例如:

1 2

[root@localhost ~]# docker inspect -f {{.Metadata}} f975c5035748 {2018-03-04 22:36:07.543131988 +0800 CST}

注意{{}}之间配置项前面是由”点“的。

搜寻镜像

使用docker search IMAGE_NAME来搜索

1 2 3 4 5 6 7 8 9 10 11 12 13 14

[root@localhost ~]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 4093 [OK] ansible/centos7-ansible Ansible on Centos7 105 [OK] jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x86_… 92 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC session… 48 [OK] imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 39 [OK] tutum/centos Simple CentOS docker image with SSH access 36 gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glust… 24 [OK] centos/python-35-centos7 Platform for building and running Python 3.5… 19 openshift/base-centos7 A Centos7 derived base image for Source-To-I… 17 kinogmt/centos-ssh CentOS with SSH 17 [OK] openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use w… 10

删除镜像

使用docker rmi IMAGE_NAME(TAG|IMAGE ID)来删除镜像

1 2 3 4 5 6 7

[root@localhost ~]# docker rmi www.devilf.cc/ubuntu Untagged: www.devilf.cc/ubuntu:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 73acd1f0cfad Less than a second ago 109MB ubuntu latest f975c5035748 Less than a second ago 112MB

如果是改镜像有创建过容器时,此时的镜像默认是无法被删除的,需要使用-f参数来删除,但是不推荐用此参数,建议先删除容器,再删除镜像

创建镜像

  • 基于已有镜像的容器创建
  • 基于本地模板导入
  • 基于dockerfile创建

第一种:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

启动容器,并修改文件 [root@localhost ~]# docker run -it ubuntu /bin/bash root@0d221d71780d:/# touch 1 root@0d221d71780d:/# echo test > 1 root@0d221d71780d:/# exit 提交镜像 [root@localhost ~]# docker commit -m "add 1" 0d221d71780d ubuntu_test sha256:3b614bc7442b1c46f2a8290d8957b9109af385f862efed22203c8a0a35026b71 查看 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 73acd1f0cfad Less than a second ago 109MB test latest f975c5035748 Less than a second ago 112MB ubuntu latest f975c5035748 Less than a second ago 112MB ubuntu_test latest 3b614bc7442b About a minute ago 112MB

第二种:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

save: [root@localhost ~]# docker save -o test.tar test [root@localhost ~]# ll 总用量 135936 -rw-------. 1 root root 116514816 3月 4 23:45 test.tar 可以使用一下命令导入: [root@localhost ~]# cat test.tar | docker import - test:v1 sha256:c9b3439309d3bfeb1122a00c2b38f57434770cbec90c84213cd7a90e2b324496 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 73acd1f0cfad Less than a second ago 109MB test latest f975c5035748 Less than a second ago 112MB ubuntu latest f975c5035748 Less than a second ago 112MB test v1 c9b3439309d3 5 seconds ago 116MB ubuntu_test latest 3b614bc7442b 16 minutes ago 112MB 也可以使用load: [root@localhost ~]# docker load --input test.tar Loaded image: test:latest

上传镜像

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

登陆 #docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: devilf Password: Login Succeeded 使用Docker Hub账户的用户名打一个标签 #docker tag ubuntu:17.04 devilf/ubuntu:17.04 查看# docker images 推送 #docker push devilf/ubuntu:17.04 The push refers to repository [docker.io/devilf/ubuntu] 3ff70ce53dac: Pushed b8e5935ae7cc: Pushed ba76b502dc9b: Pushed 803030df23c1: Pushed db8686e0ca43: Pushed 17.04: digest: sha256:213e05583a7cb8756a3f998e6dd65204ddb6b4c128e2175dcdf174cdf1877459 size: 1357 搜索 #docker search devilf NAME DESCRIPTION STARS OFFICIAL AUTOMATED devilflake/get-started 0