docker镜像基础操作

镜像获取

1
2
3
4
5
镜像获取
docker pull [image_name][:image_version]
eg:
docker pull nginx:1.17

查看镜像信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#查看本地主机上已有的镜像的基本信息
docker images
docker images ls
REPOSITORY TAG IMAGE ID CREATED SIZE
镜像来源 镜像标签 镜像id 镜像创建时间 镜像大小

参数:
-a:列出所有(包括临时文件)镜像文件
-q:仅显示 ID 信息
--digests=true|false:列出镜像的数字摘要值

#查看镜像详细信息,可以看到作者、id等
docker inspect [REPOSITORY]

#查看镜像历史,若构建信息过长,可以使用--no-trunc来输出完整信息
docker history [REPOSITORY]

镜像添加tag

1
2
3
4
5
镜像添加tag
docker tag redis:latest myredis:latest

REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 74d107221092 19 hours ago 104MB

搜索镜像

1
2
3
4
5
6
7
8
9
docker search [option] keyword
option:
-f:过滤输出内容 is-official=true/false stars=800(收藏数超过8000)
--limit:限制3
--no-trunc:不截断输出结果,有些输出结果过长的话会被截断,添加这个可以看到完整的信息

输出参数释义
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
镜像名称 镜像描述 点赞数 是否是官方 自动构建

删除镜像

1
2
3
4
5
6
7
8
9
docker rmi [option] [name]
docker image rm [option] [name]
参数:
-f:强制

docker image prune [option] #清理镜像
-a:删除所有不用的镜像,不加的话是删除临时镜像
-f:强制删除,没有提示

构建镜像

构建镜像的方式有三种,基于容器导入、基于本地模板导入、基于dockerfile创建

基于容器导入(commit)

1
2
3
4
5
6
7
8
docker commit [option] [container id] [image:tag]
eg:docker commit -m "Add a file" -a "Alvin" 820a17fe3935 centos:v1
option:
-a:作者信息
-m:提交信息
-p:提交时,暂停容器运行

执行命令后会在本地生成一个基于原先容器的镜像

将容器导出并保存为镜像(export)

1
2
3
4
5
docker export [container id] > file
eg:
docker export daf9c3656be3 > nginx.tar

ps:使用export命令导出的是个压缩包,且只保存容器当时的状态

从包中读取镜像(import)

1
2
3
4
对应export的读取
docker import file image:tag
eg:
docker import nginx.tar nginx:v1

将本地的镜像保存为镜像包(save)

1
2
3
4
5
6
7
8
docker save [REPOSITORY]/[image_id] > file
eg:
docker save nginx > nginx.tar
docker save 62d49f9bab67 > nginx.tar

将本地的多个镜像保存为镜像包
docker save [image] [image] > file
docker save -o file [image] [image]

从包中加载镜像(load)

1
2
对应save的读取
docker load < file

save与export区别

1
2
3
4
5
6
7
8
9
10
11
12
export导出的是容器打包
save 是将镜像打包成包

export因为是保存的容器当时的状态,因此体积会小于save,相应的会丢失到容器的元数据和以前的历史记录
save会完整的保存

export 不能将多个容器打包
save 可以将多个包打包

export 一般用来制作基础镜像:如我们从一个 ubuntu 镜像启动一个容器,然后安 装一些软件和进行一些设置后,使用 docker export 保存为一个基础镜像。然后,把这个镜像分发给其他人使用, 比如作为基础的开发环境

save 一般用于不能连接外网的情况下,将本地的镜像打包,给别的机器使用