简体   繁体   English

Dockerized Node JS 项目得到:“command failed signal SIGTERM”错误

[英]Dockerized Node JS project getting: “command failed signal SIGTERM” error

I had a express application and I used this application in my Kubernetes cluster.我有一个快速应用程序,我在我的 Kubernetes 集群中使用了这个应用程序。

This application is auth service for my micro service architecture study.这个应用程序是我的微服务架构研究的身份验证服务。

I use Skaffold dev command for applying Kubernetes for this app.我使用 Skaffold dev 命令为这个应用程序应用 Kubernetes。

My Dockerfile is like that:我的 Dockerfile 是这样的:

FROM node:alpine

WORKDIR /app
COPY package.json .
RUN npm install

COPY . .

CMD ["npm", "start"]

And I run it with "Scaffold dev" command我用“Scaffold dev”命令运行它

And I getting error like that:我得到这样的错误:

...
...    
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

And my package.json file:还有我的 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"
  }
}

How can I solve this error?我该如何解决这个错误?

I assume you're either incorrectly specifying your script in the package.json or your script is not server.js .我假设您在package.json中错误地指定了您的脚本,或者您的脚本不是server.js

A minimal repro of your question works:您的问题的最小复制工作:

Using the Node.JS Getting Started guide's example with one minor tweak: https://nodejs.org/en/docs/guides/getting-started-guide/使用 Node.JS 入门指南的示例并稍作调整: https://nodejs.org/en/docs/guides/getting-started-guide/

NOTE Change const hostname = '127.0.0.1';注意更改const hostname = '127.0.0.1'; to const hostname = '0.0.0.0';const hostname = '0.0.0.0'; This is necessary to access the containerized app from the host.这是从主机访问容器化应用程序所必需的。

Adding a package.json because you have one and to show npm start :添加 package.json 因为您有一个并显示npm start

package.json : package.json

{
    "name": "66281738",
    "version": "0.0.1",
    "scripts": {
        "start": "node app.js"
    }
}

NOTE I believe npm start defaults to "start": "node server.js"注意我相信npm start默认为"start": "node server.js"

Using your Dockerfile and:使用您的Dockerfile和:

QUESTION="66281738"
docker build --tag=${QUESTION} --file=./Dockerfile .
docker run --interactive --tty --publish=7777:3000 ${QUESTION}

Yields:产量:

> 66281738@0.0.1 start
> node app.js

Server running at http://0.0.0.0:3000/

NOTE docker run binds the container's :3000 port to the host's :7777 just to show these need not be the same.注意docker run将容器的:3000端口绑定到主机的:7777只是为了表明它们不必相同。

Then:然后:

curl --request GET http://localhost:3000/

Yields:产量:

Hello World

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

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