admin管理员组

文章数量:1405389

I am trying to run a node application on a docker compose setup like this:

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: nodejs
    container_name: nodejs
    restart: always
    working_dir: /app
    volumes:
      - ./app:/app
    ports:
      - "3000:3000"
    post_start:
        - command: "node app.js"

Dockerfile

FROM node

WORKDIR /app

EXPOSE 3000

docker compose up gives error:

Attaching to nodejs
app hook exited with status 137

app.js

const express = require("express");
const app = express();
const port = 3000;

app.get("/test", (req, res) => {
  res.json({
    success: true,
  });
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

I have done npm install and all files required are in the app folder

I am trying to run a node application on a docker compose setup like this:

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: nodejs
    container_name: nodejs
    restart: always
    working_dir: /app
    volumes:
      - ./app:/app
    ports:
      - "3000:3000"
    post_start:
        - command: "node app.js"

Dockerfile

FROM node

WORKDIR /app

EXPOSE 3000

docker compose up gives error:

Attaching to nodejs
app hook exited with status 137

app.js

const express = require("express");
const app = express();
const port = 3000;

app.get("/test", (req, res) => {
  res.json({
    success: true,
  });
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

I have done npm install and all files required are in the app folder

Share Improve this question asked Mar 8 at 15:21 SamSam 1,8684 gold badges33 silver badges61 bronze badges 1
  • 2 Status code 137 usually means that the process was killed by a SIGKILL signal. – Hans Kilian Commented Mar 8 at 17:32
Add a comment  | 

1 Answer 1

Reset to default 0

The first problem here is that you haven't told Docker what the main container command should actually be. You're using Compose post_start:, a fairly new feature that launches an auxiliary command after the container is already running, but you're not telling Docker anywhere what the main container command should be. You'd usually do this using a Dockerfile CMD directive:

CMD ["node", "app.js"]

You'll also run into potential issues because you're injecting all of the code at run time. This reintroduces the "works on my machine" problem that Docker usually tries to avoid. If your host environment is different from the container environment (maybe you're running Linux containers on a Windows or MacOS host) then there can be incompatibilities in the node_modules directory. A usual best practice is to install the code in the image so that it's self-contained, and avoid overwriting that code with volumes:.

A typical minimal Node Dockerfile might look like

FROM node
WORKDIR /app
COPY package.json package-lock.json .
RUN npm ci
COPY ./ ./
EXPOSE 3000
CMD ["node", "app.js"]

With this setup, you can significantly simplify the Compose file. You don't need to manually specify the container_name:, restate the image's working_dir:, inject code with volumes:, or manually specify the image: name for the image you're building, plus there's a shorter form of build: you can use. I might cut this down to just

version: '3.8'  # harmless, compatible with older versions of Compose
services:
  app:
    build: .
    restart: always
    ports:
      - "3000:3000"

本文标签: nodejsDocker Node application quotapp hook exited with status 137quotStack Overflow