\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}]}}