Docker注冊中心


一、概念

1、Docker注冊中心和倉庫

Registry(注冊中心或注冊服務器):存放倉庫的地方,一個注冊中心往往有很多倉庫。

Repositories(倉庫):集中存放鏡像文件的地方,一個倉庫存放多個鏡像文件,每個倉庫只存放一類鏡像,通過"倉庫名:標簽"指定特定版本的鏡像。

Docker倉庫分類:根據存儲的鏡像文件是否共享,分為 公開倉庫(Public Repositories)、私有倉庫(Private Repositories)。

2、Docker Hub

Docker Hub 是一個基於雲的注冊中心,為鏡像檢索、發布、變更、用戶和團隊、開發流程的自動化提供集中式的資源服務。

主要功能:

  1. 鏡像倉庫:上傳和下載容器鏡像。
  2. 團隊和組織:管理對容器鏡像的私有倉庫的訪問。
  3. 官方鏡像:下載和使用由Docker官方提供的高質量容器鏡像。
  4. 發布者鏡像:下載和使用由外部供應商提供的高質量容器鏡像。
  5. 構建:從GitHub和Bitbucket這兩個源代碼托管平台自動構建容器鏡像並將它們上傳到Docker Hub。當對源代碼庫進行修改時,會自動構建一個新鏡像。
  6. Webhooks:這是一個自動化構建特性,在一個鏡像上傳成功后,Webhooks會觸發操作,將Docker Hub與其他服務進行整合。

根據內容分層鏡像倉庫:

  1. 頂級鏡像倉庫:基礎鏡像
  2. 次級鏡像倉庫:特定命名空間下的鏡像倉庫,命名空間可能就是用戶名。例:gitlab/gitlab-runner

3、官方倉庫

指的是由廠商和貢獻者(比如:Oracle、Redhat)向Docker提供的公開的、經過認證的倉庫,能確保倉庫及時安全更新。用戶可以使用作為基礎鏡像構建自己的應用。

官方倉庫用途:

  1. 提供必要的基礎OS鏡像倉庫,作為大多數用戶構建鏡像的起點。
  2. 為流行的編程語言運行時、數據存儲和其他服務提供類似於平台即服務(PaaS)所提供的解決方案。
  3. 作為學習Dockerfile的最佳實踐,提供清晰的文檔供其他Dockerfile作者參考。
  4. 確保及時的安全更新

4、鏡像加速器

Docker Hub注冊中心部署在國外服務器。國內訪問會存在問題,Docker 官方提供了中國的加速器,直接使用加速器地址即可使用。

配置方法:

# 1.修改配置文件
vi /etc/docker/daemon.json
{
  "registry-mirrors":["https://registry.docker-cn.com"]
}
# 保存文件
# 2.重啟Docker
[root@hqs docker]# systemctl daemon-reload
[root@hqs docker]# systemctl restart docker

這個方法只能加速訪問流行的公開鏡像,私有鏡像仍然要去美國服務器中拉取。

另外可以配置相應的國內鏡像源來提高鏡像的下載速度和穩定性。阿里雲除了提供Docker Hub鏡像加速器之外,還提供與Docker Hub類似的容器鏡像服務,方便用戶進行鏡像全生命周期管理。

阿里雲加速器:

# 可以通過修改daemon配置文件/etc/docker/daemon.json來使用加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://nxwgbmaq.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

二、注冊中心相關操作

1、push推送

將鏡像推送到由其名稱或標簽指定的倉庫中。與pull命令相對。

[root@hqs docker-hello]# docker push --help
Usage:  docker push [OPTIONS選項] NAME倉庫名[:TAG標簽]
Push an image or a repository to a registry    # 推送一個鏡像或倉庫到注冊中心
Options:
      --disable-content-trust   Skip image signing (default true)     # 跳過鏡像簽名 

# 例子:
docker push hqs/testdocker:22.03

2、commit提交

通過 docker commit 命令將現有的容器提交來生成新的鏡像。

[root@hqs docker-hello]# docker commit --help
Usage:  docker commit [OPTIONS選項] CONTAINER容器 [REPOSITORY倉庫名[:TAG標簽]]
Create a new image from a containers changes
Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")    # 指定作者
  -c, --change list      Apply Dockerfile instruction to the created image             # 允許使用dockerfile的指令
  -m, --message string   Commit message                                  # 提交信息
  -p, --pause            Pause container during commit (default true)    # 創建鏡像過程中容器暫停(掛起)

