【Docker】4. Docker Image

Dokcer Image

Docker Image 简介

Docker Image 本质上是一个只读文件,这个文件包含了文件系统、源码、库文件、依赖、工具等一些运行application(应用)所必须的文件。可以将其理解成一个模板,可以实例化出很多容器

本质是一个联合文件系统(Union FS),每一层文件系统我们叫做一层,联合文件系统可以对每一层文件系统设置三种权限,只读,读写,写出。但是Docker Image中的每一层文件系统都是只读的

Docker Image 作用

部署应用时,常常会出现云端和本地环境不一致的问题,用户为每个应用打包过程非常繁琐,Docker Image就是为了解决这个打包功能

Docker Image把一个镜像制作成一个完整的操作系统所有文件和对应的目录结构,这样的压缩包是跟你本地和测试环境用的操作系统一摸一样的,很好解决了Paas时代云端和本地一致性问题

Docker 最大的贡献就是定义了容器镜像的分层存储格式,镜像技术的基础时联合文件系统(Union FS)其文件系统是分层的,这样既可以充分利用共享层,又可以减少存储空间占用


Docker Image Command

命令别名功能
docker imagesdocker image ls / docker image list列出本地镜像
docker tagdocker image tag给镜像打标签,用于推送镜像仓库
docker pulldocker image pull从镜像仓库拉去镜像
docker pushdocker image push向镜像仓库推送镜像
docker rmidocker image rm / docker image remove删除本地镜像
docker builddocker image build通过dockerfile 制作镜像
docker savedocker image save将指定镜像保存成tar归档文件
docker loaddocker image load导入使用docker save 命令导出的镜像
docker image inspect查看镜像详细信息
docker historydocker image history查看镜像历史信息
docker importdocker image import从归档文件docker export 中创建镜像
docker image prune删除不能是能的镜像

docker rmi [option] image [image…] 删除本地镜像

# 拉去busybox:1.35.0
[root@VM-20-6-centos ~]# docker pull busybox:1.35.0
1.35.0: Pulling from library/busybox
1.35.0: Pulling from library/busybox
c15cbdab5f8e: Pull complete 
Digest: sha256:b4e4a06de46acc0958cd93e2eeb769077d255f06a7c3a91196509c16b7bc989e
Status: Downloaded newer image for busybox:1.35.0
docker.io/library/busybox:1.35.0
[root@VM-20-6-centos ~]# docker images busybox
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
busybox      1.36      8135583d97fe   9 days ago    4.86MB
busybox      1.35.0    dddc7578369a   2 weeks ago   4.85MB
# 删除本地安装的镜像
[root@VM-20-6-centos ~]# docker image remove dddc7578369a
Untagged: busybox:1.35.0
Untagged: busybox@sha256:b4e4a06de46acc0958cd93e2eeb769077d255f06a7c3a91196509c16b7bc989e
Deleted: sha256:dddc7578369a0eb6d94c6eb359fb15cc807e2874fbd7e40614ed0b348c45fd2c
Deleted: sha256:42ef21f45b9a2f44c8235aebe71f7bf716cc464fc45d0a302ef48469040a87da

# 注意:如果一个容器已经被启用了,那么容器运行时无法删除其镜像
# 运行惊醒busybox:1.35.0 创建容器后退出
[root@VM-20-6-centos ~]# docker run -it --name mybusybox6 busybox:1.35.0 sh
/ # ls
bin    dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var
/ # exit

# 可以看到该容器正在停用
[root@VM-20-6-centos ~]# docker ps -a | grep busybox:1.35.0
cce740c20464   busybox:1.35.0   "sh"                     About a minute ago   Exited (0) About a minute ago                                             mybusybox6
acdc9b399f32   busybox:1.35.0   "bash"                   2 minutes ago        Created                                                                   mybusybox

# 删除busybox:1.35.0 镜像
[root@VM-20-6-centos ~]# docker image remove busybox:1.35.0
Untagged: busybox:1.35.0

# 发现无法删除
[root@VM-20-6-centos ~]# docker image remove busybox:1.35
Error response from daemon: conflict: unable to remove repository reference "busybox:1.35" (must force) - container acdc9b399f32 is using its referenced image dddc7578369a

# 使用-f选项强制删除
[root@VM-20-6-centos ~]# docker image remove -f busybox:1.35
Untagged: busybox:1.35
Untagged: busybox@sha256:b4e4a06de46acc0958cd93e2eeb769077d255f06a7c3a91196509c16b7bc989e
Deleted: sha256:dddc7578369a0eb6d94c6eb359fb15cc807e2874fbd7e40614ed0b348c45fd2c

