当您推送具有相同旧标签的新图像时会发生什么?

Dar*_*der 4 docker

我是 Docker 新手。

我已经对我的 Django 应用程序进行了容器化,并且当我运行容器时,它成功地在公开的端口上运行。

我想知道当我更新应用程序(更改代码)并再次构建图像​​并在已有图像“最新”时再次将其标记为“最新”时会发生什么?它会取代旧图像吗?

tha*_*tah 6

简短的回答:是的,新标签“覆盖”以前的标签

更长的答案:标签的工作方式有点类似于 Git 中标签的工作方式;该标签引用图像的特定版本(内容可寻址摘要),并且可以更新以指向不同的版本。

覆盖标签时,旧图像本身不会被覆盖,但标签现在指向您推送的图像的新版本。如果您通过摘要引用旧映像,仍然可以拉取(并运行)它。

例如,当您拉取busybox:latest图像时,您会看到拉取后打印的摘要:

docker pull busybox:latest

# 7c9d20b9b6cd: Pull complete 
# Digest: sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e
# Status: Downloaded newer image for busybox:latest
# docker.io/library/busybox:latest
Run Code Online (Sandbox Code Playgroud)

因此,除了上述之外,您还可以通过摘要拉取图像:

docker pull busybox@sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e

# sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e: Pulling from library/busybox
# Digest: sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e
# Status: Image is up to date for #busybox@sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e
# docker.io/library/busybox@sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e

Run Code Online (Sandbox Code Playgroud)

虽然较新版本的busybox:latest映像可能会到达 Docker Hub,但如果您知道旧版本的摘要,您仍然可以运行旧版本;

docker run --rm busybox@sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e echo 'hello from busybox'

# hello from busybox
Run Code Online (Sandbox Code Playgroud)