正在使用docker构建react应用程序,但在运行npm安装时出错



我正在用docker构建一个react应用程序,但在RUN npm安装时出错。我添加了ENV PATH和ENV CI,但问题还没有解决。

这是我的docker文件:

FROM node:alpine
ENV PATH /app/node_modules/.bin:$PATH
ENV CI=true
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
RUN npm install
COPY ./ ./
CMD ["npm", "start"]

Docker命令:docker build -t dockeruser/client .

终端响应:

[+] Building 213.3s (9/10)
=> [internal] load build definition from Dockerfile                                                                                                         0.0s 
=> => transferring dockerfile: 225B                                                                                                                         0.0s 
=> [internal] load .dockerignore                                                                                                                            0.0s 
=> => transferring context: 34B                                                                                                                             0.0s 
=> [internal] load metadata for docker.io/library/node:alpine                                                                                               0.0s 
=> [1/6] FROM docker.io/library/node:alpine                                                                                                                 0.0s 
=> [internal] load build context                                                                                                                            0.0s 
=> => transferring context: 957B                                                                                                                            0.0s 
=> CACHED [2/6] WORKDIR /app                                                                                                                                0.0s 
=> CACHED [3/6] COPY package.json ./                                                                                                                        0.0s 
=> CACHED [4/6] COPY package-lock.json ./                                                                                                                   0.0s 
=> ERROR [5/6] RUN npm install                                                                                                                            213.2s 
------
> [5/6] RUN npm install:
#9 213.1 npm ERR! code ECONNRESET
#9 213.1 npm ERR! network aborted
#9 213.1 npm ERR! network This is a problem related to network connectivity.
#9 213.1 npm ERR! network In most cases you are behind a proxy or have bad network settings.
#9 213.1 npm ERR! network
#9 213.1 npm ERR! network If you are behind a proxy, please make sure that the
#9 213.1 npm ERR! network 'proxy' config is set properly.  See: 'npm help config'
#9 213.1
#9 213.1 npm ERR! A complete log of this run can be found in:
#9 213.1 npm ERR!     /root/.npm/_logs/2021-07-07T07_40_20_529Z-debug.log
------
executor failed running [/bin/sh -c npm install]: exit code: 1

我遇到了完全相同的问题。我所做的是增加超时
npm install --fetch-timeout=600000它奏效了。

显然,您的网络并不坏,但应用程序位于代理之后,如错误日志中所示。我能想到两个潜在的解决方案。

首先,您可以使用设置代理配置

npm config set proxy http://proxy_host:port
npm config set https-proxy https://proxy.company.com:8080

或者第二,你可以用清除你的代理

npm config rm proxy
npm config rm https-proxy
set HTTP_PROXY=null
set HTTPS_PROXY=null

然后你可以运行

npm config list

查看是否还有任何代理配置。

当然,我假设你知道你的代理地址是什么

最新更新