繁体   English   中英

Docker和Node-使用docker-compose时获得“ ELIFECYCLE”,可与docker run一起使用

[英]Docker and Node - Getting “ELIFECYCLE” when using docker-compose, works with `docker run`

这使我疯狂了几个小时。 我想要一个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更新到最新版本。

我的.dockerignorenode-modules*

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

尝试从docker-compose.yml中删除以下行

volumes:
  - .:/home/example
command: ["npm", "run", "start"]

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

请告诉我-您的输出,以便我进一步帮助您。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM