You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker中NPM安装失败:目标机器主动拒绝连接问题求助

Docker构建Debian源连接失败问题解决

问题现象

执行Docker构建时出现Debian软件包拉取失败错误,示例错误:

139.5 E: Failed to fetch http://deb.debian.org/debian/pool/main/n/node-string-width/node-string-width_4.2.3%2b%7ecs13.2.3-1_all.deb 403 connecting to deb.debian.org:80: connecting to :80: dial tcp :80: connectex: No connection could be made because the target machine actively refused it. [IP: 80]
failed to solve: process "/bin/sh -c apt install -y npm" did not complete successfully: exit code: 100

添加8.8.8.8作为Docker DNS后,出现域名解析错误:

210.7 E: Failed to fetch http://deb.debian.org/debian/pool/main/n/node-promise-retry/node-promise-retry_2.0.1-4_all.deb 403 connecting to deb.debian.org:80: resolving host deb.debian.org: lookup deb.debian.org: getaddrinfow: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. [IP: 80]

已尝试操作:

  • 开启Docker Desktop的Expose daemon on tcp://localhost:2375 without TLS选项
  • 重启机器和路由器
  • 添加8.8.8.8作为Docker Daemon DNS

提供的配置文件

Dockerfile内容

# Step 1: Use official lightweight Python image as base OS.
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7

# ARG GOOGLE_APPLICATION_CREDENTIALS="/usr/src/app/serviceAccount.json"

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies

# Bundle app source
COPY app /usr/src/app
ENV GOOGLE_APPLICATION_CREDENTIALS="./serviceAccount.json"
ENV SENTRY_SDK_DSN="https://36508a31dbb3468994b7ae470704ce10@o718840.ingest.sentry.io/5783806"

WORKDIR pdf2jsonl
ENV PATH pdf2jsonl/node_modules/.bin:$PATH
COPY package.json .
COPY package-lock.json .
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
RUN apt update && apt-get upgrade -y
RUN apt-get install -y nodejs
RUN npm install
COPY . ./
RUN sed -i 's/token in cache/token in cache \&\& typeof cache[token] === "string"/g' node_modules/gpt-3-encoder/Encoder.js

WORKDIR ..
# Step 3. Install production dependencies.
RUN pip install -r requirements.txt

EXPOSE 80

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]

docker-compose.yml内容

version: '3.8'

services:
  web:
    build: ./app
    command: uvicorn main:app --reload --workers 1 --host 0.0.0.0 --port 8000
    volumes:
      - ./app:/usr/src/app
      - /usr/src/app/pdf2jsonl/node_modules
    ports:
      - 8000:8000
    environment:
      - ENVIRONMENT=dev
      - TESTING=0
      - GOOGLE_APPLICATION_CREDENTIALS=/usr/src/app/serviceAccount.json

解决方案

1. 检查Docker Desktop代理设置

  • 打开Docker Desktop设置 -> Resources -> Proxies
  • 选择Use system proxy settings,或手动填写网络所需的HTTP/HTTPS代理
  • 在No proxy列表中添加localhost,127.0.0.1,deb.debian.org,避免代理拦截内部和源站请求
  • 应用设置后重启Docker Desktop

2. 替换Debian源为国内镜像

Debian官方源国内访问不稳定,在Dockerfile的apt update前添加源替换命令:

RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
RUN sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list

修改后的相关代码段:

RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
# 替换为国内Debian镜像源
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
RUN sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
RUN apt update && apt-get upgrade -y
RUN apt-get install -y nodejs

3. 配置Docker Daemon使用国内DNS

  • 打开Docker Desktop设置 -> Docker Engine
  • 在配置JSON中添加国内DNS服务器,示例:
{
  "dns": ["223.5.5.5", "114.114.114.114"]
}
  • 应用配置后重启Docker Desktop,避免8.8.8.8可能存在的解析延迟问题

4. 检查防火墙与杀毒软件

  • 临时关闭Windows Defender防火墙或第三方杀毒软件,测试是否存在网络拦截
  • 若关闭后恢复正常,添加docker.exe、dockerd.exe到防火墙允许列表,允许其访问网络

5. 优化npm安装速度

在Dockerfile的npm install前添加国内镜像源配置:

RUN npm config set registry https://registry.npmmirror.com

内容的提问来源于stack exchange,提问作者johnnyrocket33

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 12:26:32