簡介
- 小伙伴們好,今天介紹一個docker鏡像拉取失敗的解決辦法,也就是使用7層代理工具shadow socks的方式
- 目前對於鏡像拉去失敗,主流的解決方案,要么國內外做鏡像的同步, 要么就是使用代理; 而做鏡像同步會耗費較多的資源,不是最優的選擇, 建議使用代理的方式
- 據我所知,目前存在的拉取失敗,要么是國內服務器拉取國外鏡像倉庫鏡像失敗,比如 docker.io、us.gcr.io;或國外服務器拉取國內鏡像倉庫鏡像失敗,比如 registry.cn-hangzhou.aliyuncs.com
- 當然我們可以通過 在能夠拉取的地方下載鏡像命令
docker save -o myimages.gz williamguozi/httpd:v0.1 williamguozi/httpd:v0.2
,之后在該服務器上加載該鏡像docker load -i myimages.gz
, 偶爾一兩次還可以,持續更新就很難接受了 - 今天就將該代理方式介紹給大家,希望對需要的小伙伴有所幫助
- 邏輯結構如圖
所有操作在需要拉取鏡像的服務器上執行
依賴環境
- 該依賴是ss運行環境所需要的庫
apt update
apt-get install build-essential wget -y
wget https://github.com/jedisct1/libsodium/releases/download/1.0.10/libsodium-1.0.10.tar.gz
tar xzvf libsodium-1.0.10.tar.gz
cd libsodium*
./configure
make -j8 && make install
echo /usr/local/lib > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
ss 客戶端
- 安裝
shadow socks
客戶端,用於連接ss服務器 shadow socks
服務器自行搜索資料建設,選址需滿足拉取鏡像的服務器可達,並且可訪問目標鏡像源- 以下為客戶端參考配置,實際情況應更改 server(可只填寫一個),server_port,password,method
apt-get install python-pip -y
pip install shadowso\
cks
cat > /etc/ss.json << EOF
{
"server":["100.100.100.100","200.200.200.220"],
"server_port":2911,
"local_port":1080,
"password":"password",
"timeout":600,
"method":"chacha20"
}
EOF
#啟動服務
sslocal -c /etc/ss.json -d start
安裝privoxy 轉socket 到http https工具
- ss 代理后的數據為socket格式,需要轉換為http和https
apt-get install privoxy -y
# 需添加如下配置
vim /etc/privoxy/config
forward-socks5 / 127.0.0.1:1080 .
listen-address 127.0.0.1:8118
systemctl start privoxy
systemctl enable privoxy
Docker 使用代理
- 首先需檢測docker重啟是否會導致容器重啟
- 該配置文件需要有如下配置,否則重啟docker服務會導致所有容器重啟
- 如果之前沒有該配置,添加后,第一次重啟仍然會導致所有容器重啟
cat > /etc/docker/daemon.json << EOF
{
"live-restore": true,
"group": "docker"
}
EOF
cat docker.service |grep process
# kill only the docker process, not all processes in the cgroup
KillMode=process
- 修改容器服務啟動參數
Environment=HTTP_PROXY=http://127.0.0.1:8118/
Environment=HTTPS_PROXY=http://127.0.0.1:8118/
代理http httpsEnvironment=NO_PROXY=localhost,127.0.0.1,docker.io
不要代理的鏡像倉庫源域名,否則將全部代理
vim /etc/systemd/system/multi-user.target.wants/docker.service
[Service]
Environment=HTTP_PROXY=http://127.0.0.1:8118/
Environment=HTTPS_PROXY=http://127.0.0.1:8118/
Environment=NO_PROXY=localhost,127.0.0.1,docker.io
systemctl daemon-reload
systemctl restart docker
總結
- 在該服務器上重新拉取阿里雲鏡像,發現可以正常拉取
- 如有疑問或更好的解決方案,歡迎留言交流
參考文檔
- docker 使用 socks5代理:https://blog.csdn.net/S1234567_89/article/details/73223200
- 安裝libsodium庫解決libsodium not found問題: https://www.debugnode.com/ubuntul_ibsodium
- 如何保證 docker daemon重啟,但容器不重啟: https://blog.csdn.net/qianggezhishen/article/details/71082689