Files
nexus/raw/Home Office/WSL2 中 Docker 容器访问宿主机代理.md
2026-04-30 06:57:02 +08:00

1.1 KiB
Raw Blame History

#wsl #docker #proxy

容器内的 127.0.0.1 指向容器自身,无法访问宿主机代理。需用 Docker 内置 DNS 名称替代。

解决方案

将 127.0.0.1 替换为 host.docker.internalDocker 会自动解析为宿主机 IP。

FROM python:3.12-slim
WORKDIR /app

RUN apt-get update && apt-get install -y \
    libpq-dev gcc \
    && rm -rf /var/lib/apt/lists/*

COPY requirements/base.txt requirements/base.txt

RUN pip install --no-cache-dir \
    --proxy http://host.docker.internal:10808 \
    --timeout 120 \
    -r requirements/base.txt

COPY . .
EXPOSE 8000
CMD ["uvicorn", "config.asgi:application", "--host", "0.0.0.0", "--port", "8000"]

其他命令同理

apt

apt-get -o Acquire::http::Proxy="http://host.docker.internal:10808" update

curl

curl -x http://host.docker.internal:10808 <url>

注意事项

  • 代理软件Clash 等)需开启「允许局域网连接」
  • 端口号按实际代理端口调整Clash 默认 7890
  • host.docker.internal 在 Docker DesktopWindows/Mac及 WSL2 环境下均可用
  • 避免在 Dockerfile 中硬编码 ENV HTTPS_PROXY=http://127.0.0.1:...,容器内无法访问