教你搭建 Docker 私有仓库详细步骤

准备两台服务器
Docker 客户端:192.168.56.201
Docker 私有仓库服务器:192.168.56.202

docker 及 docker-compose 环境安装

请查看 CentOS 8 安装 docker/docker-compose 进行环境配置。

搭建步骤

1. 在仓库服务器 202 上通过以下 docker-compose 进行部署

目录结构

.
|-- docker-compose.yml
`-- registry # 容器目录,用于存放仓库文件

查看 docker-compose.yml 文件内容

version: '3'
services:
  registry:
    container_name: registry
    image: registry:latest
    privileged: true
    ports:
      - 5000:5000
    volumes:
      - ./registry:/var/lib/registry
    restart: always

2. 在仓库服务器 202 运行命令进行部署

执行部署

# docker-compose up -d

查看部署情况

# docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                                      NAMES
319ea4031989        registry:latest      "/entrypoint.sh /etc…"   About an hour ago   Up 40 minutes       0.0.0.0:5000->5000/tcp                     registry

3. 在客户端服务器 201 上制作精细

以 hello-world 为例,先把它从 hub.docker.com 官方仓库中拉取下来

# docker pull hello-world

给 hello-world 镜像打上 tag,表示最新的版本

# docker tag hello-world 192.168.56.202:5000/hello-world:latest

4. 将新的 hello-world 镜像上传到私有仓库

# docker push 192.168.56.202:5000/hello-world:latest

发现会报以下错误:

扫描二维码关注公众号,回复: 11330618 查看本文章
The push refers to a repository [192.168.56.202:5000/hello-world]
Get https://192.168..56.202:5000/v1/_ping: http: server gave HTTP response to HTTPS client

原因是 docker 私有仓库服务器,默认是基于 https 传输的,所以我们需要在客户端 201 上做相关设置,不使用 https 传输

# vi /etc/docker/daemon.json

增加以下代码

"insecure-registries":["182.168.56.201:5000"]

最终如下所示:

{
        "registry-mirrors": ["https://demo.mirror.aliyuncs.com"],
        "insecure-registries":["182.168.56.201:5000"]
}

依次执行以下命令,重启 docker

# systemctl daemon-reload
# systemctl restart docker

再次执行推送命令即可成功

# docker push 192.168.56.202:5000/hello-world:latest

执行输出:

The push refers to repository [192.168.56.202:5000/hello-world]
9c27e219663c: Pushed
latest: digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 size: 525

5. 查看刚上传的镜像

在私有仓库 202 上,通过以下命令查看:

# ls ./registry/docker/registry/v2/repositories/

输出:

hello-world

或者通过在客户端执行以下命令查看:

# curl http://192.168.56.202:5000/v2/_catalog

输出:

{"repositories":["hello-world"]}

猜你喜欢

转载自blog.csdn.net/qq_32828933/article/details/106636109