Docker镜像创build抛出“没有这样的文件或目录,访问'/ node_modules / sails'”当dockerizing一个sails.js应用程序

以下是Dockerfile

 FROM node:latest RUN npm install -g sails@0.12.13 ADD . / ./ RUN npm install EXPOSE 80 CMD (sails lift) 

使用以下日志创build映像失败:

 ending build context to Docker daemon 70.03MB Step 1/6 : FROM node:latest ---> 60bea5b86079 Step 2/6 : RUN npm install -g sails@0.12.13 ---> Using cache ---> 3f3c7fcdb090 Step 3/6 : ADD . / ./ ---> Using cache ---> 78700b41cf26 Step 4/6 : RUN (npm install) ---> Running in d49423611a77 npm info it worked if it ends with ok npm info using npm@5.3.0 npm info using node@v8.4.0 npm info lifecycle ecs-notification@0.0.0~preinstall: ecs-notification@0.0.0 npm WARN checkPermissions Missing write access to /node_modules/sails npm ERR! path /node_modules/sails npm ERR! code ENOENT npm ERR! errno -2 npm ERR! syscall access npm ERR! enoent ENOENT: no such file or directory, access '/node_modules/sails' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2017-09-08T13_00_06_956Z-debug.log The command '/bin/sh -c (npm install)' returned a non-zero code: 254 

即使我尝试创build目录,或使用以下命令将权限更改为777: RUN (mkdir -p /node_modules/sails; chmod 777 /node_modules/sails)

它仍然失败,同样的错误:

 Sending build context to Docker daemon 70.03MB Step 1/7 : FROM node:latest ---> 60bea5b86079 Step 2/7 : RUN npm install -g sails@0.12.13 ---> Using cache ---> 3f3c7fcdb090 Step 3/7 : RUN (mkdir -p /node_modules/sails; chmod 777 /node_modules/sails) ---> Using cache ---> c7f1784c24c8 Step 4/7 : ADD . / ./ ---> Using cache ---> 334017659dde Step 5/7 : RUN (npm install) ---> Running in 833e3ef6a010 npm info it worked if it ends with ok npm info using npm@5.3.0 npm info using node@v8.4.0 npm info lifecycle ecs-notification@0.0.0~preinstall: ecs-notification@0.0.0 npm WARN checkPermissions Missing write access to /node_modules/sails npm ERR! path /node_modules/sails npm ERR! code ENOENT npm ERR! errno -2 npm ERR! syscall access npm ERR! enoent ENOENT: no such file or directory, access '/node_modules/sails' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2017-09-08T13_13_55_258Z-debug.log The command '/bin/sh -c (npm install)' returned a non-zero code: 254 

Docker版本:17.06.2-ce-mac27(19124)

任何指针如何我可以debugging呢?

将您的Dockerfile更改为下面

 FROM node:latest RUN npm install -g sails@0.12.13 WORKDIR /usr/app COPY package.json ./ RUN npm install COPY . ./ EXPOSE 80 CMD sails lift 

你应该首先复制package.json ,然后执行npm install然后复制代码。 你的ADD语句也是错误的。 有一个额外的空间

接下来确保你有忽略node_modules的.dockerignore。 否则node_modules将被复制操作覆盖