docker image 镜像管理

  • Docker 管理命令
    • 容器管理
    • 镜像管理
    • 网络管理
    • 系统管理
    • 数据卷管理
    • 快照管理
    • 插件管理
    • 其他
    • swarmkit 管理
      • 模式管理
      • 节点管理
      • 秘钥管理
      • 服务管理
      • 服务栈管理

镜像管理 image

build

根据 dockerfile 构建镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@ubuntu:~# docker image build -t centos2 .
Sending build context to Docker daemon 19.46kB
Step 1/2 : FROM centos
---> a8493f5f50ff
Step 2/2 : LABEL author "lvrui"
---> Running in 9a996772de96
---> 61b7efba9133
Removing intermediate container 9a996772de96
Successfully built 61b7efba9133
root@ubuntu:~# cat dockerfile
FROM centos
LABEL author="lvrui"
root@ubuntu:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos2 latest 61b7efba9133 About a minute ago 192MB
nginx2 0.1 7cb5220c81d5 2 hours ago 204MB
centos latest a8493f5f50ff 37 hours ago 192MB
nginx latest 5766334bdaa0 41 hours ago 183MB
hello-world latest 48b5124b2768 2 months ago 1.84kB

-t 的作用是打标签, 可以直接打成 Registry 的形式, 方便直接 push 到 Registry

如果没有打赏 Registry 地址也没有关系,后期可以通过 tag 命令修改

history

显示镜像的历史构建信息

1
2
3
4
5
6
7
8
9
10
11
12
13
root@ubuntu:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos2 latest 61b7efba9133 2 minutes ago 192MB
nginx2 0.1 7cb5220c81d5 2 hours ago 204MB
centos latest a8493f5f50ff 37 hours ago 192MB
nginx latest 5766334bdaa0 41 hours ago 183MB
hello-world latest 48b5124b2768 2 months ago 1.84kB
root@ubuntu:~# docker image history centos2
IMAGE CREATED CREATED BY SIZE COMMENT
61b7efba9133 2 minutes ago /bin/sh -c #(nop) LABEL author=lvrui 0B
a8493f5f50ff 37 hours ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 37 hours ago /bin/sh -c #(nop) LABEL name=CentOS Base ... 0B
<missing> 37 hours ago /bin/sh -c #(nop) ADD file:807143da05d7013... 192MB

import/save/load

  • import 导入镜像, 导入 export 出来的镜像
  • save 导出镜像
  • load 导入镜像, 导入 save 出来的镜像

inspect

查看镜像详细信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
root@ubuntu:~# docker image inspect centos2
[
{
"Id": "sha256:61b7efba9133e4ceaf4046e76a2f455a1204a8a794f16920c7543010e2759323",
"RepoTags": [
"centos2:latest"
],
"RepoDigests": [],
"Parent": "sha256:a8493f5f50ffda70c2eeb2d09090debf7d39c8ffcd63b43ff81b111ece6f28bf",
"Comment": "",
"Created": "2017-04-08T09:12:50.750877048Z",
"Container": "9a996772de96183b48460aa1f02b907d1e8d97f8d322cc41aff909d2a8a74b61",
"ContainerConfig": {
"Hostname": "f85c553c1496",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"LABEL author=lvrui"
],
"Image": "sha256:a8493f5f50ffda70c2eeb2d09090debf7d39c8ffcd63b43ff81b111ece6f28bf",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": [],
"Labels": {
"author": "lvrui",
"build-date": "20170406",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"DockerVersion": "17.04.0-ce",
"Author": "",
"Config": {
"Hostname": "f85c553c1496",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "sha256:a8493f5f50ffda70c2eeb2d09090debf7d39c8ffcd63b43ff81b111ece6f28bf",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": [],
"Labels": {
"author": "lvrui",
"build-date": "20170406",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 192481139,
"VirtualSize": 192481139,
"GraphDriver": {
"Data": null,
"Name": "aufs"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:36018b5e978717a047892794aebab513ba6856dbe1bdfeb478ca1219df2c7e9c"
]
}
}
]

ls

作用等同于 docker images 查看镜像列表

1
2
3
4
5
6
7
root@ubuntu:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos2 latest 61b7efba9133 3 hours ago 192MB
nginx2 0.1 7cb5220c81d5 5 hours ago 204MB
centos latest a8493f5f50ff 40 hours ago 192MB
nginx latest 5766334bdaa0 44 hours ago 183MB
hello-world latest 48b5124b2768 2 months ago 1.84kB

prune

删除没有被引用的镜像

1
root@ubuntu:~# docker image prune

tag/pull/push

  • tag: 打标签
  • pull: 从远程仓库 Registry 拉取镜像
  • push: 从本地上传镜像

从registry中拉取镜像:

1
$ sudo docker pull registry.cn-beijing.aliyuncs.com/lvreg/webconsole:[镜像版本号]

将镜像推送到registry:

1
2
3
$ sudo docker login --username=lvrui03@126.com registry.cn-beijing.aliyuncs.com
$ sudo docker tag [ImageId] registry.cn-beijing.aliyuncs.com/lvreg/webconsole:[镜像版本号]
$ sudo docker push registry.cn-beijing.aliyuncs.com/lvreg/webconsole:[镜像版本号]

注意: 如果 Registry 有密码, 还需要先 login 到镜像仓库才可以上传镜像

rm

删除指定的镜像

1
root@ubuntu:~# docker image rm centos2:latest