2016-08-26 49 views
3

我试图运行我的项目与泊坞内的所有依赖关系,但我被卡住了咕噜依赖,出于某种原因咕噜失败,它可以发生错误找不到当地的咕噜声。错误在Docker中运行grunt:致命错误:无法找到本地grunt

我创建的如何重现这样的一个例子:

. 
├── code 
│   ├── bower.json 
│   ├── Gruntfile.js 
│   └── package.json 
├── docker-compose.yml 
└── frontend.dockerfile 

搬运工-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 ... installing all dependencies ... 安装后,它在尝试运行失败我指定的grunt命令指明分数在我docker-compose.yml文件与此错误:

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后进行安装。

回答

3

做一个docker exec -it <nameofyourcontainer> bash,并查看node_modules如果grunt本地安装。

您是否在某处设置了NODE_ENVproduction? 这会导致npm未安装devDepedencies

+0

貌似唯一node_modules文件夹我是'''在/ usr /本地/ lib/node_modules''',node_modules和bower_components实际上是从项目文件夹中丢失的。 'NODE_ENV'''没有设置。 – vladimirze

+0

你的'package.json'是否存在于'WORKDIR'中? –

+0

同时在你的'command'里放一个'cwd'来看看它在哪里试图运行grunt。 –

0

我想我找到了原因为什么node_modulesbower_components没有创建in the docs

Note: If any build steps change the data within the volume after it has been declared, those changes will be discarded.

虽然我没有做我的dockerfile宣布VOLUME,我有volumesdocker-compose.yml所以我怀疑这说明是影响我,因为这两个npm install & bower install构建步骤做的体积内触摸数据。

我删除那些构筑起dockerfile步骤,并没有他们后手动构建完成:

$ docker-compose build 
$ docker-compose run --rm frontend npm install 
$ docker-compose run --rm frontend bower install 

不过,我运行这些命令时上面,因为我的新创建的用户没有权限遇到权限问题写入主机,我不想以容器内的root身份运行(如果没有--allow-root,那么bower不会喜欢它)

解决方案是使用与主机相同的UID和GID创建用户。

我发现,泊坞窗允许variable substitutionbuild arguments这意味着你不必硬编码的UID和GID内docker-compose.yml,相反,他们可以从主机ENV变量取像这样,可以在生成过程中进行访问。

$ export HOST_UID=$(id -u) 
$ export HOST_GID=$(id -g) 

frontend.dockerfile

FROM node:wheezy 

ARG hostuid 
ARG hostgid 

ADD code /srv/frontend 
WORKDIR /srv/frontend 
RUN npm install -g grunt-cli bower 

RUN groupadd -g "$hostgid" devgroup && useradd -m -u "$hostuid" -g devgroup developer 
RUN chown -R developer:devgroup /srv/frontend 
USER developer 

泊坞窗,compose.yml

version: "2" 
services: 
    frontend: 
    build: 
     context: . 
     dockerfile: frontend.dockerfile 
     args: 
     - hostuid=${HOST_UID} 
     - hostgid=${HOST_GID} 
    ports: 
     - "8585:8000" 
    volumes: 
     - ./code:/srv/frontend 
    command: grunt