用Dockerfile构建镜像后,npm总是无法安装

Dockerfile文件:

#制定node镜像的版本
FROM node:8.9-alpine

#声明作者
MAINTAINER test

#移动当前目录下面的文件到app目录下
ADD . /app/

#进入到app目录下面,类似cd
WORKDIR /app

#安装依赖
RUN npm install

#对外暴露的端口
EXPOSE 3000

#程序启动脚本
CMD ["npm", "start"]

执行镜像构建 docker build -t docker_demo . 命令后:

[root@cd56723212387 testnodejs]# docker build -t docker_demo .
Sending build context to Docker daemon 17.92 kB
Step 1/7 : FROM node:8.9-alpine
Trying to pull repository docker.io/library/node ... 
8.9-alpine: Pulling from docker.io/library/node
605ce1bd3f31: Pull complete 
79b85b1676b5: Pull complete 
20865485d0c2: Pull complete 
Digest: sha256:6bb963d58da845cf66a22bc5a48bb8c686f91d30240f0798feb0d61a2832fc46
Status: Downloaded newer image for docker.io/node:8.9-alpine
 ---> 406f227b21f5
Step 2/7 : MAINTAINER test
 ---> Running in 641a6df02500
 ---> c957a05f33c1
Removing intermediate container 641a6df02500
Step 3/7 : ADD . /app/
 ---> ba5d7c9c701d
Removing intermediate container 52c5456d5f6d
Step 4/7 : WORKDIR /app
 ---> 7fc86e6854d1
Removing intermediate container c2a6af1b18fd
Step 5/7 : RUN npm install
 ---> Running in c715c3318df3

**npm ERR! code EAI_AGAIN
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/express failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org:443
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-08-07T01_25_32_257Z-debug.log
The command '/bin/sh -c npm install' returned a non-zero code: 1**
[root@cd56723212387 testnodejs]# 

大伙给看看什么原因

阅读 26.7k
3 个回答
FROM node:10
MAINTAINER xialeistudio xialeistudio@gmail.com
WORKDIR /usr/src/app
ENV TZ Asia/Shanghai
ARG registry=https://registry.npm.taobao.org
ARG disturl=https://npm.taobao.org/dist
RUN yarn config set disturl $disturl
RUN yarn config set registry $registry
COPY package.json /usr/src/app/
RUN yarn --frozen-lockfile --production
COPY . /usr/src/app
EXPOSE 8080
CMD [ "yarn", "start:prod" ]

拿走

解决了走了不少弯路,

正确的解决办法是这样的:
dns改为阿里的DNS:223.5.5.5 或 223.6.6.6
然后重启service network restart即可

网络问题,访问不到npmjs.org站点

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