2021Docker容器技术全解-镜像管理的10个命令Docker(2)
容器和镜像的区别:
容器死了就是镜像,镜像活了就是容器,一个镜像可以创建多个容器,
容器:你可以理解为windows
镜像:你可以理解成windows的安装包
官方镜像仓库地址:https://hub.docker.com
常用命令:
#查找镜像 docker search xxx #下载镜像 docker pull imageName:tag #备份镜像 docker save imageName:tag -o bakFileName(tar格式) #恢复镜像 docker load -i bakFileName #查看镜像 docker images
#查看镜像历史:
docker history imageName:tag
#查看镜像详情:
docker inspect imageName:tag
#删除镜像
docker rmi imageName:tag
#上传镜像
docker push imageName:tag
#给镜像打标签
docker tag oldImageName:oldTag NewImageName:NewTag
示例:
BusyBox 是一个集成了三百多个最常用Linux命令和工具的软件。BusyBox 包含了一些简单的工具,例如ls、cat和echo等等,还包含了一些更大、更复杂的工具,例grep、find、mount以及telnet。有些人将 BusyBox 称为 Linux 工具里瑞士军刀。简单的说BusyBox就好像是个大工具箱,它集成压缩了 Linux 的许多工具和命令,也包含了 Android 系统的自带的shell。
#搜索镜像 [root@web01 ~]#docker search busybox #下载镜像 [root@web01 ~]#docker pull busybox #查看镜像 [root@web01 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest ffe9d497c324 2 weeks ago 1.24MB #保存镜像 [root@web01 ~]#docker save busybox:latest -o busybox.tar [root@web01 ~]#ls anaconda-ks.cfg busybox.tar #拷贝镜像到另外一台机器 [root@web01 ~]#scp busybox.tar 10.0.0.8:./ #恢复镜像(解压tar包镜像) [root@web02 ~]#docker load -i busybox.tar
实战:
#搜索安装镜像 [root@web01 ~]#docker search redis [root@web01 ~]#docker pull redis [root@web01 ~]#docker search nginx [root@web01 ~]#docker pull nginx [root@web01 ~]#docker search ubuntu [root@web01 ~]#docker pull ubuntu [root@web01 ~]#docker search centos [root@web01 ~]#docker pull centos #查看镜像 [root@web01 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 7614ae9453d1 7 days ago 113MB nginx latest f6987c8d6ed5 7 days ago 141MB busybox latest ffe9d497c324 2 weeks ago 1.24MB ubuntu latest ba6acccedd29 2 months ago 72.8MB centos latest 5d0da3dc9764 3 months ago 231MB #备份镜像 [root@web01 ~]#docker save redis:latest -o redis.tar [root@web01 ~]#docker save nginx:latest -o nginx.tar [root@web01 ~]#docker save ubuntu:latest -o ubuntu.tar [root@web01 ~]#docker save centos:latest -o centos.tar [root@web01 ~]#ls busybox.tar centos.tar nginx.tar redis.tar ubuntu.tar #把备份的镜像tar包传送到另外一台机器 [root@web01 ~]#scp centos.tar nginx.tar redis.tar ubuntu.tar 10.0.0.8:./ root@10.0.0.8's password: centos.tar #查看镜像历史: [root@web02 ~]#docker history centos:latest IMAGE CREATED CREATED BY SIZE COMMENT 5d0da3dc9764 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 3 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB #查看镜像详情: [root@web01 ~]#docker inspect centos:latest #删除镜像: [root@web02 ~]#docker rmi busybox:latest Untagged: busybox:latest Deleted: sha256:ffe9d497c32414b1c5cdad8178a85602ee72453082da2463f1dede592ac7d5af Deleted: sha256:64cac9eaf0da6a7ae6519b6c7198929f232324e0822b5e359ee0e27104e2d3ed #给镜像打标签 [root@web01 ~]#docker tag centos:latest qq:66907360 [root@web01 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 3 months ago 231MB qq 66907360 5d0da3dc9764 3 months ago 231MB [root@web01 ~]#docker tag 5d0da3dc9764 wechat:tyjs09 [root@web01 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 3 months ago 231MB qq 66907360 5d0da3dc9764 3 months ago 231MB wechat tyjs09 5d0da3dc9764 3 months ago 231MB [root@web01 ~]#