首页 > 解决方案 > docker 容器中的 Npm 安装找不到我的自定义注册表

问题描述

我正在尝试使用 docker-compose 在 node:alpine 图像上构建一个反应应用程序。但是当时间到了npm install它不适用于以下错误:

npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! network request to https://<myCustomRegistry>/packageName failed, reason: getaddrinfo ENOTFOUND <myCustomRegistry>
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-04-03T15_06_13_130Z-debug.log

myCustomRegistry是我通过 vpn 访问的注册表的名称。

我尝试了什么:

文件:

Dockerfile

# Builds the react app into 3 static files
FROM node:alpine as builder
WORKDIR /app
COPY . .
RUN npm i && npm run build

# Copies those static files and serve them
FROM node:alpine
RUN yarn global add serve
WORKDIR /app
COPY --from=builder /app/build .
EXPOSE 80
CMD ["serve", "-p", "80", "-s", "."]

Docker-compose 配置文件:

version: "3.7"
services:

  <... other services>
  front:
    build: ./front
    container_name: dobby_front
    ports: 
      - "8080:80"
    volumes:
      - './front:/app'
      - '/app/node_modules'
    dns:
      - 10.128.103.16
      - 172.21.103.5

.npmrc

registry=https://<myCustomRegistry>
save-exact=true

提前致谢

标签: node.jsdockernpmdocker-composedns

解决方案


最后,我决定使用一种解决方法。对于处理相同问题的任何人,这将在本地解决您的问题。请确保您正在使用的任何远程服务器也以这种方式配置。

据我了解,目前无法使用 docker-compose 在构建中使用自定义 DNS,它仅在运行中使用。因此,要传递自定义 DNS,您必须使用解决方法并将其添加到整个全局配置中。

转到/etc/docker并编辑或创建一个 JSON 文件daemon.json,添加以下内容:

{
    "dns": ["<your custom remote dns address here>", "8.8.8.8"]
}

重新启动 docker,并使用您的 VPN,这应该很好:)

请注意,这是一种黑客/解决方法,对每个人都不是一个好习惯。它的范围不是很好:影响您的整个 docker 范围,而不仅仅是您的应用程序之一。


推荐阅读