5. 私有仓库下载镜像

从私有仓库内下载镜像分为两种情况:

  1. 从本机 localhost 下载镜像
  2. 从其他的私有仓库下载镜像(需要用到域名或 IP 地址)

1. 从本机 localhost 下载镜像

使用 docker pull 命令就可以下载本地镜像,

命令格式:

docker pull localhost[:端口号]/镜像名[:标签]

示例

下载 localhost:5000/ubuntu24.04:latest 镜像。

weimingze@mzstudio:~$ sudo docker pull localhost:5000/ubuntu2404
Using default tag: latest
latest: Pulling from ubuntu2404
Digest: sha256:0b9e751164f0f576086bb03062186c5643fe6dc56223b6bfef0be2a9d4828c67
Status: Downloaded newer image for localhost:5000/ubuntu2404:latest
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. 从 其他的私有仓库下载镜像

如果下载镜像的私有仓库,不是本机 localhost 则需要修改 Docker 配置文件 /etc/docker/daemon.json,将允许不安全的仓库信息加入配置文件中。

方法如下:

{
  "insecure-registries" : ["私有仓库主机域名或IP地址:5000"]
}

示例

我私有仓库的 IP 是 192.168.33.148,我的配置文件修改如下:

{
  "insecure-registries" : ["192.168.33.148:5000"]
}

然后重启 当前主机的 docker 服务。

sudo systemctl restart docker

接下来就可以使用 docker pull 下载私有仓库的镜像了。

示例

下载 192.168.33.148:5000/ubuntu24.04:latest 镜像。

weimingze@mzstudio:~$ sudo docker pull  192.168.33.148:5000/ubuntu2404
Using default tag: latest
latest: Pulling from ubuntu2404
Digest: sha256:0b9e751164f0f576086bb03062186c5643fe6dc56223b6bfef0be2a9d4828c67
Status: Downloaded newer image for 192.168.33.148:5000/ubuntu2404:latest
192.168.33.148:5000/ubuntu2404:latest

查看下载的镜像

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

至此,私有仓库搭建并测试完成。