Docker和Node - 使用docker-compose时获得“ELIFECYCLE”,使用`docker run`

时间:2018-04-18 06:44:11

标签: node.js docker npm docker-compose

这让我疯了几个小时。我想要一个docker-compose文件,它只是构建一个图像,创建一个容器,然后执行一个简单的脚本。

我尽可能简单地重新创建了这个问题

app.js

const fs = require("fs");
const util = require("util");
const sqlite3 = require("sqlite3");

console.log("DOING NOTHING");

的package.json

{
    "name": "example",
    "version": "0.1.0",
    "author": "shatnerz",
    "main": "app.js",
    "scripts": {
        "start": "node app.js"
    },
    "dependencies": {
        "sqlite3": "4.0.0"
    }
}

Dockerfile

FROM node:9.11.1-alpine

ADD . /home/example
WORKDIR /home/example
# RUN npm update -g
RUN npm install

CMD ["npm", "run", "start"]

搬运工-compose.yml

version: '3.6'

services:
  example:
    build: .
    image: shatnerz/example
    container_name: shatnerz-example
    volumes:
      - .:/home/example
    command: ["npm", "run", "start"]

当我尝试使用docker compose时,我得到了

$ docker-compose up --build
Building example-test
Step 1/5 : FROM node:9.11.1-alpine
 ---> 7af437a39ec2
Step 2/5 : ADD . /home/example
 ---> Using cache
 ---> cbb4467e0651
Step 3/5 : WORKDIR /home/example
 ---> Using cache
 ---> 377b15cc8eab
Step 4/5 : RUN npm install
 ---> Using cache
 ---> 03be15a4465b
Step 5/5 : CMD ["npm", "run", "start"]
 ---> Using cache
 ---> 286312488783

Successfully built 286312488783
Successfully tagged shatnerz/example:latest
Starting shatnerz-example ... done
Attaching to shatnerz-example
shatnerz-example | 
shatnerz-example | > example@0.1.0 start /home/example
shatnerz-example | > node app.js
shatnerz-example | 
shatnerz-example | internal/modules/cjs/loader.js:550
shatnerz-example |     throw err;
shatnerz-example |     ^
shatnerz-example | 
shatnerz-example | Error: Cannot find module 'sqlite3'
shatnerz-example |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:548:15)
shatnerz-example |     at Function.Module._load (internal/modules/cjs/loader.js:475:25)
shatnerz-example |     at Module.require (internal/modules/cjs/loader.js:598:17)
shatnerz-example |     at require (internal/modules/cjs/helpers.js:11:18)
shatnerz-example |     at Object.<anonymous> (/home/example/app.js:3:17)
shatnerz-example |     at Module._compile (internal/modules/cjs/loader.js:654:30)
shatnerz-example |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
shatnerz-example |     at Module.load (internal/modules/cjs/loader.js:566:32)
shatnerz-example |     at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
shatnerz-example |     at Function.Module._load (internal/modules/cjs/loader.js:498:3)
shatnerz-example | npm ERR! code ELIFECYCLE
shatnerz-example | npm ERR! errno 1
shatnerz-example | npm ERR! example@0.1.0 start: `node app.js`
shatnerz-example | npm ERR! Exit status 1
shatnerz-example | npm ERR! 
shatnerz-example | npm ERR! Failed at the example@0.1.0 start script.
shatnerz-example | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
shatnerz-example | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
shatnerz-example | 
shatnerz-example | npm ERR! A complete log of this run can be found in:
shatnerz-example | npm ERR!     /root/.npm/_logs/2018-04-18T06_39_32_565Z-debug.log
shatnerz-example exited with code 1

我尝试过不同版本的节点并使用ubuntu图像而不是alpine无效。我得到这个错误基本上是每个案例,但不知怎的docker run shatnerz/example按预期工作。

$ docker run shatnerz/example

> example@0.1.0 start /home/example
> node app.js

DOING NOTHING

令人难以置信的是令人沮丧。我一直盯着这个太久了。我不再进步了。我还将docker-compose和docker更新为最新版本。

我的node-modules*

中有.dockerignore

编辑:有趣的是,我在尝试从sublime运行app.js时遇到类似的错误,这导致认为它可能与stdin和stdout有关

2 个答案:

答案 0 :(得分:1)

尝试从docker-compose.yml

中删除以下行
volumes:
  - .:/home/example
command: ["npm", "run", "start"]

由于您已在docker-image中添加了这些参数和所需文件。

请让我知道 - 你的输出,以便我可以进一步帮助你。

答案 1 :(得分:0)

奇怪的是,我已经能够通过运行来解决相同的错误消息 sudo npm install直接在我的计算机上,然后是docker-compose downdocker-compose up --build