Docker设置代理

docker设置代理

一、先验证代理到 Docker Hub 是否可达

root@ly:~# curl -x http://192.168.101.7:8983 https://registry-1.docker.io/v2/
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}

二、给 Docker 守护进程配置代理(关键)

#使用你已验证可用的 Clash 端口 8983(HTTP 代理)

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/proxy.conf >/dev/null <<'EOF'
[Service]
Environment="HTTP_PROXY=http://192.168.101.7:8983"
Environment="HTTPS_PROXY=http://192.168.101.7:8983"
Environment="NO_PROXY=localhost,127.0.0.1,::1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,.svc,.cluster.local"
EOF

sudo systemctl daemon-reload
sudo systemctl restart docker
systemctl show -p Environment docker

#叠加 Docker Hub 加速和 DNS(提高成功率)
sudo tee /etc/docker/daemon.json >/dev/null <<'EOF'
{
  "registry-mirrors": ["https://docker.m.daocloud.io"],
  "dns": ["8.8.8.8", "1.1.1.1"]
}
EOF
sudo systemctl restart docker

#先拉小镜像验证,再拉 GitLab
docker pull busybox
docker pull gitlab/gitlab-ce:latest
cd /opt/sre-lab/infra && docker compose -f gitlab-compose.yml up -d