教你搭建 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

發現會報以下錯誤:

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"]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章