node.js - 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 访问的注册表的名称。
我尝试了什么:
- 重新启动vpn
- 更改 .npmrc
- 使用我公司的 DNS 在正确的服务下的 docker-compose.yml 文件中添加一个 dns 字段,这将知道
myCustomRegistry
. 我首先认为这可能来自 docker-compose dns 无法知道的事实myCustomRegistry
。我可能做错了,这仍然是一个可能的假设。 - 设置 npm 代理(阅读文档,但不知道该怎么做)
文件:
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
提前致谢
解决方案
最后,我决定使用一种解决方法。对于处理相同问题的任何人,这将在本地解决您的问题。请确保您正在使用的任何远程服务器也以这种方式配置。
据我了解,目前无法使用 docker-compose 在构建中使用自定义 DNS,它仅在运行中使用。因此,要传递自定义 DNS,您必须使用解决方法并将其添加到整个全局配置中。
转到/etc/docker
并编辑或创建一个 JSON 文件daemon.json
,添加以下内容:
{
"dns": ["<your custom remote dns address here>", "8.8.8.8"]
}
重新启动 docker,并使用您的 VPN,这应该很好:)
请注意,这是一种黑客/解决方法,对每个人都不是一个好习惯。它的范围不是很好:影响您的整个 docker 范围,而不仅仅是您的应用程序之一。
推荐阅读
- python - Odoo13 to_date 给出 False
- javascript - Angular - 使用哈希策略时如何获取 URL?
- php - How to get unlimited scroll in giphy api using jQuery & PHP?
- kotlin - Kotlin flatMapTo does not compile
- matlab - 如何计算图像模糊度的线性指数?
- c# - How can a dependency (registered in nested LifetimeScope) be resolved from autofac container
- python - 下载“主”模块时出现命令错误
- python-3.x - Python 中关于 Lambda 函数的求值顺序
- junit - JUnit 不使用 --select-directory 发现文件夹中的类
- javascript - Click on a day By Color