{"id":345,"date":"2025-06-05T21:15:09","date_gmt":"2025-06-05T13:15:09","guid":{"rendered":"https:\/\/www.xxiaoteng.cn\/?p=345"},"modified":"2026-05-11T21:21:55","modified_gmt":"2026-05-11T13:21:55","slug":"docker-compose%e7%bc%96%e6%8e%92","status":"publish","type":"post","link":"https:\/\/www.xxiaoteng.cn\/?p=345","title":{"rendered":"Docker-Compose\u7f16\u6392"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"toc-0\">\u4e00\u3001 Docker-Compose<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-1\">1.1\u3001 \u4ec0\u4e48\u662fDocker Compose<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compose \u9879\u76ee\u662f Docker \u5b98\u65b9\u7684\u5f00\u6e90\u9879\u76ee\uff0c\u8d1f\u8d23\u5b9e\u73b0 Docker \u5bb9\u5668\u96c6\u7fa4\u7684\u5feb\u901f\u7f16\u6392\uff0c\u5f00\u6e90\u4ee3\u7801\u5728 <strong><a href=\"https:\/\/github.com\/docker\/compose\">https:\/\/github.com\/docker\/compose<\/a><\/strong> \u4e0a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u77e5\u9053\u4f7f\u7528 Dockerfile \u6a21\u677f\u6587\u4ef6\u53ef\u4ee5\u8ba9\u7528\u6237\u5f88\u65b9\u4fbf\u7684\u5b9a\u4e49\u4e00\u4e2a\u5355\u72ec\u7684\u5e94\u7528\u5bb9\u5668\uff0c\u5176\u5b9e\u5728\u5de5\u4f5c\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u78b0\u5230\u9700\u8981\u591a\u4e2a\u5bb9\u5668\u76f8\u4e92\u914d\u5408\u6765\u5b8c\u6210\u7684\u67d0\u9879\u4efb\u52a1\u60c5\u51b5\uff0c\u4f8b\u5982\u5de5\u4f5c\u4e2d\u7684 web \u670d\u52a1\u5bb9\u5668\u672c\u8eab\uff0c\u5f80\u5f80\u4f1a\u5728\u540e\u7aef\u52a0\u4e0a\u6570\u636e\u5e93\u5bb9\u5668\uff0c\u751a\u81f3\u4f1a\u6709\u8d1f\u8d23\u5747\u8861\u5668\uff0c\u6bd4\u5982 LNMP \u670d\u52a1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Compose \u5c31\u662f\u6765\u505a\u8fd9\u4e2a\u4e8b\u60c5\u7684\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u4e00\u4e2a\u5355\u72ec\u7684 docker-compose.yml \u6a21\u677f\u6587\u4ef6 YAML\u683c\u5f0f \u6765\u5b9a\u4e49\u4e00\u7ec4\u76f8\u5173\u8054\u7684\u5e94\u7528\u5bb9\u5668\u4e3a\u4e00\u4e2a\u9879\u76ee project<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Compose \u4e2d\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u670d\u52a1 service :\u4e00\u4e2a\u5e94\u7528\u7684\u5bb9\u5668\uff0c\u5b9e\u9645\u4e0a\u53ef\u4ee5\u5305\u62ec\u82e5\u5e72\u8fd0\u884c\u76f8\u540c\u955c\u50cf\u7684\u5bb9\u5668\u5b9e\u4f8b<\/strong> <strong>\u9879\u76ee project :\u7531\u4e00\u7ec4\u5173\u8054\u7684\u5e94\u7528\u5bb9\u5668\u7ec4\u6210\u7684\u4e00\u4e2a\u5b8c\u6574\u4e1a\u52a1\u5355\u5143\uff0c\u5728docker-compose.yml\u4e2d\u5b9a\u4e49<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-2\">1.2\u3001 \u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># docker-compose\u7248\u672c\u9009\u62e9:https:\/\/github.com\/docker\/compose\/releases<br># curl -L https:\/\/github.com\/docker\/compose\/releases\/download\/1.25.4\/docker-compose-`uname -s`-`uname -m` -o \/usr\/local\/bin\/docker-compose<br># chmod +x \/usr\/local\/bin\/docker-compose<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-3\">1.3\u3001 \u547d\u4ee4<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">Compose \u5927\u90e8\u5206\u547d\u4ee4\u7684\u5bf9\u8c61\u5373\u53ef\u4ee5\u662f\u9879\u76ee\u7684\u672c\u8eab\uff0c\u4e5f\u53ef\u4ee5\u662f\u6307\u5b9a\u4e3a\u9879\u76ee\u4e2d\u7684\u670d\u52a1\u6216\u8005\u5bb9\u5668<br>\u6267\u884cdocker-compose [COMMAND] --help \u6216\u8005docker-compose help [COMMAND]\u53ef\u4ee5\u67e5\u770b\u547d\u4ee4\u7684\u5e2e\u52a9\u4fe1\u606f<br>\u5177\u4f53\u7684\u4f7f\u7528\u683c\u5f0f<br>docker-compose [-f=&lt;arg&gt;...] [options] [COMMAND] [ARGS]<br>\u53c2\u6570\u9009\u9879<br>-f,--file file\u6307\u5b9a\u6a21\u677f\u6587\u4ef6\uff0c\u9ed8\u8ba4\u662fdocker-compose.yml\u6a21\u677f\u6587\u4ef6,\u53ef\u4ee5\u591a\u6b21\u6307\u5b9a<br>-p,--project-name name\u6307\u5b9a\u9879\u76ee\u540d\u79f0\uff0c\u9ed8\u8ba4\u4f7f\u7528\u6240\u5728\u76ee\u5f55\u540d\u79f0\u4f5c\u4e3a\u9879\u76ee\u540d\u79f0<br>--x-networking \u4f7f\u7528Docker\u7684\u540e\u7aef\u53ef\u63d2\u62d4\u7f51\u7edc\u7279\u6027<br>--x-networking-driver driver\u6307\u5b9a\u7f51\u7edc\u7684\u540e\u7aef\u9a71\u52a8\uff0c\u9ed8\u8ba4\u4f7f\u7528bridge<br>--verbose \u8f93\u5165\u66f4\u591a\u7684\u8c03\u8bd5\u4fe1\u606f<br>-v,--version \u8f93\u51fa\u7248\u672c\u4fe1\u606f<br>\u200b<br>\u200b<br>Compose\u6240\u652f\u6301\u7684\u547d\u4ee4\uff1a<br>\u200b<br>build            \u6784\u5efa\u9879\u76ee\u4e2d\u7684\u670d\u52a1\u5bb9\u5668 <br>bundle &nbsp; &nbsp; \u4eceCompose\u6587\u4ef6\u751f\u6210\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u5305 <br>config &nbsp; &nbsp; \u9a8c\u8bc1\u5e76\u67e5\u770bCompose\u6587\u4ef6 <br>create &nbsp; &nbsp; \u4e3a\u670d\u52a1\u521b\u5efa\u5bb9\u5668 <br>down &nbsp; &nbsp; &nbsp; \u505c\u6b62\u5bb9\u5668\u5e76\u5220\u9664\u7531\u5176\u521b\u5efa\u7684\u5bb9\u5668\uff0c\u7f51\u7edc\uff0c\u5377\u548c\u56fe\u50cfup <br>events &nbsp; &nbsp; \u4e3a\u9879\u76ee\u4e2d\u7684\u6bcf\u4e2a\u5bb9\u5668\u6d41\u5f0f\u4f20\u8f93\u5bb9\u5668\u4e8b\u4ef6 <br>exec &nbsp; &nbsp; &nbsp; \u8fd9\u76f8\u5f53\u4e8edocker exec\u3002 <br>help &nbsp; &nbsp; &nbsp; \u83b7\u5f97\u4e00\u4e2a\u547d\u4ee4\u7684\u5e2e\u52a9 <br>kill &nbsp; &nbsp; &nbsp; \u901a\u8fc7\u53d1\u9001SIGKILL\u4fe1\u53f7\u6765\u5f3a\u5236\u505c\u6b62\u670d\u52a1\u5bb9\u5668 <br>logs &nbsp; &nbsp; &nbsp; \u67e5\u770b\u670d\u52a1\u5bb9\u5668\u7684\u8f93\u51fa <br>pause &nbsp; &nbsp;  \u6682\u505c\u4e00\u4e2a\u5bb9\u5668 <br>port &nbsp; &nbsp; &nbsp; \u6253\u5370\u67d0\u4e2a\u5bb9\u5668\u7aef\u53e3\u6240\u6620\u5c04\u7684\u516c\u5171\u7aef\u53e3 <br>ps &nbsp; &nbsp; &nbsp; &nbsp; \u5217\u51fa\u9879\u76ee\u4e2d\u76ee\u524d\u6240\u6709\u7684\u5bb9\u5668 <br>pull &nbsp; &nbsp; &nbsp; \u62c9\u53d6\u670d\u52a1\u4f9d\u8d56\u955c\u50cf <br>push &nbsp; &nbsp; &nbsp; \u63a8\u9001\u670d\u52a1\u955c\u50cf <br>restart &nbsp;  \u91cd\u542f\u9879\u76ee\u4e2d\u7684\u670d\u52a1 <br>rm &nbsp; &nbsp; &nbsp; &nbsp; \u5220\u9664\u6240\u6709\u505c\u6b62\u72b6\u6001\u7684\u670d\u52a1\u5bb9\u5668 <br>run &nbsp; &nbsp; &nbsp;  \u5728\u6307\u5b9a\u670d\u52a1\u4e0a\u6267\u884c\u4e00\u4e2a\u547d\u4ee4 <br>scale &nbsp; &nbsp;  \u8bbe\u7f6e\u6307\u5b9a\u670d\u52a1\u6267\u884c\u7684\u5bb9\u5668\u4e2a\u6570 <br>start &nbsp; &nbsp;  \u542f\u52a8\u5df2\u5b58\u5728\u7684\u670d\u52a1\u5bb9\u5668 <br>stop &nbsp; &nbsp; &nbsp; \u505c\u6b62\u5df2\u5b58\u5728\u7684\u670d\u52a1\u5bb9\u5668 <br>top &nbsp; &nbsp; &nbsp;  \u663e\u793a\u5bb9\u5668\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b <br>unpause &nbsp;  \u6062\u590d\u5904\u4e8e\u6682\u505c\u72b6\u6001\u7684\u5bb9\u5668 <br>up &nbsp; &nbsp; &nbsp; &nbsp; \u81ea\u52a8\u5b8c\u6210\u5305\u62ec\u6784\u5efa\u955c\u50cf\u3001\u521b\u5efa\u670d\u52a1\u3001\u542f\u52a8\u670d\u52a1\u5e76\u5173\u8054\u670d\u52a1\u76f8\u5173\u5bb9\u5668\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c <br>version &nbsp;  \u8f93\u51fa\u7248\u672c <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-4\">1.4\u3001\u6a21\u677f\u6587\u4ef6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6392\u7248\u95ee\u9898\uff0c\u8bf7\u770b\u5355\u72ec\u7684\u6587\u4ef6\u3002 \u5b98\u7f51\u94fe\u63a5\uff1a<a href=\"https:\/\/docs.docker.com\/compose\/compose-file\/#compose-file-structure-and-examples\">https:\/\/docs.docker.com\/compose\/compose-file\/#compose-file-structure-and-examples<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-5\">1.5\u3001 \u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u5177\u6709\u7684\u8bf4\u660e\u4e00\u4e0b Compose \u7684\u4f7f\u7528<br>\u200b<br>1. \u521b\u5efa\u4e00\u4e2a\u76ee\u5f55(\u91cc\u9762\u5305\u542b\u9700\u8981\u7684\u6587\u4ef6)<br>\u200b<br>[root@wing] mkdir compose-py<br>\u200b<br>2. \u521b\u5efa\u4e00\u4e2a Python \u5e94\u7528\uff0c \u4f7f\u7528 Flask \uff0c\u5c06\u6570\u503c\u8bb0\u5165 Redis<br>\u200b<br>[root@wing compose-py] cat app.py<br>\u200b<br>import time<br> <br>import redis<br>from flask import Flask<br> <br> <br>app = Flask(__name__)<br>cache = redis.Redis(host='redis', port=6379)<br> <br> <br>def get_hit_count():<br> &nbsp;  retries = 5<br> &nbsp;  while True:<br> &nbsp; &nbsp; &nbsp;  try:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  return cache.incr('hits')<br> &nbsp; &nbsp; &nbsp;  except redis.exceptions.ConnectionError as exc:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  if retries == 0:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  raise exc<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  retries -= 1<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  time.sleep(0.5)<br> <br> <br>@app.route('\/')<br>def hello():<br> &nbsp;  count = get_hit_count()<br> &nbsp;  return 'Hello World! I have been seen {} times.\\n'.format(count)<br> <br>if __name__ == \"__main__\":<br> &nbsp;  app.run(host=\"0.0.0.0\", debug=True)<br>\u200b<br>3. \u521b\u5efa requirements.txt \u6587\u4ef6\uff0c\u91cc\u9762\u662f\u9700\u8981\u5b89\u88c5\u7684 Python \u5305<br>\u200b<br>[root@wing compose-py] cat requirements.txt<br>flask<br>redis<br>\u200b<br>4. \u521b\u5efa Dockerfile \u6587\u4ef6<br>\u200b<br>[root@wing compose-py] cat Dockerfile<br>FROM python<br>ADD . \/code<br>WORKDIR \/code<br>RUN pip install -r requirements.txt<br>CMD [\"python\", \"app.py\"]<br> <br># \u8fd9\u544a\u8bc9Docker\uff1a<br> &nbsp;  \u4ece Python \u5f00\u59cb\u6784\u5efa\u955c\u50cf<br> &nbsp;  \u5c06\u5f53\u524d\u76ee\u5f55 . \u6dfb\u52a0\u5230 \/code \u955c\u50cf\u4e2d\u7684\u8def\u5f84<br> &nbsp;  \u5c06\u5de5\u4f5c\u76ee\u5f55\u8bbe\u7f6e\u4e3a \/code<br> &nbsp;  \u5b89\u88c5 Python \u4f9d\u8d56\u9879<br> &nbsp;  \u5c06\u5bb9\u5668\u7684\u9ed8\u8ba4\u547d\u4ee4\u8bbe\u7f6e\u4e3a python app.py<br> &nbsp; &nbsp;<br>5. \u521b\u5efa docker-compose.yml \u6587\u4ef6<br>\u200b<br>[root@wing compose-py] cat docker-compose.yml<br>version: '3'<br>services:<br>  web:<br> &nbsp;  build: .<br> &nbsp;  ports:<br> &nbsp; &nbsp; - \"5000:5000\"<br> &nbsp;  volumes:<br> &nbsp; &nbsp; - .:\/code<br>  redis:<br> &nbsp;  image: \"redis\"<br> <br>\u6b64 Compose \u6587\u4ef6\u5b9a\u4e49\u4e86\u4e24\u4e2a\u670d\u52a1\uff0cweb \u548c redis <br>\u8be5web\u670d\u52a1\uff1a<br>    \u4f7f\u7528\u4ece Dockerfile \u5f53\u524d\u76ee\u5f55\u4e2d\u6784\u5efa\u7684\u955c\u50cf<br>    \u5c06\u5bb9\u5668\u4e0a\u7684\u516c\u5f00\u7aef\u53e3 5000 \u8f6c\u53d1\u5230\u4e3b\u673a\u4e0a\u7684\u7aef\u53e3 5000 \u6211\u4eec\u4f7f\u7528 Flask Web \u670d\u52a1\u5668\u7684\u9ed8\u8ba4\u7aef\u53e3 5000<br>    \u8be5 redis \u670d\u52a1\u4f7f\u7528\u4ece Docker Hub \u4e2d\u63d0\u53d6\u7684\u516c\u5171 Redis \u6620\u50cf<br>    <br>6. \u4f7f\u7528 Compose \u6784\u5efa\u5e76\u8fd0\u884c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f<br>\u200b<br>[root@wing compose-py] docker-compose up<br>\u200b<br>7. \u6d4b\u8bd5\u8bbf\u95ee\uff0c\u5728\u6d4f\u89c8\u5668\u8bbf\u95ee IP:5000 \u6bcf\u5237\u65b0\u4e00\u6b21\u5c31\u4f1a\u52a0\u4e00<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001 Docker-Compose 1.1\u3001 \u4ec0\u4e48\u662fDocker Compose Compose \u9879\u76ee\u662f Do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-345","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts\/345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=345"}],"version-history":[{"count":1,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":346,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts\/345\/revisions\/346"}],"wp:attachment":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}