4. 导入本地镜像

下面我们来讲解一下如何将本地镜像导入到搭建的本地私有仓库。

我们要确认如下信息:

步骤如下:

  1. 给本地的镜像打标签,指向仓库。
  2. 使用 docker push 命令将打完标签的本地镜像推送到私有仓库。

注意事项

如果私有仓库需要认证,需要在推送前先登录,如:

sudo docker login localhost:5000

1. 打标签

在推送到仓库前,需要将镜像打标签,并指向私有仓库的位置。

命令格式

docker tag 镜像[:标签] 仓库地址/新的镜像名称:新的标签名

示例

将 官方镜像 ubuntu:24.04 推送到 localhost:5000 的仓库,重新命令为 ubuntu2404:latest

weimingze@mzstudio:~$ sudo docker tag ubuntu:24.04 localhost:5000/ubuntu2404:latest

查看打完标签的镜像

weimingze@mzstudio:~$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
ubuntu                      24.04     602eb6fb314b   2 months ago   78.1MB
localhost:5000/ubuntu2404   latest    602eb6fb314b   2 months ago   78.1MB
registry                    latest    3dec7d02aaea   2 months ago   57.7MB

2. docker push 推送到仓库

使用 docker push 命令将 标签为 localhost:5000/ubuntu2404:latest 的镜像推送到 私有仓库。

docker push 命令

命令格式

docker push [选项] 镜像名[:标签]

命令别名: docker image push

常用选项

选项
说明
-q
静默模式,不打印输出。

示例

推送 localhost:5000/ubuntu2404:latest 的到私有仓库。

weimingze@mzstudio:~$ sudo docker push localhost:5000/ubuntu2404:latest
The push refers to repository [localhost:5000/ubuntu2404]
3abdd8a5e7a8: Pushed
latest: digest: sha256:0b9e751164f0f576086bb03062186c5643fe6dc56223b6bfef0be2a9d4828c67 size: 529

验证推送结果

通过浏览器或 curl 命令访问 http://localhost:5000/v2/_catalog 查看仓库信息。

weimingze@mzstudio:~$ curl http://localhost:5000/v2/_catalog
{"repositories":["ubuntu2404"]}

可见本地仓库内已经有 名为 ubuntu2404 的镜像

查看 仓库内 镜像的标签信息的链接地址格式:

http://localhost:5000/v2/<镜像名>/tags/list

如,可以通过如下方式查看 ubuntu2404 的镜像的标签信息

weimingze@mzstudio:~$ curl http://localhost:5000/v2/ubuntu2404/tags/list
{"name":"ubuntu2404","tags":["latest"]}