环境准备与镜像拉取
本次操作的环境配置如下所示。
操作系统为 CentOS 7。
服务器 IP 地址设定为 10.211.55.30。
使用的 Docker 版本为 1.10.3。
计划部署的镜像仓库版本为 v2。
首先需要在 IP 为 10.211.55.30 的机器上拉取 registry 官方镜像。
$ docker pull registry
若需离线安装,也可通过导入镜像的方式完成。可从网盘获取资源:https://pan.baidu.com/s/1jHZlz2u。
随后进入 Docker 环境执行镜像导入操作。
$ docker load -i registry.tar
镜像下载完成后,利用该镜像启动一个容器实例。
$ docker run -d -p 5000:5000 registry
默认情况下,仓库存放于容器内的 /tmp/registry 目录下。若容器被删除,其中的镜像数据也会随之丢失。因此,通常建议将本地目录挂载至容器的 /tmp/registry 路径下。此处将 /opt/data/registry 目录映射到 /tmp/registry,若本地不存在该目录需新建,并赋予其相应的读写权限。
chmod +777 /opt/data/registry
注意此处存在差异:虽然文档常提及 /tmp/registry,但在实际测试中,容器内的镜像存储位置位于 /var/lib/registry。这是通过搭建完毕并上传镜像后,使用 find / -name *** 命令搜索确认的。
[root@server01 ~]# docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry
55c60589cb0e2d094d5371c4dd650127cfeae1b361477d50cfe48552e6308830
启动成功后,可通过地址 10.211.55.30:5000 访问该仓库服务。
接下来进行功能测试。
目标是将本地镜像推送到私有仓库。首先在 10.211.55.30 机器上拉取一个体积较小的测试镜像 busybox。
$ sudo docker pull busybox
修改该镜像的标签(tag),格式应为 镜像仓库IP:端口/镜像名称。
$ sudo docker tag busybox 10.211.55.30:5000/busybox
随后尝试将打好 tag 的镜像上传至私有仓库。
$ sudo docker push 10.211.55.30:5000/busybox
此时推送操作会失败,具体报错信息如下。
复制代码 代码如下: 2026/01/05 11:01:17 Error: Invalid registry endpoint https://192.168.112.136:5000/v1/: Get https://192.168.112.136:5000/v1/_ping: dial tcp 192.168.112.136:5000: connection refused. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 192.168.112.136:5000` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/192.168.112.136:5000/ca.crt
自 Docker 1.3.X 版本起,与 registry 交互默认启用 HTTPS。由于当前搭建的私有仓库仅支持 HTTP,导致连接被拒。解决方法是在启动 Docker 服务时添加参数以允许 HTTP 访问。需修改 10.211.55.30 机器的配置文件,在 CentOS 7 中路径为 /usr/lib/systemd/system/docker.service,在其中增加 --insecure-registry 10.211.55.30:5000。
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target rhel-push-plugin.socket
Wants=docker-storage-setup.service
[Service]
Type=notify
NotifyAccess=all
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
ExecStart=/usr/bin/docker-current daemon \
--exec-opt native.cgroupdriver=systemd \
--insecure-registry=10.211.55.30:5000 \
$OPTIONS \
$DOCKER_STORAGE_OPTIONS \
$DOCKER_NETWORK_OPTIONS \
$ADD_REGISTRY \
$BLOCK_REGISTRY \
$INSECURE_REGISTRY
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
MountFlags=slave
Restart=on-abnormal
[Install]
WantedBy=multi-user.target
配置修改完毕后,重启 Docker 服务使更改生效。
$ restart docker
服务重启后,再次执行推送命令,将本地镜像推送到私有服务器。
$ sudo docker push 10.211.55.30:5000/busybox
此时可以看到镜像已成功 push 到私有仓库中。
若仍无法成功,可运行 journalctl -f 查看日志。日志中可能显示 SELinux 相关的错误提示。处理方法通常是直接关闭 SELinux。
Jan 27 16:08:16 server01 docker-current[15241]: time="2026-01-27T08:08:16Z" level=error msg="response completed with error" err.code="blob unknown" err.detail=sha256:45a2e645736c4c66ef34acce2407ded21f7a9b231199d3b92d6c9776df264729 err.message="blob unknown to registry" go.version=go1.7.3 http.request.host="10.211.55.30:5000" http.request.id=a2dbff10-2937-4e9e-94f3-16275739ad61 http.request.method=HEAD http.request.remoteaddr="10.211.55.30:48256" http.request.uri="/v2/centos_20260127/blobs/sha256:45a2e645736c4c66ef34acce2407ded21f7a9b231199d3b92d6c9776df264729" http.request.useragent="docker/1.10.3 go/go1.6.3 git-commit/cb079f6-unsupported kernel/3.10.0-327.36.3.el7.x86_64 os/linux arch/amd64" http.response.contenttype="application/json; charset=utf-8" http.response.duration=1.4262ms http.response.status=404 http.response.written=157 instance.id=f9f97de9-15bc-41e3-9ec3-f1033e57a77e vars.digest="sha256:45a2e645736c4c66ef34acce2407ded21f7a9b231199d3b92d6c9776df264729" vars.name="centos_20260127" version=v2.6.0
Jan 27 16:08:16 server01 docker-current[15241]: 10.211.55.30 - - [27/Jan/2026:08:08:16 +0000] "HEAD /v2/centos_20260127/blobs/sha256:45a2e645736c4c66ef34acce2407ded21f7a9b231199d3b92d6c9776df264729 HTTP/1.1" 404 157 "" "docker/1.10.3 go/go1.6.3 git-commit/cb079f6-unsupported kernel/3.10.0-327.36.3.el7.x86_64 os/linux arch/amd64"
Jan 27 16:08:16 server01 docker-current[15241]: time="2026-01-27T08:08:16Z" level=error msg="response completed with error" err.code=unknown err.detail="filesystem: mkdir /var/lib/registry/docker: permission denied" err.message="unknown error" go.version=go1.7.3 http.request.host="10.211.55.30:5000" http.request.id=158612a0-39f5-41f5-9985-c80db7da911f http.request.method=POST http.request.remoteaddr="10.211.55.30:48258" http.request.uri="/v2/centos_20260127/blobs/uploads/" http.request.useragent="docker/1.10.3 go/go1.6.3 git-commit/cb079f6-unsupported kernel/3.10.0-327.36.3.el7.x86_64 os/linux arch/amd64" http.response.contenttype="application/json; charset=utf-8" http.response.duration=1.671997ms http.response.status=500 http.response.written=164 instance.id=f9f97de9-15bc-41e3-9ec3-f1033e57a77e vars.name="centos_20260127" version=v2.6.0
Jan 27 16:08:16 server01 docker-current[15241]: 10.211.55.30 - - [27/Jan/2026:08:08:16 +0000] "POST /v2/centos_20260127/blobs/uploads/ HTTP/1.1" 500 164 "" "docker/1.10.3 go/go1.6.3 git-commit/cb079f6-unsupported kernel/3.10.0-327.36.3.el7.x86_64 os/linux arch/amd64"
Jan 27 16:08:16 server01 docker-current[15241]: time="2026-01-27T16:08:16.115997771+08:00" level=error msg="Upload failed, retrying: Received unexpected HTTP status: 500 Internal Server Error"
Jan 27 16:08:16 server01 setroubleshoot[5771]: failed to retrieve rpm info for /opt/data/registry
Jan 27 16:08:16 server01 setroubleshoot[5771]: SELinux is preventing /bin/registry from write access on the directory /opt/data/registry. For complete SELinux messages. run sealert -l 748743d8-dd8a-4482-9771-94a403bccf18
Jan 27 16:08:16 server01 python[5771]: SELinux is preventing /bin/registry from write access on the directory /opt/data/registry.
***** Plugin catchall_labels (83.8 confidence) suggests *******************
If you want to allow registry to have write access on the registry directory
Then you need to change the label on /opt/data/registry
Do
# semanage fcontext -a -t FILE_TYPE '/opt/data/registry'
where FILE_TYPE is one of the following: cgroup_t, docker_var_lib_t, svirt_home_t, svirt_sandbox_file_t, virt_home_t.
Then execute:
restorecon -v '/opt/data/registry'
***** Plugin catchall (17.1 confidence) suggests **************************
If you believe that registry should be allowed write access on the registry directory by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# grep registry /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp
关闭 SELinux 的具体步骤如下。
首先检查 SELinux 的状态。
1、/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态
SELinux status: enabled
2、getenforce ##也可以用这个命令检查
接着执行关闭操作。
方法一:临时关闭,无需重启机器。
setenforce 0 ##设置SELinux 成为permissive模式 setenforce 1 设置SELinux 成为enforcing模式
方法二:修改配置文件,需重启机器生效。
编辑 /etc/selinux/config 文件。
将 SELINUX=enforcing 修改为 SELINUX=disabled。
保存后重启机器即可永久关闭。
完成上述步骤后,删除本地镜像,然后从私有仓库中 pull 下来验证。
$ sudo docker pull 10.211.55.30:5000/busybox
至此,Docker 私有仓库搭建完成。上述过程未配置身份认证。
以下介绍如何管理仓库中的镜像。
关于查询功能。
在 Private Registry v2 中,无法直接使用 docker search 命令检索 Repository 或 images,否则会报错。
$ docker search 10.211.55.30:5000/busybox/
Error response from daemon: Unexpected status code 404
但可以通过 v2 版本的 API 实现相同目的,请求格式必须遵循 IP:port/v2/_catalog。
[root@server01 ~]# curl http://10.211.55.30:5000/v2/_catalog
{"repositories":["centos"]}
[root@server01 ~]# curl http://10.211.55.30:5000/v2/centos/tags/list
{"name":"centos","tags":["latest"]}
拉取镜像的操作如下。
[root@server01 ~]# docker pull 10.211.55.30:5000/centos
Using default tag: latest
Trying to pull repository 10.211.55.30:5000/centos ...
latest: Pulling from 10.211.55.30:5000/centos
Digest: sha256:7dfffa13a2addc317ac3bdfbddbd4604ea629decea19c271481e5c45245b7612
Status: Downloaded newer image for 10.211.55.30:5000/centos:latest
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。