2016-08-26 73 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