3、login登錄 和 logout退出

使用 docker login 命令登錄docker注冊中心。

# 語法
[root@hqs docker]# docker login --help
Usage:  docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.        # 如果server沒有指定,默認從daemon獲取。
Options:
  -p, --password string   Password                   # 密碼
      --password-stdin    Take the password from stdin     # 標准輸入輸入密碼
  -u, --username string   Username    # 用戶名

# 案例:
[root@hqs docker-hello]# docker login --username=hqs2212586 registry.cn-hangzhou.aliyuncs.com

使用 docker logout 命令退出注冊中心登錄。

# 語法
[root@hqs docker]# docker logout --help
Usage:  docker logout [SERVER]
Log out from a Docker registry.
If no server is specified, the default is defined by the daemon.

# 示例:
[root@hqs docker]# docker logout
Removing login credentials for https://index.docker.io/v1/

三、操作案例

1、docker hub鏡像上傳

# 1.修改鏡像鏡像加速器
[root@hqs ~]# cd /etc/docker/
[root@hqs docker]# vi daemon.json
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}
[root@hqs docker]# systemctl daemon-reload
[root@hqs docker]# systemctl restart docker

# 2.登錄docker hub官網注冊賬號:https://hub.docker.com/signup

# 3.登錄賬戶,創建一個倉庫
# “Create Repository”——》輸入命名空間和倉庫名——》“Description”填寫描述信息——》“visibility”中選擇是創建public倉庫還是private倉庫——》點擊“Create”創建倉庫

# 4.登錄Docker Hub
[root@hqs docker]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you dont have a Docker ID, head over to https://hub.docker.com to create one.
Username: hqs2212586      # 輸入創建的用戶名
Password:                 # 輸入密碼並按回車
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

# 5.構建鏡像
[root@hqs docker-hello]# docker build -t hqs2212586/testdocker .
Sending build context to Docker daemon  865.3kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : COPY hello /
 ---> 2322533b4210
Step 3/3 : CMD ["/hello"]
 ---> Running in 5994e2099b8d
Removing intermediate container 5994e2099b8d
 ---> 32fedb0d83ba
Successfully built 32fedb0d83ba
Successfully tagged hqs2212586/testdocker:latest

# 6.為已存在的鏡像重新設置標簽
[root@hqs docker-hello]# docker tag 32fedb0d83ba hqs2212586/testdocker:22.03
[root@hqs docker-hello]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE
hqs2212586/testdocker   22.03               32fedb0d83ba        About a minute ago   861kB
hqs2212586/testdocker   latest              32fedb0d83ba        About a minute ago   861kB

# 7.推送鏡像
[root@hqs docker-hello]# docker push  hqs2212586/testdocker:22.03
The push refers to repository [docker.io/hqs2212586/testdocker]
88dc11111fec: Pushed 
22.03: digest: sha256:c9225ad48cb94c8f160e839187e81ce28f85dbff8316e7443879f5169f41cfb0 size: 527

