707 lines
547 KiB
HTML
707 lines
547 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html lang="zh"><head><title>Docker Compose</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&family=Noto Serif Simplified Chinese:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="Docker Compose"/><meta property="og:description" content="Docker Compose."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="Docker Compose."/><meta name="generator" content="Quartz"/><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/OperatingSystem/Docker/进阶/Docker-Compose"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0
|
|||
|
<p>Docker Compose 是一种工具,用于定义和运行多容器 Docker 应用程序。通过使用 Compose,你可以使用 YAML 文件来配置你的应用服务。然后,只需一个简单的命令,就能创建并启动你配置中的所有服务。</p>
|
|||
|
<p>Docker Compose 允许你使用 YAML 文件来定义多个容器的应用服务,包括网络、卷等其他资源。这种方法可以让你将整个应用的配置集中管理,极大地简化了容器管理过程。</p>
|
|||
|
<ul>
|
|||
|
<li><strong>简化配置</strong>:使用 YAML 文件定义服务,使得配置过程更加简单明了。</li>
|
|||
|
<li><strong>一键部署</strong>:多容器应用可以通过一个命令同时启动,无需分别操作每个容器。</li>
|
|||
|
<li><strong>易于维护和扩展</strong>:服务的更新和扩展可以通过修改配置文件实现,易于管理。</li>
|
|||
|
</ul>
|
|||
|
<h2 id="安装">安装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#安装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>Docker Compose 的安装过程取决于你的操作系统。在大多数情况下,它可以作为 Docker Desktop 的一部分自动安装,或者可以单独安装。</p>
|
|||
|
<h3 id="在-linux-上安装">在 Linux 上安装<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在-linux-上安装" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">curl</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -L</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "https://github.com/docker/compose/releases/download/v2.24.6/docker-compose-linux-x86_64"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /usr/local/bin/docker-compose</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> chmod</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> +x</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /usr/local/bin/docker-compose</span></span></code></pre></figure>
|
|||
|
<h2 id="基本命令">基本命令<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基本命令" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td><code>docker-compose up</code></td><td>构建、(重新)创建、启动和连接到服务的容器。使用 <code>-d</code> 参数以后台模式运行。</td></tr><tr><td><code>docker-compose down</code></td><td>停止并移除容器、网络、卷和镜像。</td></tr><tr><td><code>docker-compose build</code></td><td>构建或重新构建服务中定义的镜像。</td></tr><tr><td><code>docker-compose logs</code></td><td>查看服务的日志输出。</td></tr><tr><td><code>docker-compose pull</code></td><td>拉取服务依赖的镜像。</td></tr><tr><td><code>docker-compose push</code></td><td>将服务镜像推送到 Docker Hub 或其他镜像仓库。</td></tr><tr><td><code>docker-compose restart</code></td><td>重启服务。</td></tr><tr><td><code>docker-compose start</code></td><td>启动已经存在的服务容器。</td></tr><tr><td><code>docker-compose stop</code></td><td>停止运行中的容器,不移除它们。</td></tr><tr><td><code>docker-compose pause</code></td><td>暂停服务中的容器。</td></tr><tr><td><code>docker-compose unpause</code></td><td>恢复服务中已暂停的容器。</td></tr><tr><td><code>docker-compose rm</code></td><td>删除所有(停止状态的)服务容器。</td></tr><tr><td><code>docker-compose run</code></td><td>在一个服务上运行一次性命令。</td></tr><tr><td><code>docker-compose exec</code></td><td>在服务的容器中执行命令。</td></tr><tr><td><code>docker-compose scale</code></td><td>设置服务的容器数量。<em>(注:在 3.x 版本中已被 <code>docker-compose up --scale</code> 代替)</em></td></tr><tr><td><code>docker-compose config</code></td><td>验证并查看 Compose 文件的配置。</td></tr><tr><td><code>docker-compose top</code></td><td>显示运行中的容器的进程。</td></tr><tr><td><code>docker-compose port</code></td><td>打印绑定的公开端口。</td></tr><tr><td><code>docker-compose ps</code></td><td>列出项目中目前的所有容器。</td></tr><tr><td><code>docker-compose version</code></td><td>显示 Docker Compose 的版本信息。</td></tr></tbody></table></div>
|
|||
|
<p>如果你想操作特定的一个 Docker Compose 编排,你应该在该编排文件所在的目录下执行相应的 <code>docker-compose </code> 命令,并使用 <code>-f</code> 参数指定你的编排文件(如果不是使用默认的 <code>docker-compose.yml</code> 文件名)。</p>
|
|||
|
<h2 id="管理应用">管理应用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#管理应用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>让我们通过一个简单的示例来展示如何使用 Docker Compose 管理多容器应用。</p>
|
|||
|
<h3 id="示例应用">示例应用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#示例应用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>下面创建一个 <code>docker-compose.yml</code> 文件,定义一个简单的 web 应用服务。</p>
|
|||
|
<h4 id="apppy">app.py<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#apppy" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> flask </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Flask</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> redis </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Redis</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">app </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Flask(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">__name__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">redis </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Redis(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">host</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'redis'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">port</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">6379</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">@app.route</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'/'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> hello</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">():</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> count </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> redis.incr(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'hits'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'Hello World! 该页面已被访问 </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">{}</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 次。</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.format(count)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">if</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __name__</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ==</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "__main__"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> app.run(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">host</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"0.0.0.0"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">debug</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">True</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre></figure>
|
|||
|
<h4 id="dockerfile">Dockerfile<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#dockerfile" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="dockerfile" data-theme="github-light github-dark"><code data-language="dockerfile" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">FROM</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> python:3.6-alpine</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">ADD</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> . /code</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">WORKDIR</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /code</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">RUN</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> pip install redis flask</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">CMD</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"python"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"app.py"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span></code></pre></figure>
|
|||
|
<h4 id="docker-composeyml">docker-compose.yml<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#docker-composeyml" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> web</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">.</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"5000:5000"</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> redis</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"redis:alpine"</span></span></code></pre></figure>
|
|||
|
<h3 id="启动应用">启动应用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#启动应用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>使用 <code>docker-compose up</code> 命令来启动应用,Docker Compose 会自动启动定义的所有服务。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">docker-compose</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> up</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -d</span></span></code></pre></figure>
|
|||
|
<h3 id="停止应用">停止应用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#停止应用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>当你完成工作后,可以使用 <code>docker-compose down</code> 命令来停止并清理应用服务。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">docker-compose</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> down</span></span></code></pre></figure>
|
|||
|
<h2 id="compose-文件">Compose 文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#compose-文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>Docker Compose 文件是 Docker Compose 的核心,它使用 YAML 文件格式定义了多容器 Docker 应用的所有服务、网络和卷。</p>
|
|||
|
<h3 id="文件结构">文件结构<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#文件结构" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>一个基本的 <code>docker-compose.yml</code> 文件包含<strong>三个主要部分:services(服务)、networks(网络)和 volumes(卷)。</strong></p>
|
|||
|
<p>下面是一个简单的示例,展示了这些组件如何被定义和关联:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> web</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">nginx</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"80:80"</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> depends_on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">db</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">backend</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> db</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">postgres</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> POSTGRES_PASSWORD</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mysecretpassword</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">db-data:/var/lib/postgresql/data</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">backend</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> backend</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> db-data</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span></code></pre></figure>
|
|||
|
<h4 id="服务services">服务(Services)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#服务services" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>在 <code>services</code> 部分,你定义了应用中的各个服务,每个服务可以是一个容器,在上面的例子中,有两个服务:<code>web</code> 和 <code>db</code>。</p>
|
|||
|
<ul>
|
|||
|
<li><strong>image</strong>: 指定服务使用的镜像。</li>
|
|||
|
<li><strong>ports</strong>: 映射端口到宿主机。</li>
|
|||
|
<li><strong>depends_on</strong>: 表示服务之间的依赖关系。</li>
|
|||
|
<li><strong>networks</strong>: 指定服务连接的网络。</li>
|
|||
|
<li><strong>environment</strong>: 设置环境变量。</li>
|
|||
|
</ul>
|
|||
|
<h4 id="网络networks">网络(Networks)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#网络networks" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>在 <code>networks</code> 部分,你可以定义一个或多个网络,服务可以连接到这些网络。在上例中,定义了一个名为 <code>backend</code> 的网络,<code>web</code> 和 <code>db</code> 服务都连接到了这个网络,使得它们可以相互通信。</p>
|
|||
|
<h4 id="卷volumes">卷(Volumes)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#卷volumes" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>在 <code>volumes</code> 部分,你定义了数据卷用于数据持久化。在上例中,<code>db-data</code> 卷被挂载到了 <code>db</code> 服务的容器中,用于存储数据库数据。</p>
|
|||
|
<h3 id="语法">语法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#语法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<pre><code class="mermaid">graph LR
|
|||
|
A[Docker Compose] --> B[Services]
|
|||
|
A --> C[Volumes]
|
|||
|
A --> D[Networks]
|
|||
|
A --> E[Configs]
|
|||
|
A --> F[Secrets]
|
|||
|
|
|||
|
B --> G[Service 1]
|
|||
|
B --> H[Service 2]
|
|||
|
B --> I[Service N]
|
|||
|
|
|||
|
G --> J[Image/Build]
|
|||
|
G --> K[Environment]
|
|||
|
G --> L[Ports]
|
|||
|
G --> M[Depends On]
|
|||
|
G --> N[Volumes]
|
|||
|
G --> O[Networks]
|
|||
|
|
|||
|
C --> P[Volume 1]
|
|||
|
C --> Q[Volume 2]
|
|||
|
C --> R[Volume N]
|
|||
|
|
|||
|
D --> S[Network 1]
|
|||
|
D --> T[Network 2]
|
|||
|
D --> U[Network N]
|
|||
|
|
|||
|
E --> V[Config 1]
|
|||
|
E --> W[Config 2]
|
|||
|
|
|||
|
F --> X[Secret 1]
|
|||
|
F --> Y[Secret 2]
|
|||
|
|
|||
|
J --> J1[Type: Image or Build Path]
|
|||
|
K --> K1[Variables]
|
|||
|
L --> L1[Container:Host]
|
|||
|
M --> M1[Service Dependencies]
|
|||
|
N --> N1[Service:Volume Mapping]
|
|||
|
O --> O1[Service Networks]
|
|||
|
|
|||
|
P --> P1[Driver]
|
|||
|
Q --> Q1[Driver Options]
|
|||
|
|
|||
|
S --> S1[Driver]
|
|||
|
T --> T1[Driver Options]
|
|||
|
|
|||
|
V --> V1[File or External]
|
|||
|
W --> W1[File or External]
|
|||
|
|
|||
|
X --> X1[File or External]
|
|||
|
Y --> Y1[File or External]
|
|||
|
</code></pre>
|
|||
|
<h3 id="关键字">关键字<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#关键字" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>配置项</th><th>描述</th></tr></thead><tbody><tr><td><code>build</code></td><td>定义了构建服务的配置,可以是一个构建上下文的路径,或者一个包含 <code>context</code>、<code>dockerfile</code> 和 <code>args</code> 的对象。</td></tr><tr><td><code>cap_add</code>, <code>cap_drop</code></td><td>添加或删除容器的能力。</td></tr><tr><td><code>cgroup_parent</code></td><td>指定容器的父 cgroup。</td></tr><tr><td><code>command</code></td><td>覆盖容器的默认命令。</td></tr><tr><td><code>configs</code></td><td>为服务提供对配置的访问。</td></tr><tr><td><code>container_name</code></td><td>指定自定义容器名称。</td></tr><tr><td><code>credential_spec</code></td><td>配置管理服务帐户的凭据规范。</td></tr><tr><td><code>depends_on</code></td><td>表达服务之间的依赖关系。</td></tr><tr><td><code>deploy</code></td><td>指定与服务部署和运行相关的配置。</td></tr><tr><td><code>devices</code></td><td>设备映射列表。</td></tr><tr><td><code>dns</code>, <code>dns_search</code></td><td>自定义 DNS 服务器和搜索域。</td></tr><tr><td><code>entrypoint</code></td><td>覆盖容器的默认入口点。</td></tr><tr><td><code>env_file</code></td><td>从文件中加载环境变量。</td></tr><tr><td><code>environment</code></td><td>设置环境变量。</td></tr><tr><td><code>expose</code></td><td>暴露端口而不发布到宿主机。</td></tr><tr><td><code>external_links</code></td><td>链接到 Docker Compose 外部的容器。</td></tr><tr><td><code>extra_hosts</code></td><td>添加主机名映射。</td></tr><tr><td><code>healthcheck</code></td><td>配置容器的健康检查。</td></tr><tr><td><code>image</code></td><td>指定服务使用的镜像。</td></tr><tr><td><code>init</code></td><td>使用 Docker 的 init 进程。</td></tr><tr><td><code>labels</code></td><td>添加标签到容器。</td></tr><tr><td><code>links</code></td><td>链接到其他服务的容器。</td></tr><tr><td><code>logging</code></td><td>配置日志记录。</td></tr><tr><td><code>network_mode</code></td><td>网络模式。</td></tr><tr><td><code>networks</code></td><td>配置网络。</td></tr><tr><td><code>pid</code></td><td>PID 模式。</td></tr><tr><td><code>ports</code></td><td>发布端口。</td></tr><tr><td><code>secrets</code></td><td>配置访问秘密。</td></tr><tr><td><code>security_opt</code></td><td>安全选项。</td></tr><tr><td><code>stop_grace_period</code></td><td>设置停止前的等待时间。</td></tr><tr><td><code>stop_signal</code></td><td>设置停止容器的信号。</td></tr><tr><td><code>sysctls</code></td><td>内核参数设置。</td></tr><tr><td><code>tmpfs</code></td><td>挂载临时文件系统。</td></tr><tr><td><code>ulimits</code></td><td>用户限制。</td></tr><tr><td><code>user</code></td><td>指定运行用户。</td></tr><tr><td><code>userns_mode</code></td><td>用户命名空间模式。</td></tr><tr><td><code>volumes</code>, <code>volume_driver</code></td><td>配置卷。</td></tr><tr><td><code>volumes_from</code></td><td>从其他服务或容器挂载卷。</td></tr><tr><td><code>working_dir</code></td><td>工作目录。</td></tr></tbody></table></div>
|
|||
|
<h2 id="服务配置详解">服务配置详解<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#服务配置详解" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<h3 id="构建选项">构建选项<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#构建选项" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>如果你不是使用现有的镜像,而是需要构建自定义镜像,可以使用 <code>build</code> 选项:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> webapp</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">./dir</span></span></code></pre></figure>
|
|||
|
<h3 id="环境变量">环境变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#环境变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>你可以直接在 <code>docker-compose.yml</code> 文件中为服务设置环境变量,或者使用 <code>.env</code> 文件来管理:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> web</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">nginx</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">NGINX_PORT=80</span></span></code></pre></figure>
|
|||
|
<h3 id="依赖关系">依赖关系<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#依赖关系" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>使用 <code>depends_on</code> 选项可以定义服务启动的先后顺序:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> web</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">nginx</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> depends_on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">db</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> db</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">postgres</span></span></code></pre></figure>
|
|||
|
<h3 id="端口映射">端口映射<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#端口映射" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>通过 <code>ports</code> 选项,可以将容器内的端口映射到宿主机的端口:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> web</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">nginx</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"80:80"</span></span></code></pre></figure>
|
|||
|
<h3 id="环境变量与-env-文件的使用">环境变量与 .env 文件的使用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#环境变量与-env-文件的使用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>管理配置和敏感信息时,推荐使用 <code>.env</code> 文件来外部定义环境变量,然后在 <code>docker-compose.yml</code> 文件中引用这些变量:</p>
|
|||
|
<p>.env 文件:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="env" data-theme="github-light github-dark"><code data-language="env" data-theme="github-light github-dark" style="display:grid;"><span data-line><span>DB_PASSWORD=mysecretpassword</span></span></code></pre></figure>
|
|||
|
<p>docker-compose.yml 文件:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> db</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">postgres</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> POSTGRES_PASSWORD</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${DB_PASSWORD}</span></span></code></pre></figure>
|
|||
|
<p>通过这种方式,你可以避免将敏感信息直接硬编码在 <code>docker-compose.yml</code> 文件中,而是将其存储在外部的 <code>.env</code> 文件中,这有助于保持你的配置的安全性和灵活性。</p>
|
|||
|
<h2 id="多容器应用管理">多容器应用管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#多容器应用管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>在 Docker Compose 的使用中,管理多容器应用是核心任务之一。这包括了如何定义和运行多容器应用、如何管理容器间的网络连接以及如何实现数据的持久化和共享。</p>
|
|||
|
<h3 id="定义与运行多容器应用">定义与运行多容器应用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#定义与运行多容器应用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>利用 <code>docker-compose.yml</code> 文件,开发者可以定义涵盖多个服务(容器)的完整应用架构,实现一键部署和管理。</p>
|
|||
|
<p>考虑到一个典型的三层应用架构,包含前端、后端及数据库层:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3.8'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> frontend</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">nginx:latest</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"80:80"</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> depends_on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">backend</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">app-network</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> backend</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">node:14</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> DB_HOST</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">db</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"3000:3000"</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> depends_on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">db</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">app-network</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> db</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">postgres:13</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> POSTGRES_USER</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">user</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> POSTGRES_PASSWORD</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">password</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">db-data:/var/lib/postgresql/data</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">app-network</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> db-data</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> app-network</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span></code></pre></figure>
|
|||
|
<p>在这个例子中,我们指定了使用的镜像、环境变量、端口映射、依赖关系、网络和数据卷。通过这样的配置,可以确保应用的各个部分能够正确连接和交互,同时数据也得到了持久化。</p>
|
|||
|
<h3 id="网络管理">网络管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#网络管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>Docker Compose 默认创建一个网络,使得同一 <code>docker-compose.yml</code> 文件中定义的所有服务都能够在这个网络中相互通信。然而,复杂应用可能需要更精细的网络配置来满足不同的安全和隔离需求。</p>
|
|||
|
<p>自定义网络配置允许服务根据实际需求分配到不同的网络中,实现更细致的网络隔离和通信策略。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> app-network</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> driver</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">bridge</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> internal-network</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> driver</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">bridge</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> internal</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span></code></pre></figure>
|
|||
|
<p>在上述配置中,<code>app-network</code> 用于暴露外部可访问的服务(如前端),而 <code>internal-network</code> 则用于内部服务间的通信,不对外部暴露,增强了安全性。</p>
|
|||
|
<h3 id="数据卷与持久化">数据卷与持久化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#数据卷与持久化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>数据持久化对于任何生产级应用都至关重要,Docker Compose 通过卷(volumes)提供了数据持久化的能力。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> db-data</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> driver</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">local</span></span></code></pre></figure>
|
|||
|
<p>这里定义了一个名为 <code>db-data</code> 的卷,用于 PostgreSQL 数据库的数据持久化存储。通过指定卷,即使容器重新创建,数据也不会丢失。</p>
|
|||
|
<p>Docker Compose 也支持定义卷来实现服务之间的数据共享:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> service1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">shared-data:/path/to/data</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> service2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">shared-data:/path/to/data</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> shared-data</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span></code></pre></figure>
|
|||
|
<p>在此配置中,<code>service1</code> 和 <code>service2</code> 共享了同一个卷 <code>shared-data</code>,允许它们访问和修改相同的数据集,这在需要数据共享的应用场景中非常有用。</p>
|
|||
|
<h2 id="高级功能">高级功能<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#高级功能" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>Docker Compose 不仅仅是一个多容器部署工具,它还提供了一系列高级功能和最佳实践,帮助开发者和运维人员优化应用配置和管理。本章节将深入探讨服务的健康检查、如何使用 <code>extends</code> 特性以及如何通过覆盖文件分离环境配置。</p>
|
|||
|
<h3 id="服务的健康检查">服务的健康检查<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#服务的健康检查" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>健康检查是监控服务状态和健康状况的重要手段。通过配置健康检查,Docker 可以自动检测服务是否正常运行。</p>
|
|||
|
<p>在 <code>docker-compose.yml</code> 文件中,可以为服务配置 <code>healthcheck</code> 指令:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> web</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">my-web-app</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> healthcheck</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> test</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"CMD"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"curl"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"-f"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"http://localhost"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> interval</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">30s</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> timeout</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">10s</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> retries</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> start_period</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">40s</span></span></code></pre></figure>
|
|||
|
<p>这个配置定义了一个健康检查,每 30 秒执行一次 <code>curl -f http://localhost</code> 命令来检查 <code>web</code> 服务的健康状态,如果命令在 10 秒内没有成功执行(即返回状态码非 0),则认为是一次失败。如果连续 3 次检查失败,则服务被认为是不健康的。</p>
|
|||
|
<h3 id="扩展与重写服务">扩展与重写服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#扩展与重写服务" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p><code>extends</code> 特性允许在一个服务中重用另一个服务的配置。这对于不同环境下的配置共享非常有用。</p>
|
|||
|
<p>假设有一个基础服务配置 <code>base-service.yml</code>:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3.8'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> app_base</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">my-app</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">DEBUG=false</span></span></code></pre></figure>
|
|||
|
<p>可以在 <code>docker-compose.yml</code> 中扩展这个服务:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3.8'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> app</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> extends</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> file</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">base-service.yml</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> service</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">app_base</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"80:80"</span></span></code></pre></figure>
|
|||
|
<p>通过这种方式,<code>app</code> 服务继承了 <code>app_base</code> 的所有配置,并添加了端口映射。</p>
|
|||
|
<h3 id="使用覆盖文件来分离环境配置">使用覆盖文件来分离环境配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用覆盖文件来分离环境配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>Docker Compose 允许使用多个文件来定义项目配置,这使得可以针对不同环境(如开发、测试、生产)使用不同的配置。</p>
|
|||
|
<p>基础 <code>docker-compose.yml</code> 文件定义了所有环境共有的配置:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3.8'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> web</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">my-web-app</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">LOG_LEVEL=info</span></span></code></pre></figure>
|
|||
|
<p>针对开发环境的 <code>docker-compose.override.yml</code>:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3.8'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> web</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">DEBUG=true</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">.:/code</span></span></code></pre></figure>
|
|||
|
<p>在生产环境的 <code>docker-compose.prod.yml</code>:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3.8'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> web</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"80:80"</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">LOG_LEVEL=warning</span></span></code></pre></figure>
|
|||
|
<p>通过指定 <code>-f</code> 参数来使用不同的配置文件:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d</span></span></code></pre></figure>
|
|||
|
<p>这种方法使得基础配置与环境特定配置分离,便于管理和维护。</p>
|
|||
|
<h2 id="实战项目">实战项目<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#实战项目" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>本实战项目将引导你构建一个简单的 Web 应用,该应用包含三个主要组件:前端(使用 Nginx 静态文件服务),后端(一个简单的 Node.js API),以及数据库(PostgreSQL)。此外,我们还将探讨如何调试这些服务并进行基本的性能优化。</p>
|
|||
|
<h3 id="web-实战项目">Web 实战项目<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#web-实战项目" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<h4 id="项目概述">项目概述<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#项目概述" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<ul>
|
|||
|
<li><strong>前端</strong>:使用 Nginx 服务静态文件。</li>
|
|||
|
<li><strong>后端</strong>:Node.js 应用提供 RESTful API。</li>
|
|||
|
<li><strong>数据库</strong>:PostgreSQL 存储数据。</li>
|
|||
|
</ul>
|
|||
|
<h4 id="docker-composeyml-1"><code>docker-compose.yml</code><a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#docker-composeyml-1" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'3.8'</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> frontend</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">nginx:alpine</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">./frontend:/usr/share/nginx/html</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"80:80"</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> depends_on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">backend</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">app-network</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> backend</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> context</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">./backend</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> dockerfile</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Dockerfile</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> DATABASE_URL</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">postgres://user:password@db:5432/mydb</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"3000:3000"</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> depends_on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">db</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">app-network</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> db</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">postgres:13</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> POSTGRES_USER</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">user</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> POSTGRES_PASSWORD</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">password</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> POSTGRES_DB</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mydb</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">db-data:/var/lib/postgresql/data</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">app-network</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">volumes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> db-data</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">networks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> app-network</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span></code></pre></figure>
|
|||
|
<h4 id="前端">前端<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#前端" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>前端使用 Nginx 静态文件服务。你需要在 <code>./frontend</code> 目录下放置你的静态文件(HTML、CSS、JavaScript 文件等)。</p>
|
|||
|
<h4 id="后端">后端<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#后端" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>后端是一个简单的 Node.js 应用,提供 RESTful API。你需要在 <code>./backend</code> 目录下创建一个 <code>Dockerfile</code> 和你的 Node.js 应用代码。</p>
|
|||
|
<p><code>./backend/Dockerfile</code></p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">FROM node:14-alpine</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">WORKDIR /app</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">COPY package*.json ./</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">RUN npm install</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">COPY . .</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">EXPOSE 3000</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">CMD ["node", "server.js"]</span></span></code></pre></figure>
|
|||
|
<h4 id="nodejs-服务器">Node.js 服务器<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#nodejs-服务器" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="javascript" data-theme="github-light github-dark"><code data-language="javascript" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// server.js</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> express</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> require</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'express'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> app</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> express</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> PORT</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> process.env.</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">PORT</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ||</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3000</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">app.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">get</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'/api'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, (</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">req</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">res</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> res.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">json</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">({ message: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello from the backend!"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> });</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">});</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">app.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">listen</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">PORT</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, () </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> console.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">log</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">`Server is running on port ${</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">PORT</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">}`</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">});</span></span></code></pre></figure>
|
|||
|
<h4 id="数据库">数据库<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#数据库" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>使用 PostgreSQL 作为数据库服务。<code>docker-compose.yml</code> 文件中已经配置了必要的环境变量。</p>
|
|||
|
<h3 id="调试与优化">调试与优化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#调试与优化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<h4 id="容器日志查看">容器日志查看<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#容器日志查看" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>使用 Docker Compose 查看服务日志的命令:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker-compose logs -f backend</span></span></code></pre></figure>
|
|||
|
<p>这将跟踪并显示后端服务的实时日志输出。</p>
|
|||
|
<h4 id="资源监控与性能优化">资源监控与性能优化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#资源监控与性能优化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>Docker Desktop 和其他第三方工具如 Portainer 或 cAdvisor 可用于监控容器的资源使用情况。基于监控数据,你可以对服务进行必要的调整,比如调整容器的 CPU 和内存限制:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> backend</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">./backend</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> mem_limit</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">500m</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> cpus</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'0.5'</span></span></code></pre></figure>
|
|||
|
<p>在 <code>docker-compose.yml</code> 文件中,为后端服务设置了内存限制为 500MB,CPU 使用限制为 50%。</p></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
|
|||
|
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
|
|||
|
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
|
|||
|
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
|
|||
|
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
|
|||
|
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
|
|||
|
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
|
|||
|
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
|
|||
|
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
|
|||
|
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
|
|||
|
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#概述" data-for="概述">概述</a></li><li class="depth-0"><a href="#安装" data-for="安装">安装</a></li><li class="depth-1"><a href="#在-linux-上安装" data-for="在-linux-上安装">在 Linux 上安装</a></li><li class="depth-0"><a href="#基本命令" data-for="基本命令">基本命令</a></li><li class="depth-0"><a href="#管理应用" data-for="管理应用">管理应用</a></li><li class="depth-1"><a href="#示例应用" data-for="示例应用">示例应用</a></li><li class="depth-1"><a href="#启动应用" data-for="启动应用">启动应用</a></li><li class="depth-1"><a href="#停止应用" data-for="停止应用">停止应用</a></li><li class="depth-0"><a href="#compose-文件" data-for="compose-文件">Compose 文件</a></li><li class="depth-1"><a href="#文件结构" data-for="文件结构">文件结构</a></li><li class="depth-1"><a href="#语法" data-for="语法">语法</a></li><li class="depth-1"><a href="#关键字" data-for="关键字">关键字</a></li><li class="depth-0"><a href="#服务配置详解" data-for="服务配置详解">服务配置详解</a></li><li class="depth-1"><a href="#构建选项" data-for="构建选项">构建选项</a></li><li class="depth-1"><a href="#环境变量" data-for="环境变量">环境变量</a></li><li class="depth-1"><a href="#依赖关系" data-for="依赖关系">依赖关系</a></li><li class="depth-1"><a href="#端口映射" data-for="端口映射">端口映射</a></li><li class="depth-1"><a href="#环境变量与-env-文件的使用" data-for="环境变量与-env-文件的使用">环境变量与 .env 文件的使用</a></li><li class="depth-0"><a href="#多容器应用管理" data-for="多容器应用管理">多容器应用管理</a></li><li class="depth-1"><a href="#定义与运行多容器应用" data-for="定义与运行多容器应用">定义与运行多容器应用</a></li><li class="depth-1"><a href="#网络管理" data-for="网络管理">网络管理</a></li><li class="depth-1"><a href="#数据卷与持久化" data-for="数据卷与持久化">数据卷与持久化</a></li><li class="depth-0"><a href="#高级功能" data-for="高级功能">高级功能</a></li><li class="depth-1"><a href="#服务的健康检查" data-for="服务的健康检查">服务的健康检查</a></li><li class="depth-1"><a href="#扩展与重写服务" data-for="扩展与重写服务">扩展与重写服务</a></li><li class="depth-1"><a href="#使用覆盖文件来分离环境配置" data-for="使用覆盖文件来分离环境配置">使用覆盖文件来分离环境配置</a></li><li class="depth-0"><a href="#实战项目" data-for="实战项目">实战项目</a></li><li class="depth-1"><a href="#web-实战项目" data-for="web-实战项目">Web 实战项目</a></li><li class="depth-1"><a href="#调试与优化" data-for="调试与优化">调试与优化</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{"path":"Personal"
|
|||
|
</script><script type="module">
|
|||
|
let mermaidImport = undefined
|
|||
|
document.addEventListener('nav', async () => {
|
|||
|
if (document.querySelector("code.mermaid")) {
|
|||
|
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
|
|||
|
const mermaid = mermaidImport.default
|
|||
|
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
|
|||
|
mermaid.initialize({
|
|||
|
startOnLoad: false,
|
|||
|
securityLevel: 'loose',
|
|||
|
theme: darkMode ? 'dark' : 'default'
|
|||
|
})
|
|||
|
|
|||
|
await mermaid.run({
|
|||
|
querySelector: '.mermaid'
|
|||
|
})
|
|||
|
}
|
|||
|
});
|
|||
|
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>
|