{"id":1436,"date":"2024-04-16T15:57:04","date_gmt":"2024-04-16T07:57:04","guid":{"rendered":"https:\/\/www.yinyubo.com\/?p=1436"},"modified":"2024-04-16T15:57:04","modified_gmt":"2024-04-16T07:57:04","slug":"docker%e8%bf%90%e8%a1%8c%e7%9a%84postgresql%e4%bc%98%e9%9b%85%e5%85%b3%e9%97%ad","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=1436","title":{"rendered":"docker\u8fd0\u884c\u7684postgresql\u4f18\u96c5\u5173\u95ed"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<p>        \u6211\u4eec\u7684\u8bb8\u591a\u9879\u76ee\u90e8\u7f72\u5b8c\u4e4b\u540e\uff0c\u662f\u4f1a\u7ecf\u5e38\u5173\u95ed\u6216\u8005\u91cd\u542f\u670d\u52a1\u5668\u7684\uff0c\u6bd4\u5982\u5404\u79cd\u8fc7\u5e74\u8fc7\u8282\u7684\u65e5\u5b50\uff0c\u505c\u5de5\u7684\u65e5\u5b50\uff0c\u8fd9\u4e00\u5173\u673a\uff0c\u6570\u636e\u5e93\u5c31\u5bb9\u6613\u51fa\u95ee\u9898\u3002\u6211\u4eec\u7684postgresql\u662f\u8fd0\u884c\u5728\u6211\u81ea\u5b9a\u4e49\u7684\u4e00\u4e2adebian\u5bb9\u5668\u4e2d\uff0c\u56e0\u4e3a\u6dfb\u52a0\u4e86\u4e00\u4e9b\u6211\u81ea\u5b9a\u4e49\u7684\u5b9a\u65f6\u5907\u4efd\u548c\u65e5\u5fd7\u529f\u80fd\uff0c\u6240\u4ee5\u8fd0\u884c\u65b9\u5f0f\u548c\u5b98\u65b9\u955c\u50cf\u4e0d\u4e00\u81f4\u3002\u4e3a\u4e86\u51cf\u5c11\u6570\u636e\u5e93\u56e0\u4e3a\u5173\u673a\u800c\u9020\u6210\u51fa\u95ee\u9898\u7684\u60c5\u51b5\uff0c\u5f97\u60f3\u4e00\u4e2a\u4f18\u96c5\u5173\u95ed\u7684\u65b9\u6cd5(\u81f3\u5c11\u4e0d\u80fd\u539f\u5730\u7206\u70b8^_^)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e00\u4e2a\u8fdb\u884c\u4e0d\u4e0b\u53bb\u7684\u65b9\u6848(systemctl)<\/h2>\n\n\n\n<p>        \u5728systemctl\u91cc\u662f\u53ef\u4ee5\u5199\u5173\u673a\u524d\u8fd0\u884c\u7684\u670d\u52a1\u7684\uff0c\u4f46\u662f\u6211\u9047\u5230\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u6211\u7f16\u5199\u7684\u8fd9\u4e2a\u670d\u52a1\u662f\u5728\u8f93\u5165\u5173\u673a\u547d\u4ee4\u540e\uff0c\u5148\u5173\u95ed\u4e86docker\u670d\u52a1\u624d\u542f\u52a8\u8fd9\u4e2a\u670d\u52a1\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u6570\u636e\u5e93\u5173\u95ed\u547d\u4ee4\u7684\u65f6\u5019\uff0c\u56e0\u4e3adocker\u63d0\u524d\u5173\u95ed\u800c\u5931\u8d25\uff0c\u8fd9\u4e2a\u65b9\u6848\u544a\u7ec8\u3002<\/p>\n\n\n\n<p>\u547d\u4ee4\u6267\u884c\u6548\u679c\u662f\uff1ashutdown&#8211;&gt;docker.socket\u5173\u95ed&#8211;&gt;pg_shutdown.service\u5173\u95ed<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]\nDescription=pg_shutdown\nDefaultDependencies=no\nBefore=shutdown.target\n\n&#91;Service]\nType=oneshot\n\nExecStart=\/usr\/bin\/docker exec -it pg_ctlcluster 14 main stop --m fast\n\n&#91;Install]\nWantedBy=shutdown.target<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6700\u7ec8\u91c7\u7528\u7684\u65b9\u6848<\/h2>\n\n\n\n<p>        \u65e2\u7136\u6211\u4eec\u9700\u8981\u5728docker\u5173\u95ed\u65f6\u6267\u884c\u547d\u4ee4\uff0c\u90a3\u4e48\u5c31\u5f97\u8c03\u5934\u53bb\u627edocker\u662f\u5982\u4f55\u5904\u7406\u63a5\u53d7\u201c\u7ec8\u6b62\u201d\u4fe1\u53f7\u7684\u65b9\u6848\u3002\u6839\u636edocker\u5b98\u7f51\u7684\u77e5\u8bc6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u77e5\uff0c\u5f53\u89e6\u53d1docker stop\u6216\u8005\u5173\u673a\u7684\u65f6\u5019\uff0c\u662f\u4f1a\u5411\u5bb9\u5668\u53d1\u9001\u4e00\u4e2aSIGTERM\u4fe1\u53f7\u7684\uff0c\u6211\u4eec\u9700\u8981\u5728dockerfile\u91cc\uff0c\u4fee\u6539\u542f\u52a8\u811a\u672c\uff0c\u6dfb\u52a0\u4e00\u4e2a\u201c\u5b89\u5168\u5173\u95ed\u201d\u7684\u51fd\u6570\uff0c\u5e76\u4e14\u8ba9\u811a\u672c\u901a\u8fc7\u94a9\u5b50\u628aSIGTERM\u4fe1\u53f7\u4e0e\u201c\u5b89\u5168\u5173\u95ed\u201d\u51fd\u6570\u8fde\u5728\u4e00\u8d77\u3002\u5f53\u6536\u5230SIGTERM\u4fe1\u53f7\u65f6\u5c31\u201c\u5b89\u5168\u5173\u95ed\u201d<\/p>\n\n\n\n<p>#Dockerfile\u6587\u4ef6\u53c2\u8003<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nFROM debian:12\n# \u6b64\u5904\u7701\u7565\u5b89\u88c5postgresql\u8fc7\u7a0b\n....\n# \u7136\u540e\u653e\u4e00\u4e2adocker-entrypoint.sh\u6587\u4ef6\u8fdb\u53bb\nCOPY docker-entrypoint.sh \/usr\/local\/bin\/\nENTRYPOINT &#91;\"docker-entrypoint.sh\"]<\/code><\/pre>\n\n\n\n<p>#docker-entrypoint.sh\u6587\u4ef6\u53c2\u8003<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#!\/usr\/bin\/env bash\nfile_path=\"\/var\/run\/postgresql\/.s.PGSQL.5432.lock\"\nif &#91; -f \"$file_path\" ]; then\n    rm \"$file_path\"\nfi\nservice cron start\npg_ctlcluster 14 main start\n\nstop_container() {\n  mkdir -p \/var\/lib\/postgresql\/14\/main\/log\/\n  echo \"$(date) - Stopping PostgreSQL...\" &gt;&gt; \/var\/lib\/postgresql\/14\/main\/log\/stop.log\n  pg_ctlcluster 14 main stop --m fast\n  echo \"$(date) - PostgreSQL stopped.\" &gt;&gt; \/var\/lib\/postgresql\/14\/main\/log\/stop.log\n}\ntrap 'stop_container' SIGTERM\ntail -f \/var\/log\/postgresql\/*.log &amp;\nwait<\/code><\/pre>\n\n\n\n<p>\u8fd9\u8fb9\u6709\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7ed3\u5c3e\u9700\u8981\u52a0&amp; wait \u800c\u4e0d\u80fd\u53ea\u7528tail \uff0c\u4e0d\u7136stop_container\u4e0d\u4f1a\u89e6\u53d1\u7684<\/p>\n\n\n\n<p>        \u4f46\u662f\u5230\u8fd9\u91cc\u8fd8\u6ca1\u6709\u5b8c\u5168\u7ed3\u675f\uff0c\u56e0\u4e3apostgresql\u7684\u5173\u95ed\u6709\u65f6\u5019\u4e0d\u90a3\u4e48\u5feb\uff0c\u9ed8\u8ba4\u7684docker \u5173\u95ed\u7684\u8d85\u65f6\u65f6\u95f4\u662f10\u79d2\uff0c\u4e3a\u4e86\u6c42\u7a33\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4fee\u6539docker\u8fd0\u884c\u5bb9\u5668\u65f6\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u5728docker run\u7684\u65f6\u5019\u589e\u52a0\u53c2\u6570&#8211;stop-timeout=60\uff0c\u6ce8\u610f\uff0c\u8fd9\u4e2a\u8d85\u65f6\u65f6\u95f4\u53ea\u80fd\u5728docker run\u5bb9\u5668\u7684\u65f6\u5019\u53bb\u8bbe\u7f6e\uff0c\u901a\u8fc7\u4fee\u6539\/etc\/docker\/daemon.json\u91cc\u7684shutdown-timeout\u662f\u65e0\u6548\u7684<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6700\u540e<\/h2>\n\n\n\n<p>        \u7ecf\u8fc7\u4e00\u756a\u4fee\u6539\u4e0e\u91cd\u65b0\u7f16\u5199\u7a0b\u5e8f\uff0c\u6211\u53c8\u6d4b\u8bd5\u4e86\u591a\u6b21poweroff\u548creboot\u68c0\u67e5\uff0c\u786e\u5b9a\u7a33\u5982\u8001\u72d7\u4e4b\u540e\uff0c\u7ec8\u4e8e\u5c06\u5176\u53d1\u5e03\u81f3\u73b0\u573a\uff0c\u4ee5\u540e\u53ef\u4ee5\u5b89\u5fc3\u8fc7\u8282\u5566\u3002\u867d\u7136\u770b\u8d77\u6765\u4fee\u6539\u7684\u4ee3\u7801\u4e0d\u591a\uff0c\u4f46\u662f\u8981\u77e5\u9053\u5728\u90a3\u4fee\u6539\uff0c\u5982\u4f55\u4fee\u6539\u6709\u6548\uff0c\u603b\u5171\u82b1\u4e86\u6211\u4e00\u5929\u7684\u65f6\u95f4($_$)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u6211\u4eec\u7684\u8bb8\u591a\u9879\u76ee\u90e8\u7f72\u5b8c\u4e4b\u540e\uff0c\u662f\u4f1a\u7ecf\u5e38\u5173\u95ed\u6216\u8005\u91cd\u542f\u670d\u52a1\u5668\u7684\uff0c\u6bd4\u5982\u5404\u79cd\u8fc7\u5e74\u8fc7\u8282\u7684\u65e5\u5b50\uff0c\u505c\u5de5\u7684\u65e5\u5b50\uff0c\u8fd9\u4e00\u5173\u673a\uff0c\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1436","post","type-post","status-publish","format-standard","hentry","category-influxdb"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/1436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1436"}],"version-history":[{"count":0,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/1436\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}