Dockerized Node JS 项目出现:“命令失败信号 SIGTERM”错误

aka*_*saa 5 node.js npm express docker kubernetes

我有一个 Express 应用程序,并且在我的 Kubernetes 集群中使用了该应用程序。

这个应用程序是我的微服务架构研究的授权服务。

我使用 Skaffold dev 命令为此应用程序应用 Kubernetes。

我的 Dockerfile 是这样的:

FROM node:alpine

WORKDIR /app
COPY package.json .
RUN npm install

COPY . .

CMD ["npm", "start"]
Run Code Online (Sandbox Code Playgroud)

我用“Scaffold dev”命令运行它

我收到这样的错误:

...
...    
Deployments stabilized in 3.752 seconds
Watching for changes...
[auth] npm notice 
[auth] npm notice New patch version of npm available! 7.5.1 -> 7.5.4
[auth] npm notice Changelog: <https://github.com/npm/cli/releases/tag/v7.5.4>
[auth] npm notice Run `npm install -g npm@7.5.4` to update!
[auth] npm notice 
[auth] npm ERR! path /app
[auth] npm ERR! command failed
[auth] npm ERR! signal SIGTERM
[auth] npm ERR! command sh -c node server.js
[auth] 
[auth] npm ERR! A complete log of this run can be found in:
[auth] npm ERR!     /root/.npm/_logs/2021-02-19T16_46_28_956Z-debug.log
Run Code Online (Sandbox Code Playgroud)

还有我的 package.json 文件:

    {
  "name": "authservice",
  "version": "1.0.0",
  "description": "Auth Service",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "keywords": [
    "auth",
    "user"
  ],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.0",
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-rate-limit": "^5.2.3",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "5.10.19",
    "morgan": "^1.10.0",
    "multer": "^1.4.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.6"
  }
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?

Daz*_*kin 2

我假设您在 中错误地指定了您的脚本,package.json或者您的脚本不是 中server.js

您的问题的最小重现有效:

使用 Node.JS 入门指南的示例并进行一点小小的调整: https://nodejs.org/en/docs/guides/getting-started-guide/

注意更改const hostname = '127.0.0.1';const hostname = '0.0.0.0';这是从主机访问容器化应用程序所必需的。

添加 package.json 因为你有一个并显示npm start

package.json:

{
    "name": "66281738",
    "version": "0.0.1",
    "scripts": {
        "start": "node app.js"
    }
}
Run Code Online (Sandbox Code Playgroud)

注意我相信npm start默认为"start": "node server.js"

使用您的Dockerfile和:

{
    "name": "66281738",
    "version": "0.0.1",
    "scripts": {
        "start": "node app.js"
    }
}
Run Code Online (Sandbox Code Playgroud)

产量:

> 66281738@0.0.1 start
> node app.js

Server running at http://0.0.0.0:3000/
Run Code Online (Sandbox Code Playgroud)

注意 docker run将容器的:3000端口绑定到主机的端口:7777只是为了表明它们不必相同。

然后:

QUESTION="66281738"
docker build --tag=${QUESTION} --file=./Dockerfile .
docker run --interactive --tty --publish=7777:3000 ${QUESTION}
Run Code Online (Sandbox Code Playgroud)

产量:

Hello World
Run Code Online (Sandbox Code Playgroud)