1. 程式人生 > >docker構建國內映象服務

docker構建國內映象服務

       在國內想要下載映象比較困難,因此很多公司都構建自己的私有倉庫。如何搭建私有倉庫,請參考docker私有倉庫從無到有》。然而即使私有倉庫服務構建完成,但是裡面沒有映象,一樣很苦惱。今天介紹一下如何利用國內雲服務商提供的映象加速器,來快速下載映象。

  目前國內docker映象加速器服務商,質量比較好的有阿里雲、DaoCloud以及Docker中國。

它們配置都是相同,獲取專屬加速地址,然後將地址寫到docker配置檔案/etc/docker/daemon.json中。

一、阿里雲加速地址:

     1、註冊阿里雲賬號,如果有淘寶號也是可以的。https://dev.aliyun.com/search.html

  2、註冊並登陸成功之後,進入之後點選管理中心

  3、進入管理中心,預設業務就是下圖,然後在選擇映象加速器,裡面就是專屬加速地址,並且有配置方法。


 DaoCloud配置方式與阿里雲類似,只是服務商不一樣罷了。

二、Docker中國

  如果不想註冊阿里雲,那麼可以使用Docker中國提供的服務。

  可以將https://registry.docker-cn.com,設定到registry-mirrors中即可(與阿里雲設定方式一樣)。具體方法,可參考https://www.docker-cn.com/registry-mirror

  以上設定好映象之後,不需要docker login進行登入。

三、遇到問題

  問題1:設定完加速地址之後,輸入如下命令:

[[email protected] docker]# docker pull mysql
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
[[email protected] docker]# 
[[email protected]
docker]#

  檢視具體日誌資訊:

[[email protected] docker]# grep docker /var/log/messages
Mar  7 15:59:06 localhost dockerd: time="2018-03-07T15:59:06.551882623+08:00" level=warning msg="Error getting v2 registry: Get https://6jwksk60.mirror.aliyuncs.com/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
Mar  7 15:59:06 localhost dockerd: time="2018-03-07T15:59:06.551997545+08:00" level=error msg="Not continuing with pull after error: Get https://6jwksk60.mirror.aliyuncs.com/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
Mar  7 15:59:06 localhost dockerd: time="2018-03-07T15:59:06.552080576+08:00" level=error msg="Handler for POST /v1.35/images/create returned error: Get https://6jwksk60.mirror.aliyuncs.com/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
Mar  7 15:59:08 localhost dockerd: time="2018-03-07T15:59:08.441427015+08:00" level=debug msg="Calling GET /_ping"
Mar  7 15:59:08 localhost dockerd: time="2018-03-07T15:59:08.449887376+08:00" level=debug msg="Calling GET /v1.35/info"
Mar  7 15:59:08 localhost dockerd: time="2018-03-07T15:59:08.485088164+08:00" level=debug msg="Calling POST /v1.35/images/create?fromImage=mysql&tag=latest"
Mar  7 15:59:08 localhost dockerd: time="2018-03-07T15:59:08.547151332+08:00" level=debug msg="hostDir: /etc/docker/certs.d/6jwksk60.mirror.aliyuncs.com"
Mar  7 15:59:08 localhost dockerd: time="2018-03-07T15:59:08.547231472+08:00" level=debug msg="Trying to pull mysql from https://6jwksk60.mirror.aliyuncs.com/ v2"
Mar  7 15:59:23 localhost dockerd: time="2018-03-07T15:59:23.548000137+08:00" level=warning msg="Error getting v2 registry: Get https://6jwksk60.mirror.aliyuncs.com/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
Mar  7 15:59:23 localhost dockerd: time="2018-03-07T15:59:23.548084051+08:00" level=info msg="Attempting next endpoint for pull after error: Get https://6jwksk60.mirror.aliyuncs.com/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
Mar  7 15:59:23 localhost dockerd: time="2018-03-07T15:59:23.548133902+08:00" level=debug msg="Trying to pull mysql from https://registry-1.docker.io v2"
Mar  7 15:59:38 localhost dockerd: time="2018-03-07T15:59:38.548628804+08:00" level=warning msg="Error getting v2 registry: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
Mar  7 15:59:38 localhost dockerd: time="2018-03-07T15:59:38.548715275+08:00" level=info msg="Attempting next endpoint for pull after error: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
Mar  7 15:59:38 localhost dockerd: time="2018-03-07T15:59:38.548798878+08:00" level=error msg="Handler for POST /v1.35/images/create returned error: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"

  實際錯誤是:

  Get https://6jwksk60.mirror.aliyuncs.com/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"

【定位過程】

  通過谷歌翻牆(百度不行,解決不了這個問題),一共大概有兩種方式

  1、設定dns,在/etc/resolv.conf,以及/etc/docker/daemon.json中增加dns。例如dns地址:8.8.8.8

  2、docker代理(centosubuntu同樣設定)

   [[email protected] docker]# mkdir -p /etc/systemd/system/docker.service.d
   [[email protected] docker]# cat > /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
   > [Service]
   > Environment="HTTP_PROXY=http://proxy.example.com:80/" "HTTPS_PROXY=https://proxy.example.com:443/"
   > EOF
   [[email protected] docker]#

  然後重啟服務

   [[email protected] docker]# systemctl daemon-reload
   [[email protected] docker]# systemctl restart docker

  檢視配置

   [[email protected] docker]# systemctl show --property=Environment docker
   Environment=HTTP_PROXY=http://proxy.example.com:80/ HTTPS_PROXY=https://proxy.example.com:443/
   [[email protected] docker]# 

  設定完成後再進行docker pull就可以下載了。

【分析一下原因】

  1、通過curl來判斷服務是正常的,所以肯定是docker配置問題。

  2、當前環境在公司內網,公司是有代理的,因此可以肯定是公司網路問題,通過谷歌搜尋,最終確定需要為docker指定代理。