给wsl内的docker配置代理

2025-03-22  本文已影响0人  七镜

wsl内的docker,使用systemd服务部署的,这时候想要给docker配置代理的话,一定要在systemd配置文件中配置,不能在 ~/.bashrc 中部署,因为systemd 启动服务时,实际上是以 独立的环境 运行的,通常不会自动加载登录 shell 中的配置文件(如 /etc/profile 或 ~/.bashrc)。因此,systemd 启动的进程需要显式地配置环境变量才能让其生效。

systemd服务配置如下:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target

[Service]
Environment="HTTP_PROXY=http://172.17.144.1:8001"
Environment="HTTPS_PROXY=http://172.17.144.1:8001"
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
ExecStart=/usr/bin/dockerd --exec-opt native.cgroupdriver=systemd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.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
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

可以看到配置完成之后,就可以正常下载docker镜像了。


上一篇 下一篇

猜你喜欢

热点阅读