226 lines
477 KiB
HTML
226 lines
477 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html lang="zh"><head><title>Docker 存储</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 存储"/><meta property="og:description" content="Docker 存储."/><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 存储."/><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-存储"><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="0px" v
|
|||
|
<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>数据卷是一种用于持久化存储容器数据的机制。它可以将主机上的目录或文件挂载到容器中,从而实现数据在容器和主机之间的共享和持久化。数据卷的主要优势是容器与数据的解耦,使得容器可以在不同的主机上迁移和共享数据。</p>
|
|||
|
<p>数据卷的主要特点包括:</p>
|
|||
|
<ul>
|
|||
|
<li>数据卷是独立于容器的实体。即使容器被删除,数据卷仍然存在。</li>
|
|||
|
<li>数据卷可以被多个容器共享。这意味着多个容器可以访问和修改同一个数据卷中的数据。</li>
|
|||
|
<li>数据卷可以在容器之间进行传递。例如,可以将一个数据卷从一个容器挂载到另一个容器中,从而实现数据的共享和传递。</li>
|
|||
|
<li>数据卷可以在容器运行时进行挂载和卸载。这意味着可以在容器运行时动态地修改数据卷的挂载点。</li>
|
|||
|
</ul>
|
|||
|
<p>使用数据卷的步骤如下:</p>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p>创建一个数据卷:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> volume</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> create</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-volume</span></span></code></pre></figure>
|
|||
|
<p>这将创建一个名为 <code>my-volume</code> 的数据卷。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>将数据卷挂载到容器中:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> run</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-volume:/path/in/container</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-image</span></span></code></pre></figure>
|
|||
|
<p>这将把名为 <code>my-volume</code> 的数据卷挂载到容器中的 <code>/path/in/container</code> 目录。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>可以在多个容器中使用相同的数据卷:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> run</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-volume:/path/in/container</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> another-image</span></span></code></pre></figure>
|
|||
|
<p>这将把同一个名为 <code>my-volume</code> 的数据卷挂载到另一个容器中。</p>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<p>数据卷的生命周期管理包括创建、使用和删除。可以使用以下命令和操作来管理数据卷:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>创建一个数据卷:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> volume</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> create</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-volume</span></span></code></pre></figure>
|
|||
|
<p>这将创建一个名为 <code>my-volume</code> 的数据卷。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>将数据卷挂载到容器中:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> run</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-volume:/path/in/container</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-image</span></span></code></pre></figure>
|
|||
|
<p>这将把名为 <code>my-volume</code> 的数据卷挂载到容器中的 <code>/path/in/container</code> 目录。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>查看数据卷的详细信息:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> volume</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> inspect</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-volume</span></span></code></pre></figure>
|
|||
|
<p>这将显示关于数据卷的详细信息,包括挂载点、创建时间等。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>删除一个数据卷:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> volume</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> rm</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-volume</span></span></code></pre></figure>
|
|||
|
<p>这将删除名为 <code>my-volume</code> 的数据卷。请注意,只有在没有容器使用该数据卷时才能删除。</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<p>数据卷的备份和还原可以使用以下方法:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>备份数据卷:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> run</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --rm</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-volume:/data</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">pwd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">:/backup</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> busybox</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tar</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cvf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /backup/my-volume.tar</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /data</span></span></code></pre></figure>
|
|||
|
<p>这将创建一个名为 <code>my-volume.tar</code> 的备份文件,其中包含了 <code>my-volume</code> 数据卷中的所有数据。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>还原数据卷:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> run</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --rm</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-volume:/data</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">pwd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">:/backup</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> busybox</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tar</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> xvf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /backup/my-volume.tar</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -C</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /</span></span></code></pre></figure>
|
|||
|
<p>这将从备份文件 <code>my-volume.tar</code> 中还原数据到 <code>my-volume</code> 数据卷中。</p>
|
|||
|
</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>目录绑定是将主机上的目录直接映射到容器中的一种机制。通过目录绑定,容器可以直接访问和修改主机上的文件和目录。</p>
|
|||
|
<p>目录绑定的主要特点包括:</p>
|
|||
|
<ul>
|
|||
|
<li>目录绑定是容器和主机之间的直接映射关系。容器中的操作会直接影响主机上的文件和目录。</li>
|
|||
|
<li>目录绑定是一种静态的映射关系。一旦映射建立,容器运行期间无法修改映射关系。</li>
|
|||
|
<li>目录绑定是一对一的关系。每个容器只能与一个主机目录进行映射。</li>
|
|||
|
<li>目录绑定可以在容器创建时进行设置,也可以在容器运行时进行修改。但无论何时修改映射关系,都需要重新启动容器。</li>
|
|||
|
</ul>
|
|||
|
<p>使用目录绑定的步骤如下:</p>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p>将主机上的目录映射到容器中:</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> run</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /path/on/host:/path/in/container</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-image</span></span></code></pre></figure>
|
|||
|
<p>这将把主机上的 <code>/path/on/host</code> 目录映射到容器中的 <code>/path/in/container</code> 目录。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>容器中对映射的目录进行操作,将直接影响主机上的目录。</p>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<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 提供了几种方式来实现临时数据存储。</p>
|
|||
|
<p>临时数据的存储方式包括:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>临时文件系统(tmpfs):可以将一个临时文件系统挂载到容器中的某个目录,用于存储临时数据。这些数据在容器停止时会被删除。</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> run</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --rm</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -it</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --mount</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> type=tmpfs,destination=/data</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-image</span></span></code></pre></figure>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>临时性数据卷:可以创建一个临时性的数据卷,用于存储临时数据。这些数据卷在容器停止时会被删除。</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> run</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --rm</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -it</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /data</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-image</span></span></code></pre></figure>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<p>临时数据的清理与管理方法包括:</p>
|
|||
|
<ul>
|
|||
|
<li>定期清理任务:可以编写一个定期运行的清理任务,删除不再需要的临时数据。</li>
|
|||
|
<li>使用临时性数据卷:如果使用临时性数据卷存储临时数据,可以在容器停止时自动删除数据卷。</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>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td><code>docker volume create <volume_name></code></td><td>创建一个数据卷</td></tr><tr><td><code>docker volume ls</code></td><td>列出所有数据卷</td></tr><tr><td><code>docker volume inspect <volume_name></code></td><td>查看数据卷的详细信息</td></tr><tr><td><code>docker volume rm <volume_name></code></td><td>删除一个数据卷</td></tr><tr><td><code>docker run -v <volume_name>:<container_path> <image></code></td><td>将数据卷挂载到容器中</td></tr><tr><td><code>docker run -v <host_path>:<container_path> <image></code></td><td>将主机上的目录映射到容器中</td></tr><tr><td><code>docker run --mount type=tmpfs,destination=<container_path> <image></code></td><td>创建一个临时文件系统挂载到容器中</td></tr><tr><td><code>docker cp <container_id>:<container_path> <host_path></code></td><td>将容器中的文件或目录复制到主机上</td></tr><tr><td><code>docker run --rm -v <volume_name>:/data -v $(pwd):/backup busybox tar cvf /backup/<backup_name>.tar /data</code></td><td>备份数据卷中的数据到主机上的备份文件中</td></tr><tr><td><code>docker run --rm -v <volume_name>:/data -v $(pwd):/backup busybox tar xvf /backup/<backup_name>.tar -C /</code></td><td>从备份文件中还原数据到数据卷中</td></tr><tr><td><code>docker run --rm -it --mount type=tmpfs,destination=<container_path> <image></code></td><td>创建一个临时文件系统挂载到容器中,用于存储临时数据</td></tr><tr><td><code>docker run --rm -it -v /data <image></code></td><td>创建一个临时性数据卷,用于存储临时数据</td></tr></tbody></table></div>
|
|||
|
<p>请注意,上述命令中的 <code><volume_name></code>、<code><container_path></code>、<code><host_path></code>、<code><backup_name></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 还支持使用数据卷插件进行数据存储。数据卷插件可以提供更高级的存储功能,例如远程存储、分布式存储等。</p>
|
|||
|
<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 的一种官方插件类型,用于扩展 Docker 引擎的存储功能。存储驱动程序插件可以实现不同的存储后端,例如 Amazon EBS、GlusterFS、Ceph 等。</p>
|
|||
|
<p>使用存储驱动程序插件时,需要先安装插件并将其配置为 Docker 引擎的默认存储驱动程序。然后,可以使用标准的 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>除了官方的存储驱动程序插件,还有很多第三方数据卷插件可供选择。这些插件提供了各种不同的存储后端和功能,可以根据具体需求选择合适的插件。</p>
|
|||
|
<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>在 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>备份容器中的数据可以使用以下方法:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>使用 <code>docker cp</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</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cp</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> <</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">container_i</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">d</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">:/path/in/container</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /path/on/host</span></span></code></pre></figure>
|
|||
|
<p>这将把容器中的 <code>/path/in/container</code> 目录或文件复制到主机上的 <code>/path/on/host</code> 目录。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>使用数据卷进行备份:如果容器使用了数据卷,可以直接备份数据卷,或者将数据卷挂载到另一个容器中进行备份。</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<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>迁移容器中的数据到其他环境可以使用以下方法:</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p>使用数据卷进行迁移:如果容器使用了数据卷,可以将数据卷挂载到另一个容器中,然后在新环境中运行该容器,从而迁移数据。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>使用容器快照进行迁移:Docker 提供了容器快照功能,可以将容器的状态和数据保存为一个快照文件,然后在其他环境中加载该快照文件,从而迁移数据。</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<p>以上是关于 Docker 数据存储的详细介绍和示例。根据不同的需求和场景,可以选择合适的数据存储机制来管理容器中的数据。</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-0"><a href="#临时数据" data-for="临时数据">临时数据</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-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></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","collapsed":true},{"path":"Personal/Blog","collapsed":true},{"path":"Personal/Blog/2018","collapsed":true},{"path":"Personal/Blog/2020","collapsed":true},{"path":"Personal/Blog/2021","collapsed":true},{"path":"Personal/Blog/2022","collapsed":true},{"path":"Personal/Blog/2023","collapsed":true},{"path":"Personal/Blog/2024","collapsed":true},{"path":"Personal/Book","collapsed":true},{"path":"Personal/Book/个人成长","collapsed":true},{"path":"Personal/Book/医学健康","collapsed":true},{"path":"Personal/Book/历史","collapsed":true},{"path":"Personal/Book/哲学宗教","collapsed":true},{"path":"Personal/Book/心理","collapsed":true},{"path":"Personal/Book/政治军事","collapsed":true},{"path":"Personal/Book/教育学习","collapsed":true},{"path":"Personal/Book/文学","collapsed":true},{"path":"Personal/Book/生活百科","collapsed":true},{"path":"Personal/Book/社会文化","collapsed":true},{"path":"Personal/Book/科学技术","collapsed":true},{"path":"Personal/Book/经济理财","collapsed":true},{"path":"Personal/Book/艺术","collapsed":true},{"path":"Personal/Book/计算机","collapsed"
|
|||
|
</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>
|