DOCKER学习_009:Docker的镜像管理

1 查看镜像

[root@docker-server3 ~]# docker image ls

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
redis 4.0 e263c3a3ce1f days ago .2MB
httpd 2.4 c2aa7e16edd8 days ago 165MB
centos 5e35e350aded weeks ago 203MB
nginx latest 540a289bab6c months ago 126MB
goodsmileduck/redis-cli latest 4b2315bf1ad2 months ago .9MB

镜像的SIZE的是压缩后的大小,实际运行后的大小,要比这个SIZE大

2 镜像命名规范

无论我们对镜像做何种操作,首先它得有个名字。我们在前面使用docker run来运行容器的时候,就需要传递一个镜像名称,容器基于该镜像来运行。

一个完整的镜像名称由两部分组成:

<image name> = <repository>:[tag]

其中repository包含如下内容:

[Docker Registry地址/][项目目录/]<名称>

所以一个完整的镜像命名如下:

[Docker Registry地址/][项目目录/]<名称>:[标签]

当没指明镜像tag时,默认为latest,但latest没有任何特殊含义,在docker hub上很多repository将latest作为最新稳定版本的别名,但这只是一种约定,不是强制规定,一个repository可以有多个tag,而多个tag也可能对应同一个镜像

3 镜像的基本操作

拉取镜像

docker pull  镜像名,默认是从dokcer的仓库中拉取

[root@docker-server3 ~]# docker pull centos

Using default tag: latest
latest: Pulling from library/centos
729ec3a6ada3: Pull complete
Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

也可以指定位置拉取镜像

[root@docker-server3 ~]# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.1.3

  • 指定仓库位置 registry.cn-hangzhou.aliyuncs.com
  • 路径:google_containers
  • 镜像名:coredns:1.1.3
1.1.: Pulling from google_containers/coredns
88286f41530e: Pull complete
9e8fb813cddc: Pull complete
3ac8c130be3b: Pull complete
Digest: sha256:d929e48a87979279307111fd28a52272d3fac0ed1dc8f2f53a9489be45e5f2eb
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.1.
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.1.

[root@docker-server3 ~]# docker image ls

REPOSITORY                                                    TAG                 IMAGE ID            CREATED             SIZE
redis 4.0 e263c3a3ce1f days ago .2MB
httpd 2.4 c2aa7e16edd8 days ago 165MB
centos 5e35e350aded weeks ago 203MB
nginx latest 540a289bab6c months ago 126MB
centos latest 0f3e07c0138f months ago 220MB
goodsmileduck/redis-cli latest 4b2315bf1ad2 months ago .9MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns 1.1. b3b94275d97c months ago .6MB

4 获取镜像的详细信息

[root@docker-server3 ~]# docker inspect centos:latest

[
{
"Id": "sha256:0f3e07c0138fbe05abcb7a9cc7d63d9bd4c980c3f61fea5efa32e7c4217ef4da",
"RepoTags": [
"centos:latest"
],
"RepoDigests": [
"centos@sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9"
],
"Parent": "",
"Comment": "",
"Created": "2019-10-01T23:19:57.105928163Z",
"Container": "711572e3c0c1ac06d5c13c4e668ec170b8ad8786b5f0949f884a5f7fd350d856",
"ContainerConfig": {
"Hostname": "711572e3c0c1",
"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:c0bda62fdbad65a3c6a1843d293a3a47d8233115cc6d384e3cb07c53580a2b43",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "18.06.1-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:c0bda62fdbad65a3c6a1843d293a3a47d8233115cc6d384e3cb07c53580a2b43",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": ,
"VirtualSize": ,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/166424768dce2d380cf1fe7cd570b2b72feb3b5768859101e5b5d46aa87ccc8d/merged",
"UpperDir": "/var/lib/docker/overlay2/166424768dce2d380cf1fe7cd570b2b72feb3b5768859101e5b5d46aa87ccc8d/diff",
"WorkDir": "/var/lib/docker/overlay2/166424768dce2d380cf1fe7cd570b2b72feb3b5768859101e5b5d46aa87ccc8d/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:9e607bb861a7d58bece26dd2c02874beedd6a097c1b6eca5255d5eb0d2236983"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]

5 镜像的删除操作

[root@docker-server3 ~]# docker rmi 0f3e07c0138f

Untagged: centos:latest
Untagged: centos@sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Deleted: sha256:0f3e07c0138fbe05abcb7a9cc7d63d9bd4c980c3f61fea5efa32e7c4217ef4da
Deleted: sha256:9e607bb861a7d58bece26dd2c02874beedd6a097c1b6eca5255d5eb0d2236983

当镜像有容器存在时

[root@docker-server3 ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
acd538e93dec redis:4.0 "docker-entrypoint.s…" hours ago Up hours /tcp zen_raman
5e82ea0e65f0 centos: "/bin/bash" hours ago Up hours romantic_lewin

[root@docker-server3 ~]# docker rmi 5e35e350aded

Error response from daemon: conflict: unable to delete 5e35e350aded (cannot be forced) - image is being used by running container 5e82ea0e65f0

强制删除

[root@docker-server3 ~]# docker rmi -f 5e35e350aded

Error response from daemon: conflict: unable to delete 5e35e350aded (cannot be forced) - image is being used by running container 5e82ea0e65f0

运行的容器,镜像是无法删除的,停止容器

[root@docker-server3 ~]# docker stop 5e82ea0e65f0

再次删除镜像

[root@docker-server3 ~]# docker rmi  -f 5e35e350aded

Untagged: centos:
Untagged: centos@sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Deleted: sha256:5e35e350aded98340bc8fcb0ba392d809c807bc3eb5c618d4a0674d98d88bccd

[root@docker-server3 ~]# docker image ls

REPOSITORY                                                    TAG                 IMAGE ID            CREATED             SIZE
redis 4.0 e263c3a3ce1f days ago .2MB
httpd 2.4 c2aa7e16edd8 days ago 165MB
nginx latest 540a289bab6c months ago 126MB
hub.darren.com/library/nginx version1 540a289bab6c months ago 126MB
goodsmileduck/redis-cli latest 4b2315bf1ad2 months ago .9MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns 1.1. b3b94275d97c months ago .6MB

centos7的镜像已经被删除  

6 修改镜像名,打标签

[root@docker-server3 ~]# docker tag nginx:latest hub.darren.com/library/nginx:version1

[root@docker-server3 ~]# docker image ls

REPOSITORY                                                    TAG                 IMAGE ID            CREATED             SIZE
redis 4.0 e263c3a3ce1f days ago .2MB
httpd 2.4 c2aa7e16edd8 days ago 165MB
hub.darren.com/library/nginx version1 540a289bab6c 2 months ago 126MB
nginx latest 540a289bab6c 2 months ago 126MB
centos latest 0f3e07c0138f months ago 220MB
goodsmileduck/redis-cli latest 4b2315bf1ad2 months ago .9MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns 1.1. b3b94275d97c months ago .6MB

7 镜像的导入和导出

删除掉所有的镜像

拉去两个镜像

[root@docker-server3 ~]# docker pull busybox

[root@docker-server3 ~]# docker pull alpine

[root@docker-server3 ~]# docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox latest 6d5fcfe5ff17 days ago .22MB
alpine latest cc0abc535e36 days ago .59MB

[root@docker-server3 ~]# docker tag alpine:latest hub.darren.com/library/alpine:3.7

[root@docker-server3 ~]# docker image ls

REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
busybox latest 6d5fcfe5ff17 days ago .22MB
alpine latest cc0abc535e36 days ago .59MB
hub.darren.com/library/alpine 3.7 cc0abc535e36 days ago .59MB

镜像不是一个文件

[root@docker-server3 ~]# cd /var/lib/docker/image/overlay2/

[root@docker-server3 overlay2]# ll

drwx------  root root   Nov   : distribution
drwx------ root root Nov : imagedb
drwx------ root root Nov : layerdb
-rw------- root root Dec : repositories.json

导出

[root@docker-server3 ~]# docker save -o alpine:3.7.tar hub.darren.com/library/alpine:3.7

[root@docker-server3 ~]# ll

-rw-------   root root  Dec  : alpine:3.7.tar

[root@docker-server3 ~]# docker rmi -f cc0abc535e36

Untagged: alpine:latest
Untagged: alpine@sha256:2171658620155679240babee0a7714f6509fae66898db422ad803b951257db78
Untagged: hub.darren.com/library/alpine:3.7
Deleted: sha256:cc0abc535e36a7ede71978ba2bbd8159b8a5420b91f2fbc520cdf5f673640a34
Deleted: sha256:6b27de954cca6332272e7709b7d8ceccee1489d9452af73391df360a26123580
[root@docker-server3 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 6d5fcfe5ff17 days ago .22MB

导入

[root@docker-server3 ~]# docker load --input alpine\:3.7.tar

6b27de954cca: Loading layer [==================================================>]  .857MB/.857MB
Loaded image: hub.darren.com/library/alpine:3.7

[root@docker-server3 ~]# docker image ls

REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
busybox latest 6d5fcfe5ff17 days ago .22MB
hub.darren.com/library/alpine 3.7 cc0abc535e36 days ago .59MB

容器的镜像管理结束


博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢

上一篇:docker 私有镜像管理工具harbor 安装


下一篇:USACO Palindromic Squares 【STL__string_的应用】