我正在尝试在Docker中运行我的项目,并将所有依赖项放在其中,但是我卡在了grunt依赖项上。出于某种原因,grunt会失败并显示一个错误,指出它找不到本地的grunt。
我创建了一个示例来重现这个问题:
.
├── code
│ ├── bower.json
│ ├── Gruntfile.js
│ └── package.json
├── docker-compose.yml
└── frontend.dockerfile
docker-compose.yml:
version: "2"
services:
frontend:
build:
context: .
dockerfile: frontend.dockerfile
ports:
- "8585:8000"
volumes:
- ./code:/srv/frontend
command: grunt
frontend.dockerfile:
FROM node:wheezy
ADD code /srv/frontend
WORKDIR /srv/frontend
RUN npm install -g grunt-cli bower
RUN npm install
RUN groupadd -r usergroup && useradd -m -r -g usergroup user
RUN chown -R user:usergroup /srv/frontend
USER user
RUN bower install
bower.json:
{
"name": "code",
"description": "",
"main": "index.js",
"authors": [
"Mr. No One"
],
"license": "ISC",
"homepage": "",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"angular": "^1.5.8"
}
}
Gruntfile.json:
module.exports = function(grunt) {
grunt.initConfig({});
// tasks
grunt.registerTask('default', []);
};
package.json:
{
"name": "code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"grunt": "^1.0.1"
}
}
$ docker-compose up
... 安装所有依赖项 ...
安装完成后,当尝试使用我在docker-compose.yml
文件中指定的grunt
命令运行时,会出现以下错误:
frontend_1 | grunt-cli: The grunt command line interface (v1.2.0)
frontend_1 |
frontend_1 | Fatal error: Unable to find local grunt.
frontend_1 |
frontend_1 | If you're seeing this message, grunt hasn't been installed locally to
frontend_1 | your project. For more information about installing and configuring grunt,
frontend_1 | please see the Getting Started guide:
frontend_1 |
frontend_1 | http://gruntjs.com/getting-started
package.json
实际上包含grunt
作为一个依赖项,因此应在RUN npm install
之后安装。
node_modules
文件夹是/usr/local/lib/node_modules
,项目文件夹中实际上缺少node_modules
和bower_components
。NODE_ENV
未设置。 - psycatpackage.json
是否存在于WORKDIR
中? - Alexander Zeitlercwd
来查看 grunt 在尝试运行的位置。 - Alexander ZeitlerWORKDIR
内运行npm install
会发生什么? - Alexander Zeitler