2024-08-15 16:26:43 +09:00

195 lines
9.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Dich&#x27;blog</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<meta name="robots" content="noodp"/>
<link rel="stylesheet" href="https://blog.dich.bid/style.css">
<link rel="stylesheet" href="https://blog.dich.bid/color/blue.css">
<link rel="stylesheet" href="https://blog.dich.bid/color/background_dark.css">
<link rel="stylesheet" href="https://blog.dich.bid/font-hack-subset.css">
<meta name="description" content="">
<meta property="og:description" content="">
<meta property="og:title" content="Dich'blog">
<meta property="og:type" content="article">
<meta property="og:url" content="https://blog.dich.bid/docker-learn-4/">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:description" content="">
<meta name="twitter:title" content="Dich'blog">
<meta property="twitter:domain" content="blog.dich.bid">
<meta property="twitter:url" content="https://blog.dich.bid/docker-learn-4/">
<link rel="alternate" type="application/atom+xml" title="Dich&#x27;blog Atom Feed" href="https://blog.dich.bid/atom.xml" />
</head>
<body class="">
<div class="container">
<header class="header">
<div class="header__inner">
<div class="header__logo">
<a href="https://blog.dich.bid" style="text-decoration: none;">
<div class="logo">
Dich&#x27;blog
</div>
</a>
</div>
</div>
<nav class="menu">
<ul class="menu__inner">
<li class="active"><a href="https://blog.dich.bid">blog</a></li>
<li><a href="https://blog.dich.bid/tags">tags</a></li>
<li><a href="https://blog.dich.bid/archive">archive</a></li>
<li><a href="https://blog.dich.bid/about">about me</a></li>
<li><a href="https://blog.dich.bid/links">links</a></li>
<li><a href="https://blog.dich.bid/search">search</a></li>
<li><a href="https://github.com/Dichgrem" target="_blank" rel="noopener noreferrer">github</a></li>
</ul>
</nav>
</header>
<div class="content">
<div class="post">
<h1 class="post-title"><a href="https://blog.dich.bid/docker-learn-4/">Docker学习笔记(四)</a></h1>
<div class="post-meta-inline">
<span class="post-date">
2024-03-28
</span>
</div>
<span class="post-tags-inline">
:: tags:&nbsp;
<a class="post-tag" href="https://blog.dich.bid/tags/xue-xi-bi-ji/">#学习笔记</a></span>
<div class="post-content">
<p>前言 Docker 使您能够运行与主机操作系统分离的隔离代码段包括应用程序及其依赖项的容器。默认情况下容器是临时的这意味着容器中存储的任何数据一旦终止就会丢失。为了克服这个问题并跨容器生命周期保留数据Docker 提供了多种数据持久化方法。</p>
<span id="continue-reading"></span><h2 id="docker-zhong-de-shu-ju-chi-jiu-hua"><strong>Docker 中的数据持久化</strong></h2>
<p>在本节中,我们将介绍:</p>
<ul>
<li>Docker Volumes Docker 卷</li>
<li>Bind Mounts 绑定挂载</li>
<li>Docker tmpfs mounts Docker tmpfs 挂载</li>
</ul>
<h2 id="docker-volumes-docker-juan"><strong>Docker Volumes Docker 卷</strong></h2>
<p>Docker 卷是保存 Docker 容器生成和使用的数据的首选方法。卷是 Docker 用于存储文件和目录的主机上的一个目录这些文件和目录的寿命可能超过容器的生命周期。Docker 卷可以在容器之间共享,它们提供了各种好处,例如轻松备份和数据迁移。</p>
<p>要创建卷,请使用以下命令:</p>
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker volume create volume_name
</span></code></pre>
<p>要使用卷,请在 docker run 命令中添加 --volume或 -v标志</p>
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run --volume volume_name:/container/path image_name
</span></code></pre>
<p><strong>在容器中挂载卷</strong>
若要将卷装载到容器,需要在运行容器时使用 -v 或 --mount 标志。下面是一个示例:</p>
<p>Using -v flag: 使用 -v 标志:</p>
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run -d -v my-volume:/data your-image
</span></code></pre>
<p>Using --mount flag: 使用 --mount 标志:</p>
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run -d --mount source=my-volume,destination=/data your-image
</span></code></pre>
<p>在上面的两个示例中my-volume 是我们之前创建的卷的名称,/data 是容器内将挂载卷的路径。</p>
<p><strong>在容器之间共享卷</strong>
要在多个容器之间共享卷,只需在多个容器上挂载相同的卷即可。下面介绍如何在运行不同映像的两个容器之间共享 my-volume</p>
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run -d -v my-volume:/data1 image1
</span><span>docker run -d -v my-volume:/data2 image2
</span></code></pre>
<p>在此示例中image1 和 image2 将有权访问存储在 my-volume 中的相同数据。</p>
<p><strong>Removing a Volume 删除卷</strong>
要删除卷,可以使用 docker volume rm 命令,后跟卷名称:</p>
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker volume rm my-volume
</span></code></pre>
<h2 id="bind-mounts-bang-ding-gua-zai"><strong>Bind Mounts 绑定挂载</strong></h2>
<p>绑定挂载允许您将主机上的任何目录映射到容器中的目录。此方法在需要修改主机系统上的文件的开发环境中非常有用,并且这些更改应立即在容器中可用。</p>
<p>与卷相比,绑定挂载的功能有限。使用绑定挂载时,主机上的文件或目录将装载到容器中。文件或目录由其在主机上的绝对路径引用。相比之下,当您使用卷时,会在主机上的 Docker 存储目录中创建一个新目录Docker 管理该目录的内容。</p>
<p>文件或目录不需要已存在于 Docker 主机上。如果尚不存在,则按需创建。绑定挂载的性能非常高,但它们依赖于主机的文件系统,该文件系统具有特定的可用目录结构。</p>
<p>要创建绑定挂载,请在 docker run 命令中使用带有 type=bind 的 --mount 标志:</p>
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run --mount type=bind,src=/host/path,dst=/container/path image_name
</span></code></pre>
<h2 id="docker-tmpfs-mounts-docker-tmpfs-gua-zai"><strong>Docker tmpfs mounts Docker tmpfs 挂载</strong></h2>
<p>Docker tmpfs 挂载允许您直接在容器的内存中创建临时文件存储。存储在 tmpfs 挂载中的数据快速且安全,但一旦容器终止,数据就会丢失。
要使用 tmpfs 挂载,请在 docker run 命令中添加 --tmpfs 标志:</p>
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run --tmpfs /container/path image_name
</span></code></pre>
<p>通过采用这些方法,可以确保跨容器生命周期的数据持久性,从而增强 Docker 容器的实用性和灵活性。请记住选择最适合您的用例的方法,无论是首选的 Docker 卷、方便的绑定挂载,还是快速安全的 tmpfs 挂载。</p>
</div>
<div class="pagination">
<div class="pagination__title">
<span class="pagination__title-h">Thanks for reading! Read other posts?</span>
<hr />
</div>
<div class="pagination__buttons">
<span class="button previous">
<a href="https://blog.dich.bid/docker-learn-3/">
<span class="button__icon"></span>&nbsp;
<span class="button__text">Docker学习笔记(三)</span>
</a>
</span>
<span class="button next">
<a href="https://blog.dich.bid/docker-learn-5/">
<span class="button__text">Docker学习笔记(五)</span>&nbsp;
<span class="button__icon"></span>
</a>
</span>
</div>
</div>
</div>
</div>
<footer class="footer">
<div class="footer__inner">
<div class="copyright">
<span>©
2024
Dichgrem</span>
<span class="copyright-theme">
<span class="copyright-theme-sep">:: </span>
Theme: <a href="https://github.com/pawroman/zola-theme-terminimal/">Terminimal</a> by pawroman
</span>
</div>
</div>
</footer>
</div>
</body>
</html>