一、常用镜像命令总结
images | 显示镜像列表 |
history | 显示镜像构建历史 |
commit | 从容器创建新镜像 |
build | 从 Dockerfile 构建镜像 |
tag | 给镜像打 tag |
pull | 从 registry 下载镜像 |
push | 将 镜像 上传到 registry |
rmi | 删除 Docker host 中的镜像 |
search | 搜索 Docker Hub 中的镜像 |
image prune | 清理临时镜像 |
二、管理镜像
1.从docker hub下载镜像
[root@bogon ~]# docker pull hello-world
2.查看镜像信息
[root@bogon ~]# docker images hello-world
3.给镜像添加tag标签(起别名,创建链接)
[root@localhost ~]# docker tag ubuntu:latest ubuntu:18.10
4.运行镜像
[root@bogon ~]# docker run -it ubuntu bash
5.查看镜像创建历史
[root@bogon ~]# docker history hello-world
6.搜索镜像
[root@localhost ~]# docker search nginx
7.删除镜像
[root@bogon ~]# docker image rm ubuntu:18.10
[root@bogon ~]# docker rmi ubuntu:18.10
[root@bogon ~]# docker image rm 9SDS3HF3S2
- 出现上述情况因为后台存在退出状态的容器,依赖该镜像,可以使用docker ps -a查看,使用docker rm删除依赖的容器,然后才能删除该镜像
- rmi 只能删除 host 上的镜像,不会删除 registry 的镜像。
- 如果一个镜像对应了多个 tag,只有当最后一个 tag 被删除时,镜像才被真正删除。
8.清理临时镜像
[root@localhost ~]# docker image prune
三、构建镜像
1.docker commit(基于已有镜像)
1.1 第一步:运行容器
[root@bogon ~]# docker run -it centos
1.2 第二步:进行自定义操作(安装vim)
[root@5db3417f4eb6 /]# yum -y install vim
1.3 第三步:在宿主机操作,查看容器名称
[root@bogon ~]# docker ps
1.4 第四步:将容器保存为镜像
[root@bogon ~]# docker commit brave_austin centos-vim
或者根据容器id直接创建
[root@docker ~]# docker commit db1f98bb8a55 docker-vim
1.5 第五步:查看新镜像属性
[root@bogon ~]# docker images centos-vim
1.6 第六步:从新镜像启动容器,验证操作
[root@bogon ~]# docker run -it centos-vim
2.docker import(本地模板导入)
2.1 第一步:访问openvz模板网站,下载模板文件
Download/template/precreated - OpenVZ Virtuozzo Containers Wiki
2.2 第二步:导入到docker镜像中
[root@docker ~]# cat ubuntu-18.04-x86_64-minimal.tar.gz | docker import - ubuntu:18.04
2.3 第三步:查看镜像列表
[root@docker ~]# docker images
3.Dockerfile 构建镜像
3.1 第一步:准备Dockerfile文件
[root@bogon ~]# mkdir /docker
[root@bogon ~]# touch /docker/Dockerfile
[root@bogon docker]# cat Dockerfile
FROM centos
RUN yum -y install vim
3.2.第二步:使用docker build 创建镜像
[root@bogon docker]# docker build -t centos-vim-dockerfile .
说明:
-t:创建镜像的标签
. 表示Dockerfile文件在当前路径
-f 参数指定 Dockerfile 的位置
3.3.第三步:查看centos-vim-dockerfile镜像信息
[root@bogon docker]# docker images centos-vim-dockerfile
3.4.第四步:查看镜像构建历史,验证镜像
[root@bogon docker]# docker history centos-vim-dockerfile
四、导出和导入镜像
1.导出镜像
docker save image_name > xx.tar
docker save -o xx.tar image_name
2.导入镜像
docker load < xx.tar
docker load -i xx.tar