{"id":216,"date":"2026-04-19T10:09:59","date_gmt":"2026-04-19T02:09:59","guid":{"rendered":"https:\/\/tweakl.com\/?p=216"},"modified":"2026-05-03T00:46:21","modified_gmt":"2026-05-02T16:46:21","slug":"docker-%e9%9b%b6%e5%9f%ba%e7%a1%80%e5%88%b0%e5%ae%9e%e6%88%98%ef%bc%9a%e4%b8%80%e6%96%87%e5%90%83%e9%80%8f%e9%95%9c%e5%83%8f%e3%80%81%e5%ae%b9%e5%99%a8%e4%b8%8e-compose","status":"publish","type":"post","link":"https:\/\/tweakl.com\/index.php\/2026\/04\/19\/docker-%e9%9b%b6%e5%9f%ba%e7%a1%80%e5%88%b0%e5%ae%9e%e6%88%98%ef%bc%9a%e4%b8%80%e6%96%87%e5%90%83%e9%80%8f%e9%95%9c%e5%83%8f%e3%80%81%e5%ae%b9%e5%99%a8%e4%b8%8e-compose\/","title":{"rendered":"Docker \u96f6\u57fa\u7840\u5230\u5b9e\u6218\uff1a\u4e00\u6587\u5403\u900f\u955c\u50cf\u3001\u5bb9\u5668\u4e0e Compose"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li>Docker \u96f6\u57fa\u7840\u5230\u5b9e\u6218\uff1a\u4e00\u6587\u5403\u900f\u955c\u50cf\u3001\u5bb9\u5668\u4e0e Compose<\/li>\n\n\n\n<li>\u4e00\u3001Docker \u662f\u4ec0\u4e48\uff1f<\/li>\n\n\n\n<li>\u4e8c\u3001\u5feb\u901f\u5f00\u59cb\uff08Hello Container\uff09<\/li>\n\n\n\n<li>\u4e09\u3001\u955c\u50cf\u4e0e\u5bb9\u5668\u7684\u5173\u7cfb<\/li>\n\n\n\n<li>\u56db\u3001Dockerfile\uff1a\u628a\u5e94\u7528\u6253\u5305\u6210\u955c\u50cf<\/li>\n\n\n\n<li>\u4e94\u3001\u6570\u636e\u4e0e\u7f51\u7edc\uff1a\u5377\u4e0e\u7aef\u53e3<\/li>\n\n\n\n<li>\u516d\u3001Docker Compose\uff1a\u4e00\u952e\u62c9\u8d77\u591a\u5bb9\u5668<\/li>\n\n\n\n<li>\u4e03\u3001\u5e38\u89c1\u573a\u666f\u6e05\u5355<\/li>\n\n\n\n<li>\u516b\u3001\u6700\u4f73\u5b9e\u8df5\uff08\u6027\u80fd\u4e0e\u5b89\u5168\uff09<\/li>\n\n\n\n<li>\u4e5d\u3001\u6392\u9519\u901f\u67e5<\/li>\n\n\n\n<li>\u5341\u3001\u5b9e\u8df5\u6e05\u5355\uff08\u5efa\u8bae\uff09<\/li>\n\n\n\n<li>\u5f69\u86cb\uff1a\u6897\u7248\u603b\u7ed3\uff08\u8f7b\u677e\u8bfb\u6cd5\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4e00\u3001Docker \u662f\u4ec0\u4e48\uff1f\">\u4e00\u3001Docker \u662f\u4ec0\u4e48\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e00\u4e2a\u201c\u88c5\u5e94\u7528\u7684\u76d2\u5b50\u201d\u6807\u51c6\uff1a\u955c\u50cf\u50cf\u6a21\u5177\uff0c\u5bb9\u5668\u50cf\u6210\u54c1\uff0c\u54ea\u91cc\u90fd\u80fd\u7a33\u5b9a\u590d\u73b0\u3002<\/li>\n\n\n\n<li>\u89e3\u51b3\u201c\u6211\u7535\u8111\u80fd\u8dd1\u3001\u4ed6\u7535\u8111\u8dd1\u4e0d\u8d77\u6765\u201d\u7684\u73af\u5883\u4e0d\u4e00\u81f4\u95ee\u9898\ufffd\ufffd<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4e8c\u3001\u5feb\u901f\u5f00\u59cb\uff08Hello Container\uff09\">\u4e8c\u3001\u5feb\u901f\u5f00\u59cb\uff08Hello Container\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b89\u88c5 Docker Desktop\uff08Windows\/Mac\uff09\u6216\u4f7f\u7528\u5305\u7ba1\u7406\u5668\uff08Linux\uff09\u3002<\/li>\n\n\n\n<li>\u8bd5\u8dd1\uff1a<br>docker run &#8211;rm hello-world<\/li>\n\n\n\n<li>docker run &#8211;rm -p 8080:80 nginx<\/li>\n\n\n\n<li>\u8bbf\u95ee\u00a0<a href=\"http:\/\/localhost:8080\/\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/localhost:8080<\/a>\u00a0\u9a8c\u8bc1\u6210\u529f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u7528\u547d\u4ee4\u901f\u67e5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>docker ps \u67e5\u770b\u5bb9\u5668\u3001docker images \u67e5\u770b\u955c\u50cf<\/li>\n\n\n\n<li>docker pull\/push \u62c9\u53d6\/\u63a8\u9001\u3001docker run \u542f\u52a8<\/li>\n\n\n\n<li>docker logs \u770b\u65e5\u5fd7\u3001docker exec -it \u8fdb\u5165\u5bb9\u5668<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4e09\u3001\u955c\u50cf\u4e0e\u5bb9\u5668\u7684\u5173\u7cfb\">\u4e09\u3001\u955c\u50cf\u4e0e\u5bb9\u5668\u7684\u5173\u7cfb<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u955c\u50cf\uff08Image\uff09\uff1a\u53ea\u8bfb\u6a21\u677f\uff0c\u5305\u542b\u8fd0\u884c\u6240\u9700\u73af\u5883\u4e0e\u4f9d\u8d56\u3002<\/li>\n\n\n\n<li>\u5bb9\u5668\uff08Container\uff09\uff1a\u955c\u50cf\u7684\u8fd0\u884c\u5b9e\u4f8b\uff0c\u53ef\u8bfb\u5199\uff0c\u6709\u751f\u547d\u5468\u671f\uff08\u542f\u52a8\/\u505c\u6b62\/\u5220\u9664\uff09\u3002<\/li>\n\n\n\n<li>\u5206\u5c42\u5b58\u50a8\uff1a\u955c\u50cf\u5206\u5c42\u53ef\u590d\u7528\uff0c\u5bb9\u5668\u5728\u5176\u4e0a\u8ffd\u52a0\u53ef\u5199\u5c42\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u56db\u3001Dockerfile\uff1a\u628a\u5e94\u7528\u6253\u5305\u6210\u955c\u50cf\">\u56db\u3001Dockerfile\uff1a\u628a\u5e94\u7528\u6253\u5305\u6210\u955c\u50cf<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1aNode.js \u5e94\u7528<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># 1. \u9009\u62e9\u57fa\u7840\u955c\u50cf<\/em>\nFROM node:20-alpine AS deps\n\n<em># 2. \u5b89\u88c5\u4f9d\u8d56<\/em>\nWORKDIR \/app\nCOPY package*.json .\/\nRUN npm ci --only=production\n\n<em># 3. \u590d\u5236\u6e90\u7801\u5e76\u6784\u5efa<\/em>\nFROM node:20-alpine AS runner\nWORKDIR \/app\nCOPY --from=deps \/app\/node_modules .\/node_modules\nCOPY . .\nEXPOSE 3000\nCMD &#91;\"node\", \"server.js\"]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u591a\u9636\u6bb5\u6784\u5efa\u51cf\u5c0f\u4f53\u79ef\uff1balpine \u4f53\u79ef\u5c0f\u4f46\u6709\u65f6\u9700\u989d\u5916\u4f9d\u8d56\uff08\u5982 glibc\uff09\u3002<\/li>\n\n\n\n<li>\u5c3d\u91cf\u56fa\u5b9a\u57fa\u7840\u955c\u50cf\u4e0e\u4f9d\u8d56\u7248\u672c\uff0c\u6784\u5efa\u66f4\u53ef\u91cd\u590d\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4e94\u3001\u6570\u636e\u4e0e\u7f51\u7edc\uff1a\u5377\u4e0e\u7aef\u53e3\">\u4e94\u3001\u6570\u636e\u4e0e\u7f51\u7edc\uff1a\u5377\u4e0e\u7aef\u53e3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7aef\u53e3\u6620\u5c04\uff1a-p 8080:80\uff08\u672c\u673a:\u5bb9\u5668\uff09<\/li>\n\n\n\n<li>\u6302\u8f7d\u5377\uff1a-v \/host\/data:\/container\/data \u6301\u4e45\u5316\u6570\u636e\u6216\u70ed\u66f4\u65b0\u4ee3\u7801<\/li>\n\n\n\n<li>\u7f51\u7edc\uff1a\u9ed8\u8ba4 bridge\uff1b\u53ef\u7528\u81ea\u5b9a\u4e49\u7f51\u7edc\u8ba9\u591a\u4e2a\u5bb9\u5668\u4e92\u76f8\u901a\u8fc7\u540d\u5b57\u53d1\u73b0<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u521b\u5efa\u7f51\u7edc\ndocker network create mynet\n# \u542f\u52a8\u4e24\u4e2a\u5bb9\u5668\u5e76\u52a0\u5165\u7f51\u7edc\ndocker run -d <em>--name redis --network mynet redis:7<\/em>\ndocker run -d <em>--name api --network mynet -p 3000:3000 my-api:latest<\/em><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u516d\u3001Docker Compose\uff1a\u4e00\u952e\u62c9\u8d77\u591a\u5bb9\u5668\">\u516d\u3001Docker Compose\uff1a\u4e00\u952e\u62c9\u8d77\u591a\u5bb9\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>docker-compose.yml<\/code>&nbsp;\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>version: \"3.9\"\nservices:\n  db:\n    image: postgres:16\n    environment:\n      POSTGRES_PASSWORD: example\n    volumes:\n      - pgdata:\/var\/lib\/postgresql\/data\n  api:\n    build: .\n    ports:\n      - \"3000:3000\"\n    environment:\n      DATABASE_URL: postgres:\/\/postgres:example@db:5432\/postgres\n    depends_on:\n      - db\nvolumes:\n  pgdata:<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\uff1a<code>docker compose up -d<\/code>\uff0c\u505c\u6b62\uff1a<code>docker compose down<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4e03\u3001\u5e38\u89c1\u573a\u666f\u6e05\u5355\">\u4e03\u3001\u5e38\u89c1\u573a\u666f\u6e05\u5355<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u672c\u5730\u5f00\u53d1\uff1a\u6570\u636e\u5e93\u3001\u7f13\u5b58\u3001\u6d88\u606f\u961f\u5217\u4e00\u952e\u62c9\u8d77\uff1b\u4ee3\u7801\u6302\u8f7d\u70ed\u66f4\u65b0\u3002<\/li>\n\n\n\n<li>\u524d\u540e\u7aef\u8054\u8c03\uff1aNginx \u53cd\u5411\u4ee3\u7406 + \u540e\u7aef\u5bb9\u5668\uff0c\u6a21\u62df\u751f\u4ea7\u5165\u53e3\u3002<\/li>\n\n\n\n<li>\u4efb\u52a1\u4f5c\u4e1a\uff1a\u5b9a\u65f6\/\u961f\u5217\u6d88\u8d39\u8005\u5bb9\u5668\u5316\uff0c\u7edf\u4e00\u8fd0\u884c\u73af\u5883\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u516b\u3001\u6700\u4f73\u5b9e\u8df5\uff08\u6027\u80fd\u4e0e\u5b89\u5168\uff09\">\u516b\u3001\u6700\u4f73\u5b9e\u8df5\uff08\u6027\u80fd\u4e0e\u5b89\u5168\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u955c\u50cf\u4f53\u79ef\u4f18\u5316\uff1a\u9009\u62e9\u7cbe\u7b80\u57fa\u7840\u955c\u50cf\uff0c\u591a\u9636\u6bb5\u6784\u5efa\uff0c\u6e05\u7406\u6784\u5efa\u4ea7\u7269\u4e0e\u7f13\u5b58\u3002<\/li>\n\n\n\n<li>\u6700\u5c0f\u6743\u9650\uff1a\u5bb9\u5668\u5185\u4f7f\u7528\u975e root \u7528\u6237\uff1b\u53ea\u5f00\u653e\u5fc5\u8981\u7aef\u53e3\u4e0e\u80fd\u529b\u3002<\/li>\n\n\n\n<li>\u8d44\u6e90\u9650\u5236\uff1a&#8211;cpus\u3001&#8211;memory \u63a7\u5236\u8d44\u6e90\u5360\u7528\uff1b\u5408\u7406\u5065\u5eb7\u68c0\u67e5\u4e0e\u91cd\u542f\u7b56\u7565\u3002<\/li>\n\n\n\n<li>\u673a\u5bc6\u7ba1\u7406\uff1a\u907f\u514d\u628a\u5bc6\u94a5\u5199\u8fdb\u955c\u50cf\uff1b\u7528\u73af\u5883\u53d8\u91cf\u3001Secrets \u6216\u914d\u7f6e\u4e2d\u5fc3\u3002<\/li>\n\n\n\n<li>\u65e5\u5fd7\u4e0e\u6392\u9519\uff1adocker logs\u3001docker inspect\u3001docker exec\uff0c\u914d\u5408 Compose \u7684 depends_on \u4e0e\u5065\u5eb7\u68c0\u67e5\u5b9a\u4f4d\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4e5d\u3001\u6392\u9519\u901f\u67e5\">\u4e5d\u3001\u6392\u9519\u901f\u67e5<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7aef\u53e3\u88ab\u5360\u7528\uff1a\u66f4\u6362\u5bbf\u4e3b\u7aef\u53e3\u6216\u505c\u6b62\u51b2\u7a81\u670d\u52a1\u3002<\/li>\n\n\n\n<li>\u7f51\u7edc\u4e0d\u901a\uff1a\u68c0\u67e5\u5bb9\u5668\u7f51\u7edc\/\u670d\u52a1\u540d\uff1bdocker exec \u8fdb\u5bb9\u5668 curl\u3002<\/li>\n\n\n\n<li>\u65e0\u6cd5\u8fde\u63a5\u6570\u636e\u5e93\uff1a\u786e\u8ba4\u73af\u5883\u53d8\u91cf\u3001\u7f51\u7edc\u540d\u3001\u7aef\u53e3\u6620\u5c04\u4e0e\u6743\u9650\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5341\u3001\u5b9e\u8df5\u6e05\u5355\uff08\u5efa\u8bae\uff09\">\u5341\u3001\u5b9e\u8df5\u6e05\u5355\uff08\u5efa\u8bae\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u628a\u4e00\u4e2a Node\/Java\/Python \u5c0f\u9879\u76ee\u5bb9\u5668\u5316\u5e76\u7528 Compose \u642d\u914d\u6570\u636e\u5e93\u8dd1\u8d77\u6765\u3002<\/li>\n\n\n\n<li>\u7ed9 Dockerfile \u505a\u4f53\u79ef\u4f18\u5316\uff0c\u6bd4\u8f83\u6784\u5efa\u524d\u540e\u955c\u50cf\u5927\u5c0f\u4e0e\u542f\u52a8\u65f6\u95f4\u3002<\/li>\n\n\n\n<li>\u7f16\u5199\u5065\u5eb7\u68c0\u67e5\u4e0e\u91cd\u542f\u7b56\u7565\uff0c\u6a21\u62df\u5f02\u5e38\u6062\u590d\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5f69\u86cb\uff1a\u6897\u7248\u603b\u7ed3\uff08\u8f7b\u677e\u8bfb\u6cd5\uff09\">\u5f69\u86cb\uff1a\u6897\u7248\u603b\u7ed3\uff08\u8f7b\u677e\u8bfb\u6cd5\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Docker \u5c31\u50cf\u201c\u5916\u5356\u76d2\u201d\uff1a\u955c\u50cf\u662f\u83dc\u8c31+\u505a\u597d\u7684\u83dc\u578b\uff0c\u5bb9\u5668\u662f\u7aef\u4e0a\u684c\u7684\u4e00\u4efd\uff1b<\/li>\n\n\n\n<li>-p 8080:80 \u662f\u201c\u7a97\u53e3\u53f7\u201d\uff0c\u522b\u62ff\u9519\u7a97\u53e3\u558a\u8001\u677f\uff1b<\/li>\n\n\n\n<li>Compose \u662f\u201c\u5957\u9910\u4e00\u952e\u4e0b\u5355\u201d\uff0c\u5f00\u4f1a\u4e0d\u803d\u8bef\u5403\u996d\uff08\u5f00\u53d1\u73af\u5883\u4e00\u952e\u62c9\u8d77\uff09\uff1b<\/li>\n\n\n\n<li>\u591a\u9636\u6bb5\u6784\u5efa\u50cf\u201c\u505a\u5b8c\u5c31\u6d17\u9505\u201d\uff0c\u65e2\u5e72\u51c0\u53c8\u6e05\u723d\uff1b<\/li>\n\n\n\n<li>\u8bb0\u5f97\u7528\u975e root\uff0c\u522b\u8ba9\u201c\u5916\u5356\u5458\u6709\u5e97\u91cc\u6240\u6709\u94a5\u5319\u201d\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u5411\u5c0f\u767d\u7684\u7531\u6d45\u5165\u6df1\u6559\u7a0b\uff1a10 \u5206\u949f\u8dd1\u8d77\u6765\uff0c\u4e00\u4e2a\u5c0f\u65f6\u5b8c\u6210\u672c\u5730\u5f00\u53d1\u73af\u5883\u5bb9\u5668\u5316\uff0c\u8fdb\u4e00\u6b65\u638c\u63e1 Dockerfile \u4e0e Compose\uff0c\u6700\u540e\u7ed9\u51fa\u6027\u80fd\u4e0e\u5b89\u5168\u6700\u4f73\u5b9e\u8df5<\/p>\n","protected":false},"author":1,"featured_media":219,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-216","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vps"],"_links":{"self":[{"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/posts\/216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/comments?post=216"}],"version-history":[{"count":1,"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/posts\/216\/revisions"}],"predecessor-version":[{"id":218,"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/posts\/216\/revisions\/218"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/media\/219"}],"wp:attachment":[{"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/media?parent=216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/categories?post=216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tweakl.com\/index.php\/wp-json\/wp\/v2\/tags?post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}