# 强制删除成功
[root@VM-20-6-centos ~]# docker ps -a | grep busybox:1.35
[root@VM-20-6-centos ~]# docker ps -a | grep busybox:1.35.0

docker save [options] image [image…] 将指定镜像保存成tar归档文件

docker save [options] image [image...] 
options: -o 输出到指定文件
[root@VM-20-6-centos maxxin]# ll
total 0
[root@VM-20-6-centos maxxin]# docker images busybox
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
busybox      1.36.0    af2c3e96bcf1   2 weeks ago     4.86MB
busybox      1.34.0    8336f9f1d094   20 months ago   1.24MB

# 保存一个镜像到mybusybox1.tar
[root@VM-20-6-centos maxxin]# docker save -o mybusybox1.tar busybox:1.36.0
[root@VM-20-6-centos maxxin]# ll
total 4980
-rw------- 1 root root 5097472 May 29 19:05 mybusybox1.tar
[root@VM-20-6-centos maxxin]# ll
total 4980
-rw------- 1 root root 5097472 May 29 19:05 mybusybox1.tar

# 保存多个镜像到mybusybox2.tar 
[root@VM-20-6-centos maxxin]# docker save -o mybusybox2.tar busybox:1.36.0 busybox:1.34.0
[root@VM-20-6-centos maxxin]# ll
total 11396
-rw------- 1 root root 5097472 May 29 19:05 mybusybox1.tar
-rw------- 1 root root 6566912 May 29 19:06 mybusybox2.tar

docker load (写入,载入) 导入使用docker save 命令生成的.tar包

docker load [options]
--input, -i 指定导入文件,代替STDIN
--quiet, -q 精简输入信息


[root@VM-20-6-centos maxxin]# docker images;
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

# 加载.tar包内部镜像, 加-q 选项输出信息会变少
[root@VM-20-6-centos maxxin]# docker load -i mybusybox2.tar
1f1d08b81bbe: Loading layer [==================================================>]  5.088MB/5.088MB
Loaded image: busybox:1.36.0
f6cb480bb44e: Loading layer [==================================================>]  1.463MB/1.463MB
Loaded image: busybox:1.34.0

[root@VM-20-6-centos maxxin]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
busybox      1.36.0    af2c3e96bcf1   2 weeks ago     4.86MB
busybox      1.34.0    8336f9f1d094   20 months ago   1.24MB

docker history 显示镜像历史

docker history [options] image
-H --human : 大小和日期采用人易读的格式展现
--no-trunc : 显示全部信息
-q --quit: 只显示id

[root@VM-20-6-centos maxxin]# docker history -H busybox:1.36.0
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
af2c3e96bcf1   2 weeks ago   /bin/sh -c #(nop)  CMD ["sh"]                   0B        
<missing>      2 weeks ago   /bin/sh -c #(nop) ADD file:c22c6617fa9a85e0d…   4.86MB  

docker image prune 删除不使用镜像

docker image prune [options]
-a, --all # 删除全部不使用的镜像(容器不使用即被定义) --该选项非常危险
--filter  # filter(过滤器) 
-f --force # 不提示是否删除

Docker Image Operator

# docker 信息存储地址 /data/var/lib/docker
[root@VM-20-6-centos docker]# ll
total 44
drwx--x--x 4 root root 4096 May 29 19:01 buildkit
drwx--x--- 2 root root 4096 May 29 19:01 containers
-rw------- 1 root root   36 May 29 19:01 engine-id
drwx------ 3 root root 4096 May 29 19:01 image
drwxr-x--- 3 root root 4096 May 29 19:01 network
drwx--x--- 5 root root 4096 May 29 19:12 overlay2
drwx------ 4 root root 4096 May 29 19:01 plugins
drwx------ 2 root root 4096 May 29 19:01 runtimes
drwx------ 2 root root 4096 May 29 19:01 swarm
drwx------ 2 root root 4096 May 29 19:14 tmp
drwx-----x 2 root root 4096 May 29 19:01 volumes

[root@VM-20-6-centos docker]# cd image
[root@VM-20-6-centos image]# ll
total 4
drwx------ 5 root root 4096 May 29 19:12 overlay2   # overlay2 是docker 的存储驱动

scp mybusybox.tar root@IP:/data/maxxin


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白在进击

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值