centos7系統下搭建docker本地鏡像倉庫


## 准備工作

用到的工具, Xshell5, Xftp5, docker.io/registry:latest鏡像

關於docker的安裝和設置加速, 請參考這篇博文centos7系統下 docker 環境搭建

參考之前的博文, 設置完加速后, 執行docker pull registry命令, 下載docker.io/registry官方鏡像

## 啟動registry鏡像

啟動docker.io/registry容器, 如果tag是latest, 可以忽略不寫

docker run -d -p 80:5000 --restart=always --name local_registry docker.io/registry:latest

-d 后台運行
-p 端口映射, 宿主機80端口映射給容器的5000端口
--restart=always 容器意外關閉后, 自動重啟 (如果重啟docker服務, 帶這個參數的, 能自動啟動為Up狀態, 不帶這個的,不會自動啟動)
--name 給容器起個名字, 可以根據這個名字去停止/啟動/刪除容器

## 配置端口開放

```
[root@localhost docker]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost docker]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 
    
[root@localhost docker]# firewall-cmd --reload
success
[root@localhost docker]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh
  ports: 80/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 
```

配置端口開放之后, 需要執行firewall-cmd --reload才能生效

## 重命名鏡像

[root@localhost docker]# docker images
REPOSITORY                                           TAG                 IMAGE ID            CREATED             SIZE
repos_local/zookeeper                                0.0.1               bdb481b4f17a        2 days ago          541.5 MB

repos_local/zookeeper 是上篇博文介紹的使用Dockerfile文件創建的鏡像, 重命名

docker tag repos_local/zookeeper:0.0.1 192.168.199.131/repos_local/zookeeper:latest

docker tag 原鏡像名:tag 新鏡像名:tag

docker images 查看鏡像名稱是否更改正確

[root@localhost docker]# docker images
REPOSITORY                                           TAG                 IMAGE ID            CREATED             SIZE
192.168.199.131/repos_local/zookeeper                latest              bdb481b4f17a        2 days ago          541.5 MB

 

## 推送鏡像

docker push 192.168.199.131/repos_local/zookeeper:latest
如果提示以下錯誤, 說明沒有把搭建的registry加入可信任的列表里面, 如果有https域名或者能創建.crt證書, 那么可以忽略以下步驟

Error response from daemon: invalid registry endpoint https://192.168.199.131/v0/: unable to ping registry endpoint https://192.168.199.131/v0/
v2 ping attempt failed with error: Get https://192.168.199.131/v2/: dial tcp 192.168.199.131:443: no route to host
 v1 ping attempt failed with error: Get https://192.168.199.131/v1/_ping: dial tcp 192.168.199.131:443: no route to host. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 192.168.199.131` 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.199.131/ca.crt

解決方法:

只針對centos7下 Docker version 1.12.5, build 047e51b/1.12.5版本有效, 其它版本沒做過測試

vi /etc/sysconfig/docker

注意--insecure-registry 192.168.199.131插入的位置
# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false --insecure-registry 192.168.199.131'
if [ -z "${DOCKER_CERT_PATH}" ]; then
    DOCKER_CERT_PATH=/etc/docker
fi

重啟docker服務
systemctl restart docker.service

重新執行docker push 192.168.199.131/repos_local/zookeeper:latest , 這次應該就能成功了

## 查看鏡像倉庫

[root@localhost docker]# curl 192.168.199.131/v2/_catalog
{"repositories":["repos_local/zookeeper"]}
[root@localhost docker]# curl 192.168.199.131/v2/repos_local/zookeeper/tags/list
{"name":"repos_local/zookeeper","tags":["latest"]}

 

至於鏡像的刪除, 目前還沒找到一個好的解決方法, 如有建議請留言

 

 

 

 

如需幫助可向我發起QQ聊天發起QQ聊天

友情贊助

如果您喜歡此文,感覺對您工作有幫助,預期領導會給您漲工資,不妨小額贊助一下,讓我有動力繼續努力。

贊助方式:打開支付寶App,使用“掃一掃”付款,付款碼見下圖,別忘了付款留言哦!


或使用微信, 不用加好友就能付款


免責聲明!

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



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