为什么预期的消息没有打印到控制台输出?

时间:2020-10-18 16:20:37

标签: javascript node.js docker alpine

Here我问了关于SIGTERM的问题,我得到了答案:Windows无法发出此信号。但是我为Linux遇到了同样的问题(我写了新的代码示例)...我在node:14.14.0-alpine3.10的基础上构建了Linux docker镜像:

我的Dockerfile

FROM node:14.14.0-alpine3.10
WORKDIR /opt/app/
COPY . .
RUN npm i
CMD npm run start

我的.dockerignore文件:

node_modules/

我的src/index.js file

// Build the image and start its container:
// docker build -t my-app .
// docker run -it --rm --name my-app my-app

const appArgs = require("minimist")(process.argv.slice(2));
const readline = require("readline").createInterface({
    input: process.stdin,
    output: process.stdout,
});
const signalName = "SIGTERM";

process.on(signalName, () => {
    console.log(`${signalName} happened.`); // I don't see this message. Why?
});

console.log("application args: %o", appArgs);

let name = null;
let age = null;
readline.question("Name: ", answer => {
    name = answer;
    readline.question("Age: ", answer => {
        age = answer;
        readline.close();
        printHello();
    });
});

function printHello() {
    console.log(`Hello, ${name} (${age} age)`);
    process.kill(process.pid, signalName);
}

我的package.json文件:

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "start": "node src/index.js -abc --def --ghj 'Bob' -- 11 22 33"
  },
  "keywords": [],
  "author": "Andrey Bushman",
  "license": "ISC",
  "dependencies": {
    "minimist": "^1.2.5"
  }
}

我构建了基于Linux的映像并在Windows上运行其容器:

docker build -t my-app .
docker run -it --rm --name my-app my-app

控制台输出:

PS C:\Users\bushm\source\repos\node_sandbox\app> docker run -it --rm --name my-app my-app

> app@1.0.0 start /opt/app
> node src/index.js -abc --def --ghj 'Bob' -- 11 22 33

application args: {
  _: [ '11', '22', '33', [length]: 3 ],
  a: true,
  b: true,
  c: true,
  def: true,
  ghj: 'Bob'
}
Name: Bob
Age: 30
Hello, Bob (30 age)
PS C:\Users\bushm\source\repos\node_sandbox\app>

我的程序是由Linux(而不是Windows)启动的。为什么“ SIGTERM发生了。” 字符串此时未出现在控制台输出中?

0 个答案:

没有答案