環境準備系統: 主機兩台,分別是docker私有庫伺服器(IP 192.168.121.121)和用戶開發機(IP 192.168.121.122),開發機從私有庫伺服器拉取鏡像。 1、配置軟體源並安裝安裝docker兩台主機安裝docker 2、搭建私有鏡像倉庫登陸私有庫伺服器 創建docker管 ...
環境準備
系統:
cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core)
主機兩台,分別是docker私有庫伺服器(IP 192.168.121.121)和用戶開發機(IP 192.168.121.122),開發機從私有庫伺服器拉取鏡像。
1、配置軟體源並安裝安裝docker
兩台主機安裝docker
yum install docker-ce
2、搭建私有鏡像倉庫
登陸私有庫伺服器
創建docker管理賬戶並設置密碼
useradd dkuser passwd dkuser
把賬戶加入docker組,這一步是為了可以使用普通賬戶管理docker,而不用使用sudo命令
usermod -G docker dkuser
為賬戶配置sudo許可權
visudo
添加下麵一行
dkuser ALL=(ALL) NOPASSWD:ALL
切換賬戶
su dkuser
啟動docker服務
sudo systemctl start docker
可以通過官方提供的registry鏡像來搭建本地的私有倉庫,所以拉取官方鏡像
docker pull registry
創建存放鏡像和docker配置文件的目錄
sudo mkdir /data/docker
創建配置文件,storage配置中的delete=true配置項,是為了允許刪除鏡像。預設的鏡像是沒有這個參數
sudo vi /data/docker/config.yml
1 version: 0.1 2 3 log: 4 fields: 5 service: registry 6 storage: 7 delete: 8 enabled: true 9 cache: 10 blobdescriptor: inmemory 11 filesystem: 12 rootdirectory: /var/lib/registry 13 http: 14 addr: :5000 15 headers: 16 X-Content-Type-Options: [nosniff] 17 health: 18 storagedriver: 19 enabled: true 20 interval: 10s 21 threshold: 3View Code
運行registry容器
docker run -d -p 5000:5000 -v /data/docker/registry:/var/lib/registry -v /data/docker/config.yml:/etc/docker/registry/config.yml --restart always --name registry registry
說明:
-d -p 5000:5000 埠映射
-v /data/docker/registry:/var/lib/registry 預設情況下,會將倉庫存放於容器內的/var/lib/registry目錄下,指定本地目錄掛載到容器
--restart always 在容器退出時總是重啟容器,主要應用在生產環境
--name registry 指定容器的名稱
查看容器
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
071105c54db3 registry "/entrypoint.sh /etc…" 3 minutes ago Up About a minute 0.0.0.0:5000->5000/tcp registry
3、把本地鏡像上傳到私有倉庫
在私有庫伺服器上
這裡以busybox鏡像為例,因為比較小
首先把鏡像下載到本地
docker pull busybox
為鏡像打標簽
docker tag busybox 192.168.121.121:5000/busybox
查看鏡像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.121.121:5000/busybox latest db8ee88ad75f 7 hours ago 1.22MB
busybox latest db8ee88ad75f 7 hours ago 1.22MB
編輯配置文件,添加配置是為了能夠在本地上傳鏡像。
sudo vi /usr/lib/systemd/system/docker.service
在 ExecStart=/usr/bin/dockerd 後邊添加
--insecure-registry 192.168.121.121:5000
重啟docker服務
sudo systemctl daemon-reload sudo systemctl restart docker
推送鏡像到私有庫
docker push 192.168.121.121:5000/busybox The push refers to repository [192.168.121.121:5000/busybox] 0d315111b484: Pushed latest: digest: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 size: 527
報錯解決:
docker push 192.168.121.121:5000/busybox The push refers to repository [192.168.121.121:5000/busybox] Get https://192.168.121.121:5000/v2/: http: server gave HTTP response to HTTPS client
因為Docker從1.3.X之後,與docker registry交互預設使用的是https,然而此處搭建的私有倉庫只提供http服務,所以當與私有倉庫交互時就會報上面的錯誤。為瞭解決這個問題需要在啟動docker server時增加啟動參數為預設使用http訪問
sudo vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd 後邊添加
--insecure-registry 192.168.121.121:5000
4、開發機從私有庫載入鏡像
同樣的也需要添加配置
vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd 後邊添加
--insecure-registry 192.168.121.121:5000
啟動docker服務
systemctl start docker
從私有庫載入鏡像
docker pull 192.168.121.121:5000/busybox Using default tag: latest latest: Pulling from busybox ee153a04d683: Pull complete Digest: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 Status: Downloaded newer image for 192.168.121.121:5000/busybox:latest
查看鏡像
docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.121.121:5000/busybox latest db8ee88ad75f 7 hours ago 1.22MB
5、刪除鏡像
在私有庫伺服器上
相應的參考命令:
#查詢鏡像
curl <倉庫地址>/v2/_catalog
#查詢鏡像tag(版本)
curl <倉庫地址>/v2/<鏡像名>/tags/list
#查詢鏡像digest_hash
curl --header "Accept:application/vnd.docker.distribution.manifest.v2+json" -I -XGET <倉庫地址>/v2/<鏡像名>/manifests/<tag>
#刪除鏡像API
curl -I -X DELETE "<倉庫地址>/v2/<鏡像名>/manifests/<鏡像digest_hash>"
在伺服器執行下麵命令
查詢鏡像
curl -XGET http://192.168.121.121:5000/v2/_catalog {"repositories":["busybox"]}
查詢鏡像tag
curl http://192.168.121.121:5000/v2/busybox/tags/list {"name":"busybox","tags":["latest"]}
查詢鏡像digest_hash,刪除命令裡邊要填寫的 鏡像digest_hash 就是 查詢結果裡邊 Docker-Content-Digest: 後邊的內容
curl --header "Accept:application/vnd.docker.distribution.manifest.v2+json" -I -XGET http://192.168.121.121:5000/v2/busybox/manifests/latest HTTP/1.1 200 OK Content-Length: 527 Content-Type: application/vnd.docker.distribution.manifest.v2+json Docker-Content-Digest: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 Docker-Distribution-Api-Version: registry/2.0 Etag: "sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649" X-Content-Type-Options: nosniff Date: Fri, 19 Jul 2019 06:09:44 GMT
刪除私有庫鏡像
curl -I -XDELETE http://192.168.121.121:5000/v2/busybox/manifests/sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 HTTP/1.1 202 Accepted Docker-Distribution-Api-Version: registry/2.0 X-Content-Type-Options: nosniff Date: Fri, 19 Jul 2019 06:10:56 GMT Content-Length: 0
查看鏡像信息可以看到鏡像的標簽顯示為空 null
curl http://192.168.121.121:5000/v2/busybox/tags/list {"name":"busybox","tags":null}
這裡雖然刪除了,但是實際上硬碟地址還沒有釋放,是因為docker刪除p_w_picpath只是刪除的p_w_picpath的元數據信息。層數據並沒有刪除。現在進入registry中進行垃圾回收。
進入registry容器
docker exec -it 071105c54db3 /bin/sh
/ # cd /var/lib/registry/
查看鏡像大小
/var/lib/registry # du -sch 756.0K . 756.0K total
執行回收命令
/var/lib/registry # registry garbage-collect /etc/docker/registry/config.yml busybox 0 blobs marked, 3 blobs and 0 manifests eligible for deletion blob eligible for deletion: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/89/895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 go.version=go1.11.2 instance.id=7c37e39d-6ad1-4139-b9d1-592a900b0902 service=registry blob eligible for deletion: sha256:db8ee88ad75f6bdc74663f4992a185e2722fa29573abcc1a19186cc5ec09dceb INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/db/db8ee88ad75f6bdc74663f4992a185e2722fa29573abcc1a19186cc5ec09dceb go.version=go1.11.2 instance.id=7c37e39d-6ad1-4139-b9d1-592a900b0902 service=registry blob eligible for deletion: sha256:ee153a04d6837058642958836062f20badf39f558be3e6c7c7773ef7d8301d90 INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/ee/ee153a04d6837058642958836062f20badf39f558be3e6c7c7773ef7d8301d90 go.version=go1.11.2 instance.id=7c37e39d-6ad1-4139-b9d1-592a900b0902 service=registry
再次查詢,發現回收資源執行成功
/var/lib/registry # du -sch 0 . 0 total
參考:
https://www.cnblogs.com/Tempted/p/7768694.html
https://blog.csdn.net/jiangeeq/article/details/81056055
https://www.cnblogs.com/HwyStudy/p/10483163.html
https://blog.51cto.com/302876016/1966816