{"id":170003,"date":"2020-01-24T08:02:48","date_gmt":"2020-01-24T00:02:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=170003"},"modified":"2020-01-16T10:43:16","modified_gmt":"2020-01-16T02:43:16","slug":"docker-captures-signal","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/docker-captures-signal.html","title":{"rendered":"docker\u5bb9\u5668\u4e2d\u4fe1\u53f7\u6355\u83b7\u7684\u57fa\u672c\u77e5\u8bc6"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6211\u4eec\u53ef\u80fd\u90fd\u4f7f\u7528\u8fc7 docker stop \u547d\u4ee4\u6765\u505c\u6b62\u6b63\u5728\u8fd0\u884c\u7684\u5bb9\u5668\uff0c\u6709\u65f6\u53ef\u80fd\u4f1a\u4f7f\u7528 docker kill \u547d\u4ee4\u5f3a\u884c\u5173\u95ed\u5bb9\u5668\u6216\u8005\u628a\u67d0\u4e2a\u4fe1\u53f7\u4f20\u9012\u7ed9\u5bb9\u5668\u4e2d\u7684\u8fdb\u7a0b\u3002\u8fd9\u4e9b\u64cd\u4f5c\u7684\u672c\u8d28\u90fd\u662f\u901a\u8fc7\u4ece\u4e3b\u673a\u5411\u5bb9\u5668\u53d1\u9001\u4fe1\u53f7\u5b9e\u73b0\u4e3b\u673a\u4e0e\u5bb9\u5668\u4e2d\u7a0b\u5e8f\u7684\u4ea4\u4e92\u3002\u6bd4\u5982\u6211\u4eec\u53ef\u4ee5\u5411\u5bb9\u5668\u4e2d\u7684\u5e94\u7528\u53d1\u9001\u4e00\u4e2a\u91cd\u65b0\u52a0\u8f7d\u4fe1\u53f7\uff0c\u5bb9\u5668\u4e2d\u7684\u5e94\u7528\u7a0b\u5e8f\u5728\u63a5\u5230\u4fe1\u53f7\u540e\u6267\u884c\u76f8\u5e94\u7684\u5904\u7406\u7a0b\u5e8f\u5b8c\u6210\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u7684\u4efb\u52a1\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728 docker \u5bb9\u5668\u4e2d\u6355\u83b7\u4fe1\u53f7\u7684\u57fa\u672c\u77e5\u8bc6\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4fe1\u53f7(linux)<\/strong><\/div>\n

\u4fe1\u53f7\u662f\u4e00\u79cd\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u5f62\u5f0f\u3002\u4e00\u4e2a\u4fe1\u53f7\u5c31\u662f\u5185\u6838\u53d1\u9001\u7ed9\u8fdb\u7a0b\u7684\u4e00\u4e2a\u6d88\u606f\uff0c\u544a\u8bc9\u8fdb\u7a0b\u53d1\u751f\u4e86\u67d0\u79cd\u4e8b\u4ef6\u3002\u5f53\u4e00\u4e2a\u4fe1\u53f7\u88ab\u53d1\u9001\u7ed9\u4e00\u4e2a\u8fdb\u7a0b\u540e\uff0c\u8fdb\u7a0b\u4f1a\u7acb\u5373\u4e2d\u65ad\u5f53\u524d\u7684\u6267\u884c\u6d41\u5e76\u5f00\u59cb\u6267\u884c\u4fe1\u53f7\u7684\u5904\u7406\u7a0b\u5e8f\u3002\u5982\u679c\u6ca1\u6709\u4e3a\u8fd9\u4e2a\u4fe1\u53f7\u6307\u5b9a\u5904\u7406\u7a0b\u5e8f\uff0c\u5c31\u6267\u884c\u9ed8\u8ba4\u7684\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n

\u8fdb\u7a0b\u9700\u8981\u4e3a\u81ea\u5df1\u611f\u5174\u8da3\u7684\u4fe1\u53f7\u6ce8\u518c\u5904\u7406\u7a0b\u5e8f\uff0c\u6bd4\u5982\u4e3a\u4e86\u80fd\u8ba9\u7a0b\u5e8f\u4f18\u96c5\u7684\u9000\u51fa(\u63a5\u5230\u9000\u51fa\u7684\u8bf7\u6c42\u540e\u80fd\u591f\u5bf9\u8d44\u6e90\u8fdb\u884c\u6e05\u7406)\u4e00\u822c\u7a0b\u5e8f\u90fd\u4f1a\u5904\u7406 SIGTERM \u4fe1\u53f7\u3002\u4e0e SIGTERM \u4fe1\u53f7\u4e0d\u540c\uff0cSIGKILL \u4fe1\u53f7\u4f1a\u7c97\u66b4\u7684\u7ed3\u675f\u4e00\u4e2a\u8fdb\u7a0b\u3002\u56e0\u6b64\u6211\u4eec\u7684\u5e94\u7528\u5e94\u8be5\u5b9e\u73b0\u8fd9\u6837\u7684\u76ee\u5f55\uff1a\u6355\u83b7\u5e76\u5904\u7406 SIGTERM \u4fe1\u53f7\uff0c\u4ece\u800c\u4f18\u96c5\u7684\u9000\u51fa\u7a0b\u5e8f\u3002\u5982\u679c\u6211\u4eec\u5931\u8d25\u4e86\uff0c\u7528\u6237\u5c31\u53ea\u80fd\u901a\u8fc7 SIGKILL \u4fe1\u53f7\u8fd9\u4e00\u7ec8\u6781\u624b\u6bb5\u4e86\u3002\u9664\u4e86 SIGTERM \u548c SIGKILL \uff0c\u8fd8\u6709\u50cf SIGUSR1 \u8fd9\u6837\u7684\u4e13\u95e8\u652f\u6301\u7528\u6237\u81ea\u5b9a\u4e49\u884c\u4e3a\u7684\u4fe1\u53f7\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u7b80\u5355\u7684\u8bf4\u660e\u5728 nodejs \u4e2d\u5982\u4f55\u4e3a\u4e00\u4e2a\u4fe1\u53f7\u6ce8\u518c\u5904\u7406\u7a0b\u5e8f\uff1a<\/p>\n

process.on('SIGTERM', function() {\r\n  console.log('shutting down...');\r\n});<\/pre>\n
\u5bb9\u5668\u4e2d\u7684\u4fe1\u53f7<\/strong><\/div>\n

Docker \u7684 stop \u548c kill \u547d\u4ee4\u90fd\u662f\u7528\u6765\u5411\u5bb9\u5668\u53d1\u9001\u4fe1\u53f7\u7684\u3002\u6ce8\u610f\uff0c\u53ea\u6709\u5bb9\u5668\u4e2d\u7684 1 \u53f7\u8fdb\u7a0b\u80fd\u591f\u6536\u5230\u4fe1\u53f7\uff0c\u8fd9\u4e00\u70b9\u975e\u5e38\u5173\u952e\uff01
\nstop \u547d\u4ee4\u4f1a\u9996\u5148\u53d1\u9001 SIGTERM \u4fe1\u53f7\uff0c\u5e76\u7b49\u5f85\u5e94\u7528\u4f18\u96c5\u7684\u7ed3\u675f\u3002\u5982\u679c\u53d1\u73b0\u5e94\u7528\u6ca1\u6709\u7ed3\u675f(\u7528\u6237\u53ef\u4ee5\u6307\u5b9a\u7b49\u5f85\u7684\u65f6\u95f4)\uff0c\u5c31\u518d\u53d1\u9001\u4e00\u4e2a SIGKILL \u4fe1\u53f7\u5f3a\u884c\u7ed3\u675f\u7a0b\u5e8f\u3002
\nkill \u547d\u4ee4\u9ed8\u8ba4\u53d1\u9001\u7684\u662f SIGKILL \u4fe1\u53f7\uff0c\u5f53\u7136\u4f60\u53ef\u4ee5\u901a\u8fc7 -s \u9009\u9879\u6307\u5b9a\u4efb\u4f55\u4fe1\u53f7\u3002<\/p>\n

\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a nodejs \u5e94\u7528\u6f14\u793a\u4fe1\u53f7\u5728\u5bb9\u5668\u4e2d\u7684\u5de5\u4f5c\u8fc7\u7a0b\u3002\u521b\u5efa app.js \u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

'use strict';\r\n\r\n \r\n\r\nvar http = require('http');\r\n\r\n \r\n\r\nvar server = http.createServer(function (req, res) {\r\n  res.writeHead(200, {'Content-Type': 'text\/plain'});\r\n  res.end('Hello World\\n');\r\n}).listen(3000, '0.0.0.0');\r\n\r\n \r\n\r\nconsole.log('server started');\r\n\r\n \r\n\r\nvar signals = {\r\n  'SIGINT': 2,\r\n  'SIGTERM': 15\r\n};\r\n\r\n \r\n\r\nfunction shutdown(signal, value) {\r\n  server.close(function () {\r\n    console.log('server stopped by ' + signal);\r\n    process.exit(128 + value);\r\n  });\r\n}\r\n\r\n \r\n\r\nObject.keys(signals).forEach(function (signal) {\r\n  process.on(signal, function () {\r\n    shutdown(signal, signals[signal]);\r\n  });\r\n});<\/pre>\n

\u8fd9\u4e2a\u5e94\u7528\u662f\u4e00\u4e2a http \u670d\u52a1\u5668\uff0c\u76d1\u542c\u7aef\u53e3 3000\uff0c\u4e3a SIGINT \u548c SIGTERM \u4fe1\u53f7\u6ce8\u518c\u4e86\u5904\u7406\u7a0b\u5e8f\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u4ecb\u7ecd\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5728\u5bb9\u5668\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u65f6\u4fe1\u53f7\u7684\u5904\u7406\u60c5\u51b5\u3002<\/p>\n

\u5e94\u7528\u7a0b\u5e8f\u4f5c\u4e3a\u5bb9\u5668\u4e2d\u7684 1 \u53f7\u8fdb\u7a0b<\/strong><\/div>\n

\u521b\u5efa Dockerfile \u6587\u4ef6\uff0c\u628a\u4e0a\u9762\u7684\u5e94\u7528\u6253\u5305\u5230\u955c\u50cf\u4e2d\uff1a<\/p>\n

FROM iojs:onbuild\r\nCOPY .\/app.js .\/app.js\r\nCOPY .\/package.json .\/package.json\r\nEXPOSE 3000\r\nENTRYPOINT [\"node\", \"app\"]<\/pre>\n

\u8bf7\u6ce8\u610f ENTRYPOINT \u6307\u4ee4\u7684\u5199\u6cd5\uff0c\u8fd9\u79cd\u5199\u6cd5\u4f1a\u8ba9 node \u5728\u5bb9\u5668\u4e2d\u4ee5 1 \u53f7\u8fdb\u7a0b\u7684\u8eab\u4efd\u8fd0\u884c\u3002<\/p>\n

\u63a5\u4e0b\u6765\u521b\u5efa\u955c\u50cf\uff1a<\/p>\n

$ docker build --no-cache -t signal-app -f Dockerfile .<\/pre>\n

\u7136\u540e\u542f\u52a8\u5bb9\u5668\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n

$ docker run -it --rm -p 3000:3000 --name=\"my-app\" signal-app<\/pre>\n

\u6b64\u65f6 node \u5e94\u7528\u5728\u5bb9\u5668\u4e2d\u7684\u8fdb\u7a0b\u53f7\u4e3a 1\uff1a<\/p>\n

\"\"<\/p>\n

\u73b0\u5728\u6211\u4eec\u8ba9\u7a0b\u5e8f\u9000\u51fa\uff0c\u6267\u884c\u547d\u4ee4\uff1a<\/p>\n

$ docker container kill --signal=\"SIGTERM\" my-app<\/p>\n

\u6b64\u65f6\u5e94\u7528\u4f1a\u4ee5\u6211\u4eec\u671f\u671b\u7684\u65b9\u5f0f\u9000\u51fa\uff1a<\/p>\n

\"\"<\/p>\n

\u5e94\u7528\u7a0b\u5e8f\u4e0d\u662f\u5bb9\u5668\u4e2d\u7684 1 \u53f7\u8fdb\u7a0b<\/strong><\/div>\n

\u521b\u5efa\u4e00\u4e2a\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u7684\u811a\u672c\u6587\u4ef6 app1.sh\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

#!\/usr\/bin\/env bash\r\nnode app<\/pre>\n

\u7136\u540e\u521b\u5efa Dockerfile1 \u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

FROM iojs:onbuild\r\nCOPY .\/app.js .\/app.js\r\nCOPY .\/app1.sh .\/app1.sh\r\nCOPY .\/package.json .\/package.json\r\nRUN chmod +x .\/app1.sh\r\nEXPOSE 3000\r\nENTRYPOINT [\".\/app1.sh\"]<\/pre>\n

\u63a5\u4e0b\u6765\u521b\u5efa\u955c\u50cf\uff1a<\/p>\n

$ docker build --no-cache -t signal-app1 -f Dockerfile1 .<\/pre>\n

\u7136\u540e\u542f\u52a8\u5bb9\u5668\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n

$ docker run -it --rm -p 3000:3000 --name=\"my-app1\" signal-app1<\/pre>\n

\u6b64\u65f6 node \u5e94\u7528\u5728\u5bb9\u5668\u4e2d\u7684\u8fdb\u7a0b\u53f7\u4e0d\u518d\u662f 1\uff1a<\/p>\n

\"\"<\/p>\n

\u73b0\u5728\u7ed9 my-app1 \u53d1\u9001 SIGTERM \u4fe1\u53f7\u8bd5\u8bd5\uff0c\u5df2\u7ecf\u65e0\u6cd5\u9000\u51fa\u7a0b\u5e8f\u4e86\uff01\u5728\u8fd9\u4e2a\u573a\u666f\u4e2d\uff0c\u5e94\u7528\u7a0b\u5e8f\u7531 bash \u811a\u672c\u542f\u52a8\uff0cbash \u4f5c\u4e3a\u5bb9\u5668\u4e2d\u7684 1 \u53f7\u8fdb\u7a0b\u6536\u5230\u4e86 SIGTERM \u4fe1\u53f7\uff0c\u4f46\u662f\u5b83\u6ca1\u6709\u505a\u51fa\u4efb\u4f55\u7684\u54cd\u5e94\u52a8\u4f5c\u3002
\n\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\uff1a<\/p>\n

$ docker container stop my-app1\r\n# or\r\n$ docker container kill --signal=\"SIGKILL\" my-app1<\/pre>\n

\u9000\u51fa\u5e94\u7528\uff0c\u5b83\u4eec\u6700\u7ec8\u90fd\u662f\u5411\u5bb9\u5668\u4e2d\u7684 1 \u53f7\u8fdb\u7a0b\u53d1\u9001\u4e86 SIGKILL \u4fe1\u53f7\u3002\u5f88\u663e\u7136\u8fd9\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\uff0c\u6211\u4eec\u5e0c\u671b\u7a0b\u5e8f\u80fd\u591f\u6536\u5230 SIGTERM \u4fe1\u53f7\u4f18\u96c5\u7684\u9000\u51fa\u3002<\/p>\n

\u5728\u811a\u672c\u4e2d\u6355\u83b7\u4fe1\u53f7<\/strong><\/div>\n

\u521b\u5efa\u53e6\u5916\u4e00\u4e2a\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u7684\u811a\u672c\u6587\u4ef6 app2.sh\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

#!\/usr\/bin\/env bash\r\nset -x\r\npid=0\r\n\r\n# SIGUSR1-handler\r\nmy_handler() {\r\n  echo \"my_handler\"\r\n}\r\n\r\n# SIGTERM-handler\r\nterm_handler() {\r\n  if [ $pid -ne 0 ]; then\r\n    kill -SIGTERM \"$pid\"\r\n    wait \"$pid\"\r\n  fi\r\n  exit 143; # 128 + 15 -- SIGTERM\r\n}\r\n# setup handlers\r\n# on callback, kill the last background process, which is `tail -f \/dev\/null` and execute the specified handler\r\ntrap 'kill ${!}; my_handler' SIGUSR1\r\ntrap 'kill ${!}; term_handler' SIGTERM\r\n\r\n# run application\r\nnode app &\r\npid=\"$!\"\r\n\r\n# wait forever\r\nwhile true\r\ndo\r\n  tail -f \/dev\/null & wait ${!}\r\ndone\r\n<\/pre>\n

\u8fd9\u4e2a\u811a\u672c\u6587\u4ef6\u5728\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u7684\u540c\u65f6\u53ef\u4ee5\u6355\u83b7\u53d1\u9001\u7ed9\u5b83\u7684 SIGTERM \u548c SIGUSR1 \u4fe1\u53f7\uff0c\u5e76\u4e3a\u5b83\u4eec\u6dfb\u52a0\u4e86\u5904\u7406\u7a0b\u5e8f\u3002\u5176\u4e2d SIGTERM \u4fe1\u53f7\u7684\u5904\u7406\u7a0b\u5e8f\u5c31\u662f\u5411\u6211\u4eec\u7684 node \u5e94\u7528\u7a0b\u5e8f\u53d1\u9001 SIGTERM \u4fe1\u53f7\u3002<\/p>\n

\u7136\u540e\u521b\u5efa Dockerfile2 \u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

\r\nFROM iojs:onbuild\r\nCOPY .\/app.js .\/app.js\r\nCOPY .\/app2.sh .\/app2.sh\r\nCOPY .\/package.json .\/package.json\r\nRUN chmod +x .\/app2.sh\r\nEXPOSE 3000\r\nENTRYPOINT [\".\/app2.sh\"]<\/pre>\n

\u63a5\u4e0b\u6765\u521b\u5efa\u955c\u50cf\uff1a<\/p>\n

$ docker build --no-cache -t signal-app2 -f Dockerfile2 .<\/pre>\n

\u7136\u540e\u542f\u52a8\u5bb9\u5668\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n

$ docker run -it --rm -p 3000:3000 --name=\"my-app2\" signal-app2<\/pre>\n

\u6b64\u65f6 node \u5e94\u7528\u5728\u5bb9\u5668\u4e2d\u7684\u8fdb\u7a0b\u53f7\u4e5f\u4e0d\u662f 1\uff0c\u4f46\u662f\u5b83\u5374\u53ef\u4ee5\u63a5\u6536\u5230 SIGTERM \u4fe1\u53f7\u5e76\u4f18\u96c5\u7684\u9000\u51fa\u4e86\uff1a<\/p>\n

\"\"<\/p>\n

\u7ed3\u8bba<\/strong><\/div>\n

\u5bb9\u5668\u4e2d\u7684 1 \u53f7\u8fdb\u7a0b\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5982\u679c\u5b83\u4e0d\u80fd\u6b63\u786e\u7684\u5904\u7406\u76f8\u5173\u7684\u4fe1\u53f7\ufffd\ufffd\u90a3\u4e48\u5e94\u7528\u7a0b\u5e8f\u9000\u51fa\u7684\u65b9\u5f0f\u51e0\u4e4e\u603b\u662f\u88ab\u5f3a\u5236\u6740\u6b7b\u800c\u4e0d\u662f\u4f18\u96c5\u7684\u9000\u51fa\u3002\u7a76\u7adf\u8c01\u662f 1 \u53f7\u8fdb\u7a0b\u5219\u4e3b\u8981\u7531 EntryPoint, CMD, RUN \u7b49\u6307\u4ee4\u7684\u5199\u6cd5\u51b3\u5b9a\uff0c\u6240\u4ee5\u8fd9\u4e9b\u6307\u4ee4\u7684\u4f7f\u7528\u662f\u5f88\u6709\u8bb2\u7a76\u7684\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"

\u4fe1\u53f7\u662f\u4e00\u79cd\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u5f62\u5f0f\u3002\u4e00\u4e2a\u4fe1\u53f7\u5c31\u662f\u5185\u6838\u53d1\u9001\u7ed9\u8fdb\u7a0b\u7684\u4e00\u4e2a\u6d88\u606f\uff0c\u544a\u8bc9\u8fdb\u7a0b\u53d1\u751f\u4e86\u67d0\u79cd\u4e8b\u4ef6\u3002\u5f53\u4e00\u4e2a\u4fe1\u53f7\u88ab\u53d1\u9001\u7ed9\u4e00 […]<\/p>\n","protected":false},"author":310,"featured_media":170013,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-170003","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/170003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/310"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=170003"}],"version-history":[{"count":10,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/170003\/revisions"}],"predecessor-version":[{"id":170561,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/170003\/revisions\/170561"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/170013"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=170003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=170003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=170003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}