# 8.在docker hub上檢查推送的倉庫
[倉庫地址](https://hub.docker.com/repository/docker/hqs2212586/testdocker)

2、容器生成鏡像提交docker hub

# 1.登錄官網賬戶,創建一個倉庫
# “Create Repository”——》輸入命名空間和倉庫名——》“Description”填寫描述信息——》“visibility”中選擇是創建public倉庫還是private倉庫——》點擊“Create”創建倉庫

# 2.容器提交生成鏡像
[root@hqs docker-hello]# docker commit 4decee095428 hqs2212586/ubuntu:22.03
sha256:a4b85b3fb73e23e75440430252477b77aee8927fdf9495160297b86dcf5f917c
[root@hqs docker-hello]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
hqs2212586/ubuntu       22.03               a4b85b3fb73e        6 seconds ago       72.8MB

# 3.推送鏡像
[root@hqs docker-hello]# docker push hqs2212586/ubuntu:22.03
The push refers to repository [docker.io/hqs2212586/ubuntu]
b954f8dfd459: Pushed 
9f54eef41275: Mounted from library/ubuntu 
22.03: digest: sha256:573bc189eba09934db466a2344edee5f46cd7d98fb765690b4ba7b1e9dd26624 size: 736

# 4.在docker hub上檢查推送的倉庫
[倉庫地址](https://hub.docker.com/repository/docker/hqs2212586/ubuntu)

3、第三方Docker注冊中心

阿里雲除了提供 Docker Hub 的鏡像加速服務,還提供自己的倉庫注冊服務————容器鏡像服務。
阿里雲容器鏡像服務(簡稱 ACR)是面向容器鏡像、Helm Chart 等符合 OCI 標准的雲原生制品安全托管及高效分發平台。

阿里雲倉庫的格式:域名/命名空間/倉庫名稱:標簽。示例:registry.cn-hangzhou.aliyuncs.com/huangqs/docker_test:22.03

# 1.登錄aliyun平台,注冊登錄
[阿里雲官網地址](https://www.aliyun.com)

# 2.在阿里雲控制台創建倉庫
# 打開阿里雲控制台——》產品與服務列表——》彈性計算里的容器鏡像服務,進入容器鏡像服務控制台
# 點擊“個人實例”——》點擊左側“鏡像倉庫”——》點擊“創建鏡像倉庫”——》輸入倉庫名稱、選擇倉庫類型、輸入倉庫的摘要或描述等信息——》點擊“下一步”——》選擇“本地倉庫”——》點擊“創建鏡像倉庫”

# 3.設置registry登錄密碼
# 使用docker login通過密碼登錄鏡像服務實例,需要先設置或獲取臨時密碼或固定密碼。
# 容器鏡像服務——》個人實例——》倉庫管理菜單里的訪問憑證——》設置固定密碼

# 4.登錄阿里雲Docker Registry
[root@hqs docker-hello]# docker logout
Removing login credentials for https://index.docker.io/v1/
[root@hqs docker-hello]# docker login --username=hqs2212586 registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

# 5.構建鏡像
[root@hqs docker-hello]# docker build -t registry.cn-hangzhou.aliyuncs.com/huangqs/docker_test:22.03 .
Sending build context to Docker daemon  865.3kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : COPY hello /
 ---> Using cache
 ---> 846e09a540e6
Step 3/3 : CMD ["/hello"]
 ---> Using cache
 ---> a5dbf1d93254
Successfully built a5dbf1d93254
Successfully tagged registry.cn-hangzhou.aliyuncs.com/huangqs/docker_test:22.03

# 6.將鏡像推送到Registry
[root@hqs docker-hello]#  docker push registry.cn-hangzhou.aliyuncs.com/huangqs/docker_test:22.03
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/huangqs/docker_test]
88dc11111fec: Pushed 
22.03: digest: sha256:b1d135c19b018cfbbba32085948cb125507b508549a8296568c25a81f5b0709b size: 527

# 7.驗證鏡像上傳
# 訪問容器鏡像服務控制台——》個人實例——》鏡像倉庫——》點擊倉庫名docker_test——》點擊鏡像版本。可以看到我們上傳的鏡像。

4、自建Docker注冊中心

因為安全和網絡問題,用戶可以建立自己的注冊中心提供鏡像倉庫注冊服務。

(1)基於容器安裝運行Registry

使用的 Docker Registry工具 已經開源,可以在Docker Hub上下載鏡像啟動容器。

[root@hqs ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
3d2430473443: Pull complete 
5bf98bf80c2f: Pull complete 
950c199aa45b: Pull complete 
92504897768b: Pull complete 
c6488f74dce8: Pull complete 
Digest: sha256:65be6503496c34ec234e89a831ca248b18c2e04c800d9d74af73866e3cda8578
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@hqs ~]# docker run -d -p 5000:5000 --restart=always --name myregistry -v /opt/data/registry:/var/lib/registry registry
3cac187c798b08d12da5f292a03621e2e27021763481e9313b6f7de57a722354
[root@hqs ~]# docker ps -a
CONTAINER ID     IMAGE        COMMAND                  CREATED             STATUS              PORTS                    NAMES
3cac187c798b     registry     "/entrypoint.sh /etc…"   29 seconds ago      Up 28 seconds       0.0.0.0:5000->5000/tcp   myregistry

# 驗證剛建立的注冊中心
[root@hqs ~]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}     # 此結果說明運行正常,注冊中心還沒有任何鏡像

(2)將鏡像上傳到自建的注冊中心

要注意自建的注冊中心倉庫標簽格式:[主機:端口]/倉庫名稱:標簽。

# 1.給已有的鏡像打標簽符合自建注冊中心格式
[root@hqs ~]# docker tag ubuntu 127.0.0.1:5000/ubuntu:v1
[root@hqs ~]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
hqs2212586/ubuntu                                       22.03               93a86f5131ec        3 hours ago         72.8MB
127.0.0.1:5000/ubuntu                                   v1                  ba6acccedd29        5 months ago        72.8MB
ubuntu                                                  latest              ba6acccedd29        5 months ago        72.8MB
[root@hqs ~]# docker tag hqs2212586/testdocker 127.0.0.1:5000/testdocker:v1

# 2.執行鏡像上傳
[root@hqs ~]# docker push 127.0.0.1:5000/ubuntu:v1
The push refers to repository [127.0.0.1:5000/ubuntu]
9f54eef41275: Pushed 
v1: digest: sha256:7cc0576c7c0ec2384de5cbf245f41567e922aab1b075f3e8ad565f508032df17 size: 529
[root@hqs ~]# docker push  127.0.0.1:5000/testdocker:v1
The push refers to repository [127.0.0.1:5000/testdocker]
88dc11111fec: Pushed 
v1: digest: sha256:b1d135c19b018cfbbba32085948cb125507b508549a8296568c25a81f5b0709b size: 527

# 3.執行測試
# 查詢registry中所有的鏡像名稱:curl -XGET http://倉庫地址:5000/v2/_catalog
[root@hqs ~]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":["testdocker","ubuntu"]}

# 依據鏡像名稱查詢鏡像版本: curl -XGET http://倉庫地址:5000/v2/鏡像倉庫名/tags/list
[root@localhost ~]# curl -XGET http://127.0.0.1:5000/v2/centos/tags/list
{"name":"centos","tags":["7","net"]}

(3)從自建注冊中心下載鏡像

[root@hqs ~]# docker rmi 127.0.0.1:5000/testdocker:v1
Untagged: 127.0.0.1:5000/testdocker:v1
Untagged: 127.0.0.1:5000/testdocker@sha256:b1d135c19b018cfbbba32085948cb125507b508549a8296568c25a81f5b0709b
[root@hqs ~]# docker pull 127.0.0.1:5000/testdocker:v1
v1: Pulling from testdocker
Digest: sha256:b1d135c19b018cfbbba32085948cb125507b508549a8296568c25a81f5b0709b
Status: Downloaded newer image for 127.0.0.1:5000/testdocker:v1
127.0.0.1:5000/testdocker:v1
[root@hqs ~]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
127.0.0.1:5000/testdocker                               v1                  a5dbf1d93254        3 hours ago         861kB

(4)配置注冊中心地址

本地服務器情況下使用127.0.0.1或者localhost作為注冊中心地址都是可以的。
但如果使用主機的域名或IP地址提供服務,會報錯,這是因為1.3版本后,docker注冊中心默認使用的https,但是搭建私有注冊中心默認使用的是http。

解決方法:修改其他docker客戶端/etc/docker/daemon.json文件,將地址添加到insecure-registries列表中,允許不安全的通信即可。

# 用法:修改daemon文件
{
  "insecure-registries":["192.168.88.101:5000"]
}
# 保存后重啟docker
systemctl restart docker

# 示例:在192.168.88.102機器上
[root@localhost ~]# vi /etc/docker/daemon.json
{
  "registry-mirrors":["https://registry.docker-cn.com"],
  "insecure-registries":["192.168.88.101:5000"]
}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker pull 192.168.88.101:5000/ubuntu:23.03
23.03: Pulling from ubuntu
b65bcf19d144: Pull complete 
Digest: sha256:12149a2b7cca0a69237b84c5d7a52bb7f7db451518ace6f4190800c003642bd9
Status: Downloaded newer image for 192.168.88.101:5000/ubuntu:23.03
192.168.88.101:5000/ubuntu:23.03
[root@localhost ~]# docker images
REPOSITORY                   TAG       IMAGE ID       CREATED         SIZE
192.168.88.101:5000/ubuntu   23.03     74f2314a03de   2 weeks ago     77.8MB


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM