简体   繁体   中英

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

This has been driving me crazy for a few hours. I want a docker-compose file that will simply build an image, create a container, and then execute a simple script.

I have recreated the issue as simply as I could

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"]

docker-compose.yml

version: '3.6'

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

When I attempt to use docker compose I get

$ 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

I have tried different versions of node and using an ubuntu image instead of alpine to no avail. I get this error is basically every case, but somehow docker run shatnerz/example works as expected.

$ docker run shatnerz/example

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

DOING NOTHING

This is incredibly frustrating. I have been staring at this too long. I am no longer making progress. I also updated my docker-compose and docker to the latest versions.

I have node-modules* in my .dockerignore

edit: interesting, I get a similar error when trying to run app.js from sublime, which leads to to think it may have something to do with the stdin and stdout

try removing following lines from your docker-compose.yml

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

Since you already add these parameters and required files inside your docker-image.

Just let me know -your output so that I can help you further.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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