Docker 镜像管理

什么是镜像

简单说,Docker镜像是一个不包含Linux内核而又精简的Linux操作系统。

Docker运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker会尝试先从默认镜像仓库下载,用户也可以通过配置,使用自定义的镜像仓库。

Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。
https://hub.docker.com/explore
默认是国外的源,下载会慢,建议配置国内镜像仓库:
# vi /etc/docker/daemon.json
{
"registry-mirrors": [ "https://registry.docker-cn.com"]
}

镜像与容器联系

镜像不是一个单一的文件,而是有多层构成。我们可以通过docker history <ID/NAME> 查看镜像中各层内容及大小,每层对应着Dockerfile中的一条指令。Docker镜像默认存储在/var/lib/docker/<storage-driver>中。
容器其实是在镜像的最上面加了一层读写层,在运行容器里做的任何文件改动,都会写到这个读写层。如果容器删除了,最上面的读写层也就删除了,改动也就丢失了。

Docker使用存储驱动管理镜像每层内容及可读写层的容器层。

Docker 镜像管理

Docker 镜像管理

查看镜像
[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

拉取nginx镜像
[[email protected] ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f2aa67a397c4: Pull complete 
3c091c23e29d: Pull complete 
4a99993b8636: Pull complete 
Digest: sha256:0fb320e2a1b1620b4905facb3447e3d84ad36da0b2c8aa8fe3a5a81d1187b884
Status: Downloaded newer image for nginx:latest
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ae513a47849c        3 weeks ago         109MB
[[email protected] ~]# docker pull nginx:1.10
1.10: Pulling from library/nginx
6d827a3ef358: Pull complete 
1e3e18a64ea9: Pull complete 
556c62bb43ac: Pull complete 
Digest: sha256:6202beb06ea61f44179e02ca965e8e13b961d12640101fca213efbfd145d7575
Status: Downloaded newer image for nginx:1.10
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ae513a47849c        3 weeks ago         109MB
nginx               1.10                0346349a1a64        14 months ago       182MB

查看镜像历史
[[email protected] ~]# docker history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
ae513a47849c        3 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL [SIGTERM]         0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  EXPOSE 80/tcp                0B                  
<missing>           3 weeks ago         /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           3 weeks ago         /bin/sh -c set -x  && apt-get update  && apt…   53.7MB              
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=1.13.12.0…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.13.12…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:ec5be7eec56a74975…   55.3MB  

[[email protected] overlay2]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ae513a47849c        3 weeks ago         109MB
nginx               1.11                5766334bdaa0        13 months ago       183MB
nginx               1.10                0346349a1a64        14 months ago       182MB

查看镜像
[[email protected] overlay2]# docker image  ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ae513a47849c        3 weeks ago         109MB
nginx               1.11                5766334bdaa0        13 months ago       183MB
nginx               1.10                0346349a1a64        14 months ago       182MB

删除一个镜像
[[email protected] overlay2]# docker image rm nginx:1.11
Untagged: nginx:1.11
Untagged: [email protected]:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582
Deleted: sha256:5766334bdaa0bc37f1f0c02cb94c351f9b076bcffa042d6ce811b0fd9bc31f3b
Deleted: sha256:1fcf2d3addf02c3b6add24c7b0993038f7e3eee616b10e671e25440e03bc7697
Deleted: sha256:51c56cdbb9306c4d6f2da2b780924f3b926bd13d15a4f6693a5175690e288436
Deleted: sha256:ec9a826666cfa5df0471f716145da63294019c09a5f2e31613122b57df8f7ce0
[[email protected] overlay2]# docker image  ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ae513a47849c        3 weeks ago         109MB
nginx               1.10                0346349a1a64        14 months ago       182MB    

标记镜像
[[email protected] overlay2]# docker tag nginx:1.10 nginx:v01
[[email protected] overlay2]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ae513a47849c        3 weeks ago         109MB
nginx               1.10                0346349a1a64        14 months ago       182MB
nginx               v01                 0346349a1a64        14 months ago       182MB

保存镜像到本地生成tar文件
[[email protected] overlay2]# docker image save nginx:1.10 > /tmp/nginx1.10.tar
[[email protected] overlay2]# cd /tmp/
[[email protected] tmp]# ll
-rw-r--r-- 1 root root 189574656 5月  26 15:39 nginx1.10.tar
加载本地镜像

[[email protected] ~]# docker image load < /tmp/nginx1.10.tar

[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[[email protected] ~]# docker run -itd nginx
ced18e1589acea7078783b45b07967b823223d75aa8186dd305678b302ffc594
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
ced18e1589ac        nginx               "nginx -g 'daemon of…"   7 seconds ago       Up 5 seconds        80/tcp              gifted_kowalevski

导出容器文件tar包

[[email protected] ~]# docker export ced18e1589ac > /tmp/testnginx.tar

导入容器文件tar包

[[email protected] ~]# docker image import /tmp/testnginx.tar nginx:1.10

Docker 容器镜像删除
1.停止所有的container,这样才能够删除其中的images:
docker stop $(docker ps -a -q)
如果想要删除所有container的话再加一个指令:
docker rm $(docker ps -a -q)
2.查看当前有些什么images
docker images
3.删除images,通过image的id来指定删除谁
docker rmi <image id>
想要删除untagged images,也就是那些id为<None>的image的话可以用
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")
要删除全部image的话
docker rmi $(docker images -q)