我一直尝试通过本地 socks5 代理运行 docker,我尝试设置如下环境变量:
export ALL_PROXY=127.0.0.1:1080
但这并没有什么区别,好像 Docker 没有读取它,我也尝试将配置放入~/.docker/config.json
:
{
"proxies":
{
"default":
{
"httpProxy": "socks5://127.0.0.1:1080"
}
}
}
但这也不起作用。问题是什么,如何在 Ubuntu 上使用 socks5 代理运行 Docker?
答案1
Ubuntudocker.service
中的 与用户的环境变量无关,但如果将其传递给其docker.service
本身,那么它就可以工作。
/lib/systemd/system/docker.service
我通过在文件之前添加以下行来使其工作ExecStart
:
Environment="ALL_PROXY=socks5://127.0.0.1:1080"
所以我的docker.service
文件看起来像这样:
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
Environment="ALL_PROXY=socks5://127.0.0.1:1080"
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
[Install]
WantedBy=multi-user.target
这似乎已经为我解决了这个问题并且它正在 Ubuntu 20.04 上运行。
答案2
另一个解决方案是使用privoxy
并将其转发到您的 socks5 代理,以便您可以将 privoxy 用作 https/http 代理。