{"id":31437,"date":"2026-07-04T16:51:25","date_gmt":"2026-07-04T08:51:25","guid":{"rendered":"https:\/\/cn.hostease.com\/blog\/uncategorized\/docker-compose-multi-service-deployment\/"},"modified":"2026-07-04T16:51:25","modified_gmt":"2026-07-04T08:51:25","slug":"docker-compose-multi-service-deployment","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/blog\/tutorials\/docker-compose-multi-service-deployment\/","title":{"rendered":"Docker Compose \u591a\u670d\u52a1\u7f16\u6392\u5b9e\u6218\uff1a\u4ece\u5f00\u53d1\u73af\u5883\u5230\u751f\u4ea7\u90e8\u7f72"},"content":{"rendered":"<p><img decoding=\"async\" alt=\"Docker Compose \u591a\u670d\u52a1\u7f16\u6392\u6982\u5ff5\u56fe\" src=\"images\/docker-compose-multi-service-deployment-cover.webp\" \/><\/p>\n<p>\u4f60\u662f\u5426\u9047\u5230\u8fc7\u8fd9\u6837\u7684\u573a\u666f\uff1a\u672c\u5730\u5f00\u53d1\u65f6\u7528\u4e00\u6761 <code>docker-compose up<\/code> \u5c31\u80fd\u8dd1\u8d77\u6574\u4e2a\u5e94\u7528\u6808\uff0c\u4f46\u5230\u4e86\u751f\u4ea7\u73af\u5883\u5374\u72b6\u51b5\u767e\u51fa\u2014\u2014\u6570\u636e\u5e93\u8fde\u63a5\u8d85\u65f6\u3001\u670d\u52a1\u95f4\u901a\u4fe1\u5931\u8d25\u3001\u6570\u636e\u5377\u6302\u8f7d\u51fa\u9519\uff1f\u8fd9\u7bc7\u6587\u7ae0\u6559\u4f60\u5982\u4f55\u628a Docker Compose\uff08\u5bb9\u5668\u7f16\u6392\u5de5\u5177\uff09\u4ece\u5f00\u53d1\u73a9\u5177\u53d8\u6210\u751f\u4ea7\u53ef\u7528\u7684\u90e8\u7f72\u65b9\u6848\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4e00\u4e2a\u5178\u578b\u7684 Web \u5e94\u7528\u81f3\u5c11\u5305\u542b\u4e09\u4e2a\u670d\u52a1\uff1aWeb \u670d\u52a1\u5668\u3001\u6570\u636e\u5e93\u548c\u7f13\u5b58\u3002\u624b\u52a8\u7ba1\u7406\u8fd9\u4e9b\u670d\u52a1\u7684\u542f\u52a8\u987a\u5e8f\u3001\u7f51\u7edc\u4e92\u901a\u548c\u6570\u636e\u6301\u4e45\u5316\uff0c\u4e0d\u4ec5\u7e41\u7410\u8fd8\u5bb9\u6613\u51fa\u9519\u3002Docker Compose \u7528\u4e00\u4e2a YAML \u6587\u4ef6\u5c31\u80fd\u5b9a\u4e49\u6574\u4e2a\u5e94\u7528\u6808\uff0c\u8ba9\u4f60\u5728\u4efb\u4f55 <a href=\"https:\/\/cn.hostease.com\/vps\/\">VPS\uff08\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668\uff09<\/a> \u6216\u4e91\u670d\u52a1\u5668\uff08\u57fa\u4e8e\u4e91\u8ba1\u7b97\u7684\u865a\u62df\u4e3b\u673a\uff09\u4e0a\u90fd\u80fd\u4e00\u952e\u90e8\u7f72\u3002<\/p>\n<h2>Docker Compose \u57fa\u7840\uff1a\u4e00\u4e2a\u6587\u4ef6\u5b9a\u4e49\u6574\u4e2a\u5e94\u7528\u6808<\/h2>\n<p>Docker Compose \u7684\u6838\u5fc3\u662f\u4e00\u4e2a <code>docker-compose.yml<\/code> \u6587\u4ef6\u3002\u76f8\u6bd4\u9010\u4e2a\u8fd0\u884c <code>docker run<\/code> \u547d\u4ee4\uff0cCompose \u8ba9\u4f60\u7528\u58f0\u660e\u5f0f\u7684\u65b9\u5f0f\u63cf\u8ff0\u6240\u6709\u670d\u52a1\u3001\u7f51\u7edc\u548c\u6570\u636e\u5377\u3002\u5f53\u4f60\u9700\u8981\u91cd\u5efa\u73af\u5883\u65f6\uff0c\u4e00\u6761\u547d\u4ee4\u5c31\u80fd\u4ece\u96f6\u542f\u52a8\u5168\u90e8\u670d\u52a1\u3002<\/p>\n<p>\u4e00\u4e2a\u6700\u57fa\u7840\u7684 Compose \u6587\u4ef6\u5305\u542b\u4e09\u4e2a\u9876\u7ea7\u914d\u7f6e\uff1a<\/p>\n<ul>\n<li><strong>services<\/strong>\uff1a\u5b9a\u4e49\u5404\u4e2a\u5bb9\u5668\u670d\u52a1\uff0c\u5305\u62ec\u955c\u50cf\u3001\u7aef\u53e3\u3001\u73af\u5883\u53d8\u91cf\u7b49<\/li>\n<li><strong>networks<\/strong>\uff1a\u5b9a\u4e49\u670d\u52a1\u95f4\u7684\u901a\u4fe1\u7f51\u7edc\uff0c\u9ed8\u8ba4\u6240\u6709\u670d\u52a1\u5171\u4eab\u540c\u4e00\u4e2a\u7f51\u7edc<\/li>\n<li><strong>volumes<\/strong>\uff1a\u5b9a\u4e49\u6301\u4e45\u5316\u6570\u636e\u5377\uff0c\u786e\u4fdd\u5bb9\u5668\u91cd\u542f\u540e\u6570\u636e\u4e0d\u4e22\u5931<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5178\u578b\u7684\u4e09\u5c42\u5e94\u7528 Compose \u6587\u4ef6\u793a\u4f8b\uff1a<\/p>\n<p>yaml<br \/>\nversion: &#8220;3.8&#8221;<\/p>\n<p>services:<br \/>\n  web:<br \/>\n    image: nginx:alpine<br \/>\n    ports:<br \/>\n      &#8211; &#8220;80:80&#8221;<br \/>\n    depends_on:<br \/>\n      &#8211; app<br \/>\n    networks:<br \/>\n      &#8211; frontend<\/p>\n<p>app:<br \/>\n    build: .\/app<br \/>\n    environment:<br \/>\n      &#8211; DB_HOST=db<br \/>\n      &#8211; REDIS_HOST=cache<br \/>\n    depends_on:<br \/>\n      &#8211; db<br \/>\n      &#8211; cache<br \/>\n    networks:<br \/>\n      &#8211; frontend<br \/>\n      &#8211; backend<\/p>\n<p>db:<br \/>\n    image: mysql:8.0<br \/>\n    environment:<br \/>\n      MYSQL_ROOT_PASSWORD_FILE: \/run\/secrets\/db_root_password<br \/>\n    volumes:<br \/>\n      &#8211; db_data:\/var\/lib\/mysql<br \/>\n    networks:<br \/>\n      &#8211; backend<\/p>\n<p>cache:<br \/>\n    image: redis:7-alpine<br \/>\n    networks:<br \/>\n      &#8211; backend<\/p>\n<p>volumes:<br \/>\n  db_data:<\/p>\n<p>networks:<br \/>\n  frontend:<br \/>\n  backend:\n<\/p>\n<p>\u8fd9\u6bb5\u914d\u7f6e\u5b9a\u4e49\u4e86\u56db\u4e2a\u670d\u52a1\uff1aNginx \u53cd\u5411\u4ee3\u7406\u3001\u5e94\u7528\u670d\u52a1\u3001MySQL \u6570\u636e\u5e93\u548c Redis \u7f13\u5b58\u3002<code>depends_on<\/code> \u63a7\u5236\u542f\u52a8\u987a\u5e8f\uff0c<code>networks<\/code> \u9694\u79bb\u524d\u7aef\u548c\u540e\u7aef\u6d41\u91cf\uff0c<code>volumes<\/code> \u786e\u4fdd\u6570\u636e\u5e93\u6570\u636e\u6301\u4e45\u5316\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u6df1\u5165\u4e86\u89e3\u670d\u52a1\u5668\u914d\u7f6e\u4e0e\u6027\u80fd\u8c03\u4f18\uff0c\u53ef\u4ee5\u53c2\u8003 <a href=\"https:\/\/cn.hostease.com\/blog\/server\/\">\u670d\u52a1\u5668\u914d\u7f6e\u6307\u5357<\/a>\u3002<\/p>\n<h2>\u5f00\u53d1\u73af\u5883\u5b9e\u6218\uff1a\u4ece\u96f6\u642d\u5efa\u672c\u5730\u591a\u670d\u52a1\u73af\u5883<\/h2>\n<p>\u5728\u5f00\u53d1\u9636\u6bb5\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5feb\u901f\u542f\u52a8\u3001\u65b9\u4fbf\u8c03\u8bd5\u3001\u6570\u636e\u53ef\u91cd\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u4e8e Node.js + MySQL + Redis \u7684\u5b9e\u9645\u9879\u76ee\u914d\u7f6e\u3002<\/p>\n<h3>\u9879\u76ee\u76ee\u5f55\u7ed3\u6784<\/h3>\n<p>\nmy-project\/<br \/>\n\u251c\u2500\u2500 docker-compose.yml<br \/>\n\u251c\u2500\u2500 app\/<br \/>\n\u2502   \u251c\u2500\u2500 Dockerfile<br \/>\n\u2502   \u251c\u2500\u2500 package.json<br \/>\n\u2502   \u2514\u2500\u2500 src\/<br \/>\n\u251c\u2500\u2500 db\/<br \/>\n\u2502   \u2514\u2500\u2500 init.sql<br \/>\n\u2514\u2500\u2500 .env\n<\/p>\n<h3>\u5173\u952e\u914d\u7f6e\u8981\u70b9<\/h3>\n<p><strong>\u73af\u5883\u53d8\u91cf\u5206\u79bb<\/strong>\u662f\u5f00\u53d1\u4e0e\u751f\u4ea7\u5171\u7528\u4e00\u5957 Compose \u6587\u4ef6\u7684\u6838\u5fc3\u6280\u5de7\u3002\u901a\u8fc7 <code>.env<\/code> \u6587\u4ef6\u548c <code>env_file<\/code> \u6307\u4ee4\uff0c\u4e0d\u540c\u73af\u5883\u52a0\u8f7d\u4e0d\u540c\u914d\u7f6e\uff1a<\/p>\n<p>yaml<\/p>\n<h2>docker-compose.yml \u4e2d\u5f15\u7528\u73af\u5883\u53d8\u91cf<\/h2>\n<p>services:<br \/>\n  app:<br \/>\n    build: .\/app<br \/>\n    env_file:<br \/>\n      &#8211; .env.${NODE_ENV:-development}<br \/>\n    volumes:<br \/>\n      &#8211; .\/app\/src:\/usr\/src\/app\/src  # \u5f00\u53d1\u65f6\u6302\u8f7d\u6e90\u7801\uff0c\u652f\u6301\u70ed\u91cd\u8f7d<br \/>\n    ports:<br \/>\n      &#8211; &#8220;3000:3000&#8221;<br \/>\n      &#8211; &#8220;9229:9229&#8221;  # Node.js \u8c03\u8bd5\u7aef\u53e3\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-linenumbers=\"false\">\n# .env.development\nNODE_ENV=development\nDB_HOST=db\nDB_PORT=3306\nDB_NAME=myapp_dev\nREDIS_URL=redis:\/\/cache:6379\nDEBUG=true\n<\/pre>\n<p><strong>\u6570\u636e\u521d\u59cb\u5316<\/strong>\u65b9\u9762\uff0cMySQL \u5b98\u65b9\u955c\u50cf\u652f\u6301\u5728\u9996\u6b21\u542f\u52a8\u65f6\u81ea\u52a8\u6267\u884c <code>\/docker-entrypoint-initdb.d\/<\/code> \u76ee\u5f55\u4e0b\u7684 SQL \u6587\u4ef6\u3002\u5c06\u4f60\u7684\u5efa\u8868\u8bed\u53e5\u548c\u521d\u59cb\u6570\u636e\u653e\u5728 <code>db\/init.sql<\/code>\uff0c\u5bb9\u5668\u9996\u6b21\u542f\u52a8\u65f6\u4f1a\u81ea\u52a8\u6267\u884c\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-linenumbers=\"false\">\n# \u542f\u52a8\u6574\u4e2a\u5f00\u53d1\u73af\u5883\ndocker-compose up -d\n\n# \u67e5\u770b\u670d\u52a1\u72b6\u6001\ndocker-compose ps\n\n# \u67e5\u770b\u5e94\u7528\u65e5\u5fd7\uff08\u5b9e\u65f6\u8ddf\u8e2a\uff09\ndocker-compose logs -f app\n\n# \u91cd\u5efa\u955c\u50cf\uff08\u4ee3\u7801\u53d8\u66f4\u540e\uff09\ndocker-compose up -d --build\n<\/pre>\n<p><strong>\u8c03\u8bd5\u6280\u5de7<\/strong>\uff1a\u5f53\u5e94\u7528\u5bb9\u5668\u5185\u7684\u4ee3\u7801\u9700\u8981\u8c03\u8bd5\u65f6\uff0c\u901a\u8fc7\u6302\u8f7d\u6e90\u7801\u5377\uff08volume mount\uff09\u8ba9\u4f60\u5728\u672c\u5730\u7f16\u8f91\u5668\u4fee\u6539\u4ee3\u7801\u540e\uff0c\u5bb9\u5668\u5185\u81ea\u52a8\u751f\u6548\u3002\u5bf9\u4e8e Node.js \u5e94\u7528\uff0c\u914d\u5408 <code>nodemon<\/code> \u6216 <code>--watch<\/code> \u53c2\u6570\u53ef\u4ee5\u5b9e\u73b0\u771f\u6b63\u7684\u70ed\u91cd\u8f7d\u4f53\u9a8c\u3002\u5982\u679c\u4f60\u5728 <a href=\"https:\/\/cn.hostease.com\/dedicated-server\/\">\u72ec\u7acb\u670d\u52a1\u5668<\/a> \u4e0a\u8fd0\u884c\u5f00\u53d1\u73af\u5883\uff0c\u8d44\u6e90\u9694\u79bb\u6027\u4f1a\u66f4\u597d\u3002<\/p>\n<p><img decoding=\"async\" alt=\"Docker Compose \u5f00\u53d1\u73af\u5883\u5de5\u4f5c\u6d41\" src=\"images\/docker-compose-multi-service-deployment-dev-workflow.webp\" \/><\/p>\n<h2>\u751f\u4ea7\u73af\u5883\u90e8\u7f72\uff1a\u4ece&#8221;\u80fd\u8dd1&#8221;\u5230&#8221;\u53ef\u9760&#8221;<\/h2>\n<p>\u5f00\u53d1\u73af\u5883\u548c\u751f\u4ea7\u73af\u5883\u7684\u5dee\u5f02\uff0c\u5f80\u5f80\u4e0d\u662f\u529f\u80fd\u4e0a\u7684\uff0c\u800c\u662f\u53ef\u9760\u6027\u3001\u5b89\u5168\u6027\u548c\u6027\u80fd\u4e0a\u7684\u3002\u4ee5\u4e0b\u662f\u5c06 Docker Compose \u4ece\u5f00\u53d1\u73af\u5883\u8fc1\u79fb\u5230\u751f\u4ea7\u73af\u5883\u65f6\u5fc5\u987b\u5904\u7406\u7684\u4e94\u4e2a\u5173\u952e\u5dee\u5f02\u3002<\/p>\n<p><img decoding=\"async\" alt=\"\u5f00\u53d1\u73af\u5883\u4e0e\u751f\u4ea7\u73af\u5883\u914d\u7f6e\u5bf9\u6bd4\" src=\"images\/docker-compose-multi-service-deployment-prod-vs-dev.webp\" \/><\/p>\n<h3>\u73af\u5883\u53d8\u91cf\u4e0e\u5bc6\u94a5\u7ba1\u7406<\/h3>\n<p>\u751f\u4ea7\u73af\u5883\u7edd\u5bf9\u4e0d\u80fd\u628a\u6570\u636e\u5e93\u5bc6\u7801\u660e\u6587\u5199\u5728 <code>docker-compose.yml<\/code> \u91cc\u3002Docker Compose \u652f\u6301 <code>secrets<\/code> \u548c <code>env_file<\/code> \u4e24\u79cd\u65b9\u5f0f\u7ba1\u7406\u654f\u611f\u4fe1\u606f\uff1a<\/p>\n<p>yaml<br \/>\nservices:<br \/>\n  db:<br \/>\n    image: mysql:8.0<br \/>\n    secrets:<br \/>\n      &#8211; db_root_password<br \/>\n    environment:<br \/>\n      MYSQL_ROOT_PASSWORD_FILE: \/run\/secrets\/db_root_password<\/p>\n<p>secrets:<br \/>\n  db_root_password:<br \/>\n    file: .\/secrets\/db_root_password.txt\n<\/p>\n<h3>\u6570\u636e\u6301\u4e45\u5316\u4e0e\u5907\u4efd\u7b56\u7565<\/h3>\n<p>\u5f00\u53d1\u73af\u5883\u7684\u6570\u636e\u53ef\u4ee5\u968f\u65f6\u91cd\u7f6e\uff0c\u4f46\u751f\u4ea7\u73af\u5883\u7684\u6570\u636e\u662f\u6838\u5fc3\u8d44\u4ea7\u3002\u9664\u4e86\u4f7f\u7528\u547d\u540d\u6570\u636e\u5377\uff08named volume\uff09\uff0c\u8fd8\u9700\u8981\u5236\u5b9a\u5b9a\u671f\u5907\u4efd\u8ba1\u5212\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-linenumbers=\"false\">\n# \u5907\u4efd MySQL \u6570\u636e\u5e93\ndocker-compose exec db mysqldump -u root -p myapp_prod > backup_$(date +%Y%m%d).sql\n\n# \u5907\u4efd Redis \u6570\u636e\ndocker-compose exec cache redis-cli BGSAVE\ndocker cp $(docker-compose ps -q cache):\/data\/dump.rdb .\/backups\/\n<\/pre>\n<h3>\u7f51\u7edc\u5b89\u5168\u4e0e\u53cd\u5411\u4ee3\u7406<\/h3>\n<p>\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u6570\u636e\u5e93\u548c\u7f13\u5b58\u670d\u52a1\u4e0d\u5e94\u8be5\u66b4\u9732\u5230\u516c\u7f51\u3002\u901a\u8fc7 Compose \u7684\u7f51\u7edc\u914d\u7f6e\uff0c\u53ea\u8ba9 Web \u670d\u52a1\u76d1\u542c\u5916\u90e8\u7aef\u53e3\uff0c\u5176\u4ed6\u670d\u52a1\u4ec5\u5728\u5185\u90e8\u7f51\u7edc\u901a\u4fe1\u3002\u914d\u5408 <a href=\"https:\/\/cn.hostease.com\/blog\/optimization\/\">SSL\uff08\u5b89\u5168\u4f20\u8f93\u534f\u8bae\uff09<\/a> \u8bc1\u4e66\uff0c\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u52a0\u5bc6\u3002<\/p>\n<p>yaml<br \/>\nservices:<br \/>\n  nginx:<br \/>\n    image: nginx:alpine<br \/>\n    ports:<br \/>\n      &#8211; &#8220;443:443&#8221;<br \/>\n      &#8211; &#8220;80:80&#8221;<br \/>\n    volumes:<br \/>\n      &#8211; .\/nginx\/conf.d:\/etc\/nginx\/conf.d<br \/>\n      &#8211; .\/certs:\/etc\/nginx\/certs<br \/>\n    networks:<br \/>\n      &#8211; frontend<\/p>\n<p>app:<br \/>\n    # \u4e0d\u66b4\u9732\u7aef\u53e3\u5230\u5bbf\u4e3b\u673a\uff0c\u53ea\u5728\u5185\u90e8\u7f51\u7edc\u901a\u4fe1<br \/>\n    expose:<br \/>\n      &#8211; &#8220;3000&#8221;<br \/>\n    networks:<br \/>\n      &#8211; frontend<br \/>\n      &#8211; backend<\/p>\n<p>db:<br \/>\n    # \u751f\u4ea7\u73af\u5883\u4e0d\u6620\u5c04\u7aef\u53e3\u5230\u5bbf\u4e3b\u673a<br \/>\n    networks:<br \/>\n      &#8211; backend\n<\/p>\n<h3>\u5065\u5eb7\u68c0\u67e5\u4e0e\u81ea\u52a8\u91cd\u542f<\/h3>\n<p>\u751f\u4ea7\u73af\u5883\u9700\u8981\u786e\u4fdd\u670d\u52a1\u5728\u5d29\u6e83\u540e\u81ea\u52a8\u6062\u590d\u3002Compose \u63d0\u4f9b <code>healthcheck<\/code> \u548c <code>restart<\/code> \u914d\u7f6e\uff1a<\/p>\n<p>yaml<br \/>\nservices:<br \/>\n  app:<br \/>\n    restart: unless-stopped<br \/>\n    healthcheck:<br \/>\n      test: [&#8220;CMD&#8221;, &#8220;curl&#8221;, &#8220;-f&#8221;, &#8220;http:\/\/localhost:3000\/health&#8221;]<br \/>\n      interval: 30s<br \/>\n      timeout: 10s<br \/>\n      retries: 3<br \/>\n      start_period: 40s<\/p>\n<p>db:<br \/>\n    restart: unless-stopped<br \/>\n    healthcheck:<br \/>\n      test: [&#8220;CMD&#8221;, &#8220;mysqladmin&#8221;, &#8220;ping&#8221;, &#8220;-h&#8221;, &#8220;localhost&#8221;]<br \/>\n      interval: 10s<br \/>\n      timeout: 5s<br \/>\n      retries: 5\n<\/p>\n<p><code>restart: unless-stopped<\/code> \u786e\u4fdd\u5bb9\u5668\u5728\u610f\u5916\u9000\u51fa\u540e\u81ea\u52a8\u91cd\u542f\uff0c\u9664\u975e\u4f60\u624b\u52a8\u505c\u6b62\u5b83\u3002<code>healthcheck<\/code> \u8ba9 Docker \u5b9a\u671f\u68c0\u67e5\u670d\u52a1\u662f\u5426\u771f\u6b63\u53ef\u7528\uff0c\u800c\u4e0d\u662f\u4ec5\u4ec5&#8221;\u8fdb\u7a0b\u5728\u8fd0\u884c&#8221;\u3002<\/p>\n<h2>\u5f00\u53d1 vs \u751f\u4ea7\u914d\u7f6e\u5bf9\u6bd4<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u914d\u7f6e\u9879<\/th>\n<th>\u5f00\u53d1\u73af\u5883<\/th>\n<th>\u751f\u4ea7\u73af\u5883<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u73af\u5883\u53d8\u91cf<\/td>\n<td><code>.env.development<\/code>\uff0c\u660e\u6587<\/td>\n<td><code>secrets<\/code> \u6216\u52a0\u5bc6\u7684 <code>.env.production<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u5377<\/td>\n<td>\u7ed1\u5b9a\u6302\u8f7d\uff08bind mount\uff09\uff0c\u6e90\u7801\u540c\u6b65<\/td>\n<td>\u547d\u540d\u5377\uff08named volume\uff09\uff0c\u6570\u636e\u6301\u4e45\u5316<\/td>\n<\/tr>\n<tr>\n<td>\u7aef\u53e3\u6620\u5c04<\/td>\n<td>\u5168\u90e8\u66b4\u9732\uff0c\u65b9\u4fbf\u8c03\u8bd5<\/td>\n<td>\u53ea\u66b4\u9732 Web \u7aef\u53e3\uff0c\u6570\u636e\u5e93\u4e0d\u5bf9\u5916<\/td>\n<\/tr>\n<tr>\n<td>\u91cd\u542f\u7b56\u7565<\/td>\n<td><code>no<\/code> \u6216 <code>on-failure<\/code><\/td>\n<td><code>unless-stopped<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u5065\u5eb7\u68c0\u67e5<\/td>\n<td>\u53ef\u9009<\/td>\n<td>\u5fc5\u987b\u914d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>\u955c\u50cf\u6784\u5efa<\/td>\n<td><code>build: .\/app<\/code>\uff0c\u672c\u5730 Dockerfile<\/td>\n<td>\u9884\u6784\u5efa\u955c\u50cf\uff0c\u63a8\u9001\u5230\u955c\u50cf\u4ed3\u5e93<\/td>\n<\/tr>\n<tr>\n<td>\u65e5\u5fd7\u7ea7\u522b<\/td>\n<td><code>DEBUG<\/code><\/td>\n<td><code>WARN<\/code> \u6216 <code>ERROR<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u5e38\u89c1\u95ee\u9898\u6392\u67e5<\/h2>\n<p>\u5728\u5b9e\u9645\u90e8\u7f72\u8fc7\u7a0b\u4e2d\uff0c\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898\u51fa\u73b0\u9891\u7387\u6700\u9ad8\uff1a<\/p>\n<p><strong>\u670d\u52a1\u542f\u52a8\u987a\u5e8f\u95ee\u9898<\/strong>\uff1a<code>depends_on<\/code> \u53ea\u4fdd\u8bc1\u5bb9\u5668\u542f\u52a8\uff0c\u4e0d\u4fdd\u8bc1\u670d\u52a1\u5c31\u7eea\u3002MySQL \u5bb9\u5668\u542f\u52a8\u540e\u8fd8\u9700\u8981\u51e0\u79d2\u521d\u59cb\u5316\u6570\u636e\u5e93\uff0c\u6b64\u65f6\u5e94\u7528\u8fde\u63a5\u4f1a\u5931\u8d25\u3002\u89e3\u51b3\u65b9\u6848\u662f\u5728\u5e94\u7528\u5165\u53e3\u6dfb\u52a0\u91cd\u8bd5\u903b\u8f91\uff0c\u6216\u4f7f\u7528 <code>wait-for-it.sh<\/code> \u811a\u672c\u7b49\u5f85\u7aef\u53e3\u53ef\u7528\u3002<\/p>\n<p><strong>DNS\uff08\u57df\u540d\u7cfb\u7edf\uff09\u89e3\u6790\u5931\u8d25<\/strong>\uff1aCompose \u7f51\u7edc\u5185\u7684\u670d\u52a1\u540d\u5c31\u662f\u4e3b\u673a\u540d\uff0c\u4f46\u5982\u679c\u4f60\u5728\u5bb9\u5668\u5185\u4f7f\u7528 <code>localhost<\/code> \u8fde\u63a5\u5176\u4ed6\u670d\u52a1\uff0c\u4f1a\u8fde\u63a5\u5230\u5bb9\u5668\u81ea\u8eab\u3002\u6b63\u786e\u505a\u6cd5\u662f\u4f7f\u7528\u670d\u52a1\u540d\uff0c\u5982 <code>db:3306<\/code> \u800c\u975e <code>localhost:3306<\/code>\u3002<\/p>\n<p><strong>\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3<\/strong>\uff1a\u957f\u671f\u8fd0\u884c\u7684\u751f\u4ea7\u670d\u52a1\u5668\u4e0a\uff0cDocker \u955c\u50cf\u548c\u65e5\u5fd7\u4f1a\u9010\u6e10\u5360\u7528\u5927\u91cf\u78c1\u76d8\u7a7a\u95f4\u3002\u5b9a\u671f\u6267\u884c <code>docker system prune -f<\/code> \u6e05\u7406\u65e0\u7528\u8d44\u6e90\uff0c\u914d\u5408\u65e5\u5fd7\u8f6e\u8f6c\u914d\u7f6e\uff08<code>logging: options: max-size: \"10m\"<\/code>\uff09\u63a7\u5236\u65e5\u5fd7\u4f53\u79ef\u3002<\/p>\n<h2>\u603b\u7ed3\u4e0e\u4e0b\u4e00\u6b65\u5efa\u8bae<\/h2>\n<p>Docker Compose \u591a\u670d\u52a1\u7f16\u6392\u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e&#8221;\u4e00\u6b21\u5b9a\u4e49\uff0c\u5904\u5904\u8fd0\u884c&#8221;\u3002\u4ece\u5f00\u53d1\u5230\u751f\u4ea7\uff0c\u4f60\u53ea\u9700\u8981\u7ef4\u62a4\u4e00\u5957 Compose \u6587\u4ef6\uff0c\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u548c\u8986\u76d6\u6587\u4ef6\uff08<code>docker-compose.override.yml<\/code>\uff09\u9002\u914d\u4e0d\u540c\u73af\u5883\u3002\u5982\u679c\u4f60\u7684\u9879\u76ee\u89c4\u6a21\u9010\u6e10\u589e\u957f\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u8fdb\u9636\u65b9\u5411\uff1a<\/p>\n<ul>\n<li><strong>CI\/CD \u96c6\u6210<\/strong>\uff1a\u5728 GitHub Actions \u6216 GitLab CI \u4e2d\u4f7f\u7528 Compose \u8fd0\u884c\u96c6\u6210\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6bcf\u6b21\u63d0\u4ea4\u90fd\u7ecf\u8fc7\u5b8c\u6574\u7684\u670d\u52a1\u6808\u9a8c\u8bc1<\/li>\n<li><strong>\u76d1\u63a7\u4e0e\u544a\u8b66<\/strong>\uff1a\u63a5\u5165 Prometheus + Grafana \u76d1\u63a7\u5bb9\u5668\u8d44\u6e90\u4f7f\u7528\uff0c\u8bbe\u7f6e CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8\u7a7a\u95f4\u544a\u8b66\u9608\u503c<\/li>\n<li><strong>\u5bb9\u5668\u7f16\u6392\u5347\u7ea7<\/strong>\uff1a\u5f53\u5355\u673a Compose \u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u65f6\uff0c\u53ef\u4ee5\u5c06 Compose \u6587\u4ef6\u8f6c\u6362\u4e3a Kubernetes \u6216 Docker Swarm \u90e8\u7f72\uff0c\u5b9e\u73b0\u591a\u8282\u70b9\u96c6\u7fa4\u7ba1\u7406<\/li>\n<\/ul>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5bfb\u627e\u4e00\u4e2a\u7a33\u5b9a\u53ef\u9760\u7684\u670d\u52a1\u5668\u73af\u5883\u6765\u8fd0\u884c Docker Compose \u5e94\u7528\uff0c\u53ef\u4ee5\u8003\u8651 <a href=\"https:\/\/cn.hostease.com\/vps\/\">VPS\uff08\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668\uff09\u4e3b\u673a<\/a> \u65b9\u6848\uff0c\u652f\u6301\u4e00\u952e\u5b89\u88c5 Docker\uff0c\u8ba9\u4f60\u4e13\u6ce8\u4e8e\u5e94\u7528\u5f00\u53d1\u800c\u975e\u57fa\u7840\u8bbe\u65bd\u7ef4\u62a4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u662f\u5426\u9047\u5230\u8fc7\u8fd9\u6837\u7684\u573a\u666f\uff1a\u672c\u5730\u5f00\u53d1\u65f6\u7528\u4e00\u6761 docker-compose up \u5c31\u80fd\u8dd1\u8d77\u6574\u4e2a\u5e94\u7528\u6808\uff0c\u4f46\u5230\u4e86\u751f\u4ea7\u73af &#8230; <a title=\"Docker Compose \u591a\u670d\u52a1\u7f16\u6392\u5b9e\u6218\uff1a\u4ece\u5f00\u53d1\u73af\u5883\u5230\u751f\u4ea7\u90e8\u7f72\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/blog\/tutorials\/docker-compose-multi-service-deployment\/\" aria-label=\"\u9605\u8bfb Docker Compose \u591a\u670d\u52a1\u7f16\u6392\u5b9e\u6218\uff1a\u4ece\u5f00\u53d1\u73af\u5883\u5230\u751f\u4ea7\u90e8\u7f72\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4099],"tags":[3939,3637,3634,4479],"class_list":["post-31437","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-devops","tag-docker","tag-3634","tag-4479"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/posts\/31437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/comments?post=31437"}],"version-history":[{"count":0,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/posts\/31437\/revisions"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/media?parent=31437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/categories?post=31437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/tags?post=31437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}