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
- 2 Status code 137 usually means that the process was killed by a SIGKILL signal. – Hans Kilian Commented Mar 8 at 17:32
1 Answer
Reset to default 0The 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
版权声明:本文标题:node.js - Docker Node application "app hook exited with status 137" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744892949a2630892.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论