Docker(5):Docker镜像基本操作(上)
1、获取镜像
可以使用docker pull 命令从网络上下载镜像。该命令的格式为docker pull NAME[:TAG]。对于Docker镜像来说,如果不显示地指定TAG,则默认会选择latest标签,即下载仓库中最新版本的镜像。
从Docker Hub的hello-world仓库下载一个最新的镜像:
1 [root@localhost ~]# docker pull hello-world
2 Using default tag: latest
3 latest: Pulling from library/hello-world
4 1b930d010525: Pull complete
5 Digest: sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
6 Status: Downloaded newer image for hello-world:latest
也可以通过指定标签来下载特定版本的某一个镜像:
# docker pull ubuntu: 14.04
则会从仓库下载14.04版本的镜像。
也可以选择从其他注册服务器的仓库下载。此时,需要在仓库名称前指定完整的仓库注册服务器的地址。例如从DockerPool社区的镜像源dl.dockerpool.com下载最新的Ubuntu镜像:
# docker pull dl.dockerpool.com:5000/ubuntu
下载镜像到本地后,即可随时使用该镜像了。
2、 查看镜像信息
使用docker images命令可以列出本地主机已有的镜像。
1 [root@localhost ~]# docker images
2 REPOSITORY TAG IMAGE ID CREATED SIZE
3 hello-world latest fce289e99eb9 4 months ago 1.84kB
该信息中列出了镜像来自哪个仓库,镜像的标签信息,镜像的ID号(唯一),创建时间,镜像大小。
其中镜像的ID信息非常重要,它唯一标识了镜像
TAG信息用于标记来自同一个仓库的不同镜像。例如Ubuntu仓库中有多个镜像,通过TAG信息来区分发布版本,包括10.04、12.04、12.10、13.04、14.04等标签。
使用docker inspect命令可以获取该镜像的详细信息:
1 [root@localhost ~]# docker inspect fce2
2 [
3 {
4 "Id": "sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e",
5 "RepoTags": [
6 "hello-world:latest"
7 ],
8 "RepoDigests": [
9 "hello-world@sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e"
10 ],
11 "Parent": "",
12 "Comment": "",
13 "Created": "2019-01-01T01:29:27.650294696Z",
14 "Container": "8e2caa5a514bb6d8b4f2a2553e9067498d261a0fd83a96aeaaf303943dff6ff9",
15 "ContainerConfig": {
16 "Hostname": "8e2caa5a514b",
17 "Domainname": "",
18 "User": "",
19 "AttachStdin": false,
20 "AttachStdout": false,
21 "AttachStderr": false,
22 "Tty": false,
23 "OpenStdin": false,
24 "StdinOnce": false,
25 "Env": [
26 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
27 ],
28 "Cmd": [
29 "/bin/sh",
30 "-c",
31 "#(nop) ",
32 "CMD [\"/hello\"]"
33 ],
34 "ArgsEscaped": true,
35 "Image": "sha256:a6d1aaad8ca65655449a26146699fe9d61240071f6992975be7e720f1cd42440",
36 "Volumes": null,
37 "WorkingDir": "",
38 "Entrypoint": null,
39 "OnBuild": null,
40 "Labels": {}
41 },
42 "DockerVersion": "18.06.1-ce",
43 "Author": "",
44 "Config": {
45 "Hostname": "",
46 "Domainname": "",
47 "User": "",
48 "AttachStdin": false,
49 "AttachStdout": false,
50 "AttachStderr": false,
51 "Tty": false,
52 "OpenStdin": false,
53 "StdinOnce": false,
54 "Env": [
55 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
56 ],
57 "Cmd": [
58 "/hello"
59 ],
60 "ArgsEscaped": true,
61 "Image": "sha256:a6d1aaad8ca65655449a26146699fe9d61240071f6992975be7e720f1cd42440",
62 "Volumes": null,
63 "WorkingDir": "",
64 "Entrypoint": null,
65 "OnBuild": null,
66 "Labels": null
67 },
68 "Architecture": "amd64",
69 "Os": "linux",
70 "Size": 1840,
71 "VirtualSize": 1840,
72 "GraphDriver": {
73 "Data": {
74 "MergedDir": "/var/lib/docker/overlay2/c14d313df686c3b8675baca16d8e7c3a26fb5b5e7a29c9acb5c494e5bd7fd3a3/merged",
75 "UpperDir": "/var/lib/docker/overlay2/c14d313df686c3b8675baca16d8e7c3a26fb5b5e7a29c9acb5c494e5bd7fd3a3/diff",
76 "WorkDir": "/var/lib/docker/overlay2/c14d313df686c3b8675baca16d8e7c3a26fb5b5e7a29c9acb5c494e5bd7fd3a3/work"
77 },
78 "Name": "overlay2"
79 },
80 "RootFS": {
81 "Type": "layers",
82 "Layers": [
83 "sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3"
84 ]
85 },
86 "Metadata": {
87 "LastTagTime": "0001-01-01T00:00:00Z"
88 }
89 }
90 ]
docker inspect命令返回的是一个JSON格式的数据,如果我们只要其中一项内容时,可以使用-f参数来指定,例如,获取镜像的Architecture信息:
[root@localhost ~]# docker inspect -f {{".Architecture"}} fce2
amd64
注:在指定镜像ID时,通常可以使用该ID的前若干个字符组成的可区分字串来替代完整的ID。
3. 搜索镜像
使用docker search 命令可以搜索远端仓库中的共享的镜像,默认搜索Docker Hub官方仓库中的镜像。用法为docker search TERM,支持的参数包括:
- --automated=false 仅显示自动创建的镜像
- --no—trunc=false 输出信息不截断显示
- -s,--stars=0 指定仅显示评价为指定星级以上的镜像
例:搜索带MySQL关键字的镜像:
1 [root@localhost ~]# docker search mysql
2 NAME DESCRIPTION STARS OFFICIAL AUTOMATED
3 mysql MySQL is a widely used, open-source relation… 8118 [OK]
4 mariadb MariaDB is a community-developed fork of MyS… 2757 [OK]
5 mysql/mysql-server Optimized MySQL Server Docker images. Create… 607 [OK]
6 zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 192 [OK]
7 hypriot/rpi-mysql RPi-compatible Docker Image with Mysql 113
8 zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 101 [OK]
9 centurylink/mysql Image containing mysql. Optimized to be link… 60 [OK]
10 centos/mysql-57-centos7 MySQL 5.7 SQL database server 52
11 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
12
13 ........
返回信息中包括镜像名字、描述、星级、是否为官方创建、是否自动创建。
默认的输出结果将按照星级评价进行排序。
4. 删除镜像
使用docker rmi 命令可以删除镜像,命令格式为docker rmi IMAGE[IMAGE…],其中IMAGE可以为标签或ID。
1 [root@localhost ~]# docker rmi 18d5
2 Untagged: sshd:dockerfile
3 Deleted: sha256:18d5664af46487403289dff918d23ce621486f2afd145c449d3c2c983a2709f7
4 Deleted: sha256:43deeebb36b02b8d7e4d6a218d1c850106ca9a26cb6f26e43a58d6e8aa136a48
5 Deleted: sha256:caea84a66e29e24b4ac4399cb1c901ecd2e09bf7ec347c7e5e2062300ce66cac
6 Deleted: sha256:f8635da9e84f2a903693565201db2aa75df4a21bc65dfcee59dfad62d2574ab3
7 Deleted: sha256:1f10d01a4c2d41bc6b9e18b4c920d6ceea49b4d497e4131e4044dbbd04be36cd
8 Deleted: sha256:ad6f5929dd219aa6b85c3f0f5e2675a89a84d081f8383c1af50f7e7e9668ce96
9 Deleted: sha256:9d02b0d7acac1e4d013c605607d8ef5f2bf98d65c8b10d7ab3401e86a34d8506
10 Deleted: sha256:130ac06b3615f905f707f4c1ce858862e3be060323df6f06ff8ad596e3b2dd8f
11 Deleted: sha256:b836e16004e45115e9935066527e52b8b71b1618a7dbc4264053ee5a184decc4
12 Deleted: sha256:f7d87c7c9299bcfa360d0cc4eaff3920cc31ced19e2a52d5e14bc8b833c79dc0
13 Deleted: sha256:33a3356b8103012f80d6d8fba36bf31668797d7763bb10141f7e269b84bffab6
14 Deleted: sha256:9e3603ab4baa1579c01a5ea46c6b4e5d4dabbdc5ece83037b19d811ec92f39e3
15 Deleted: sha256:426bf87611a37a08185f2027980ee84066767ddbc085c3cad75d1b6699a424ab
16 Deleted: sha256:1ae1c04577223796b7a9db73009c9cf01ea15df51054ed7309c69f41ca191c72
17 Deleted: sha256:24cb1ed621d355cfadfb01d42e43e6323c478f4ba93aa7f9d2ab12b2d1b738dd
18 Deleted: sha256:2cf94037953608bfc2a18074e90bb6512c05383c682174a9c39259bc1c9df235
19 Deleted: sha256:4d446b97ce30091bec25464ac5d29c1662337cfc272ea5086b5910a3a0325fdf
20 Deleted: sha256:6ebddd22646cf3782850ca860b8b41fba14875a78273c0ab698c39e893234b31
21 Deleted: sha256:1d91ab0bc845595e5011745832028de0de61789f6f682880119046593edbf450
22 Deleted: sha256:3abf6384ac0fc31f6d6f09a4dca83504e7dbf49221ad7a0a51447c8a3be13478
23 Deleted: sha256:6570fed3f8544fa734c8d7f8ab703a0bbddc982cefb25f7dfe7fcd17958725ec
24 Deleted: sha256:df54e206d037e2bb07a70930c9bc113425d0214dd7e909b1f7a436d637456477
25 Deleted: sha256:6848d5e515b16f81d3bd7beaef5ebf9ffbc5946cb12c226ae23b164d32d4a480
26 Deleted: sha256:515c5abd179590c453af0b01fd85f50576c5ac5ea50dcacbee97cd93cc9cc5ba
27 Deleted: sha256:07a0c2553354cc311bd72a0990b13a4b9e21ddb711aedc4d37d46156e18da597
28 Deleted: sha256:0548ebfd926851f287983a2e32d50a93e8b6cbc198c37acbeb5f94f577950566
29 Deleted: sha256:9fe44e8931880f539ea512d5924d4b949623de4525ceacaf81b3048adc0d2878
30 Deleted: sha256:9f54b94e27aec25cf06883cbb2c2aed7576ab58284bfab82df9c2bd3a5de7be5
31 Deleted: sha256:1c6ab3c810d38aa7511315f784947b94cb0f8a0e484bbaa73cf81fd6c65bf769
注:当有该镜像创建的容器存在时,镜像文件默认是无法删除的。如果要想强行删除镜像,可以使用 –f 参数:
# docker rmi –f Ubuntu