如何在 Ubuntu 中将 Docker 与ocks5 结合使用?

Mik*_*ahi 1 proxy systemd docker

我一直在尝试通过本地socks5代理运行docker,我尝试设置环境变量如下:

export ALL_PROXY=127.0.0.1:1080
Run Code Online (Sandbox Code Playgroud)

但这没有什么区别,就好像 Docker 没有读取它一样,我也尝试将配置放入~/.docker/config.json

{
 "proxies":
 {
   "default":
   {
     "httpProxy": "socks5://127.0.0.1:1080"
   }
 }
}
Run Code Online (Sandbox Code Playgroud)

但这也行不通。有什么问题以及如何在 Ubuntu 上使用ocks5代理运行 Docker?

Mik*_*ahi 5

Ubuntudocker.service中的 与用户的环境变量无关,但如果将其传递给自身docker.service,则它可以工作。

/lib/systemd/system/docker.service我通过在文件之前添加以下行来使其工作ExecStart

Environment="ALL_PROXY=socks5://127.0.0.1:1080"
Run Code Online (Sandbox Code Playgroud)

所以我的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
Run Code Online (Sandbox Code Playgroud)

这似乎已经解决了我的问题,并且它可以在 Ubuntu 20.04 上运行。