探索Docker 镜像
查看docker镜像列表
docker images
输出:
root@debian:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest d2c94e258dcb 18 months ago 13.3kB
因为刚刚运行了docker run hello-world,所以这里只有一个镜像hello-world
REPOSITORY:仓库,也是镜像名称。
TAG:标签,也是版本号,镜像会有不同的版本号。
IMAGE ID:镜像id,根据这个id我们可以区分不同的镜像,也可以对某个镜像进行操作。
CREATED:创建时间。
SIZE:镜像的大小
拉取Docker镜像
如果你想从Docker仓库(比如 Docker Hub)获取一个新的镜像,可以使用 docker pull 命令。
例如,如果你想拉取一个官方的 nginx 镜像,可以执行以下命令:
docker pull nginx
输出:
root@debian:~# docker pull nginx Using default tag: latest latest: Pulling from library/nginx a480a496ba95: Pull complete f3ace1b8ce45: Pull complete 11d6fdd0e8a7: Pull complete f1091da6fd5c: Pull complete 40eea07b53d8: Pull complete 6476794e50f4: Pull complete 70850b3ec6b2: Pull complete Digest: sha256:28402db69fec7c17e179ea87882667f1e054391138f77ffaf0c3eb388efc3ffb Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest
这个命令会从 Docker Hub 下载最新版本的 nginx 镜像到你的本地。
下载完成后,你可以使用 docker images 再次查看,确认这个镜像已经存在。
root@debian:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 3b25b682ea82 3 weeks ago 192MB hello-world latest d2c94e258dcb 18 months ago 13.3kB
指定版本拉取 Docker 镜像
DOkcer默认拉取最新版本镜像。有时候,你可能不想拉取镜像的最新版本,而是想要某个特定的版本。
这时,你可以通过 docker pull 命令加上镜像名和标签(tag)来拉取指定版本的镜像。格式如下:
docker pull 镜像名:tag
例如,如果你想拉取 nginx 的 1.19 版本,可以执行以下命令:
docker pull nginx:1.19
输出:
root@debian:~# docker pull nginx:1.19 1.19: Pulling from library/nginx 69692152171a: Pull complete 49f7d34d62c1: Pull complete 5f97dc5d71ab: Pull complete cfcd0711b93a: Pull complete be6172d7651b: Pull complete de9813870342: Pull complete Digest: sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2 Status: Downloaded newer image for nginx:1.19 docker.io/library/nginx:1.19
这里的 :1.19 就是镜像的标签,表示你要拉取的特定版本。标签通常用来标识不同的版本或变体,比如某个软件的最新稳定版、测试版或历史版本。通过指定标签,你可以确保拉取的镜像是你所需要的那个版本。如果你省略了标签部分,Docker 会默认拉取这个镜像的最新版本(latest 标签)。
使用 docker images查看
root@debian:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 3b25b682ea82 3 weeks ago 192MB hello-world latest d2c94e258dcb 18 months ago 13.3kB nginx 1.19 f0b8a9a54136 3 years ago 133MB
删除 Docker 镜像
当你不再需要某个镜像时,可以通过 docker rmi 命令将其删除,释放空间。例如,如果你想删除我们刚刚拉取的 nginx 镜像,可以这样做:
docker rmi nginx
输出:
root@debian:~# docker rmi nginx Untagged: nginx:latest Untagged: nginx@sha256:28402db69fec7c17e179ea87882667f1e054391138f77ffaf0c3eb388efc3ffb Deleted: sha256:3b25b682ea82b2db3cc4fd48db818be788ee3f902ac7378090cf2624ec2442df Deleted: sha256:3e8a4396bcdb62aeb916ec1e4cf64500038080839f049c498c256742dd842334 Deleted: sha256:8dd6a711fbdd252eba01f96630aa132c4b4e96961f09010fbbdb11869865f994 Deleted: sha256:9368c52198f80c9fb87fc3eaf7770afb7abb3bfd4120a8defd8a8f1a68ff375d Deleted: sha256:46834c975bf2d807053675d76098806736ee94604c650aac5fe8b5172ab008c8 Deleted: sha256:6e433330e8b1553bee0637fac3b1e66c994bb2c0cab7b2372d2584171d1c93d8 Deleted: sha256:fbc611fa4a4aff4cf0bfd963c49e2c416ff8047c9f84c2dc9328d3b833f1118d Deleted: sha256:98b5f35ea9d3eca6ed1881b5fe5d1e02024e1450822879e4c13bb48c9386d0ad
注意,如果某个镜像正被某个容器使用,你会需要先停止并删除相关的容器才能删除这个镜像。
强制删除 Docker 镜像
有时你可能需要删除某个镜像,但该镜像可能正被其他容器使用,或者因为某些依赖关系导致无法正常删除。此时,你可以使用 docker image rm 命令加上 -f 参数来强制删除镜像。格式如下:
docker image rm -f 镜像名称/镜像ID
例如,如果你想强制删除一个名为 nginx 的镜像,可以执行以下命令。
刚刚删掉了nginx,先拉回来再删除。
docker pull nginx docker image rm -f nginx
输出:
root@debian:/home/x# docker image rm -f nginx Untagged: nginx:latest Untagged: nginx@sha256:28402db69fec7c17e179ea87882667f1e054391138f77ffaf0c3eb388efc3ffb Deleted: sha256:3b25b682ea82b2db3cc4fd48db818be788ee3f902ac7378090cf2624ec2442df Deleted: sha256:3e8a4396bcdb62aeb916ec1e4cf64500038080839f049c498c256742dd842334 Deleted: sha256:8dd6a711fbdd252eba01f96630aa132c4b4e96961f09010fbbdb11869865f994 Deleted: sha256:9368c52198f80c9fb87fc3eaf7770afb7abb3bfd4120a8defd8a8f1a68ff375d Deleted: sha256:46834c975bf2d807053675d76098806736ee94604c650aac5fe8b5172ab008c8 Deleted: sha256:6e433330e8b1553bee0637fac3b1e66c994bb2c0cab7b2372d2584171d1c93d8 Deleted: sha256:fbc611fa4a4aff4cf0bfd963c49e2c416ff8047c9f84c2dc9328d3b833f1118d Deleted: sha256:98b5f35ea9d3eca6ed1881b5fe5d1e02024e1450822879e4c13bb48c9386d0ad [plain]</p> <p>或者,如果你想根据镜像的 ID 来删除(镜像 ID 通常是镜像名称的简写),可以这样做: [plain] docker image rm -f 123456789abc
这里的 -f 参数表示“force”,即强制删除,无论该镜像是否正在被使用或是否有依赖容器。请注意,使用 -f 强制删除镜像时要谨慎,因为这可能会影响正在运行的容器,或破坏相关联的其他镜像或依赖项。
搜索Docker官方镜像
如果你想查找某个镜像,可以使用 docker search 命令。例如,如果你想找 nginx 的官方镜像,可以执行以下命令:
docker search nginx
这个命令会在 Docker Hub 上搜索包含 “nginx” 关键字的所有镜像,并显示一系列相关的镜像列表。你会看到每个镜像的名称、描述、星级评分(由社区用户给出)、是否为官方镜像以及其他信息。
在搜索结果中,你可以看到一行 nginx,带有 “OFFICIAL” 标记,这表明它是由 Docker 官方维护的镜像,通常可以放心使用。
这些是基本的docker的镜像的命令。