2025-01-02 21:19:48 +08:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
|
|
|
|
|
<head>
|
|
|
|
|
<title>Dich'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">
|
2025-01-21 19:11:02 +08:00
|
|
|
|
<meta property="og:url" content="https://blog.dich.bid/learn-docker-5/">
|
2025-01-02 21:19:48 +08:00
|
|
|
|
|
|
|
|
|
<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">
|
2025-01-21 19:11:02 +08:00
|
|
|
|
<meta property="twitter:url" content="https://blog.dich.bid/learn-docker-5/">
|
2025-01-02 21:19:48 +08:00
|
|
|
|
|
|
|
|
|
<link rel="alternate" type="application/atom+xml" title="Dich'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'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">
|
|
|
|
|
|
2025-01-21 19:11:02 +08:00
|
|
|
|
<h1 class="post-title"><a href="https://blog.dich.bid/learn-docker-5/">Docker学习笔记(五)</a></h1>
|
2025-01-02 21:19:48 +08:00
|
|
|
|
<div class="post-meta-inline">
|
|
|
|
|
|
|
|
|
|
<span class="post-date">
|
|
|
|
|
2024-03-29
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<span class="post-tags-inline">
|
|
|
|
|
:: tags:
|
|
|
|
|
<a class="post-tag" href="https://blog.dich.bid/tags/xue-xi-bi-ji/">#学习笔记</a></span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div class="post-content">
|
|
|
|
|
<p>前言 第三方映像是预构建的 Docker 容器映像,可在 Docker Hub 或其他容器注册表上使用。这些映像由个人或组织创建和维护,可用作容器化应用程序的起点。</p>
|
|
|
|
|
<span id="continue-reading"></span><h2 id="cha-zhao-di-san-fang-jing-xiang"><strong>查找第三方镜像</strong></h2>
|
|
|
|
|
<p>Docker Hub 是最大、最受欢迎的容器镜像注册表,包含官方和社区维护的镜像。您可以根据要使用的名称或技术搜索镜像。</p>
|
|
|
|
|
<p>例如:如果你正在寻找 Node.js 映像,你可以在 Docker Hub 上搜索“Node”,你会找到官方Node.js映像以及许多其他社区维护的映像。</p>
|
|
|
|
|
<p><strong>在 Dockerfile 中使用映像</strong>
|
|
|
|
|
要在 Dockerfile 中使用第三方映像,只需使用 FROM 指令将映像名称设置为基础映像即可。下面是使用官方Node.js镜像的示例:</p>
|
|
|
|
|
<p>FROM node:14</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span># The rest of your Dockerfile...
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p><strong>注意安全问题</strong>
|
|
|
|
|
请记住,第三方映像可能存在安全漏洞或配置错误。在生产中使用镜像之前,请务必验证镜像的来源并检查其信誉。更喜欢使用官方镜像或维护良好的社区镜像。</p>
|
|
|
|
|
<p><strong>维护您的镜像</strong>
|
|
|
|
|
使用第三方映像时,必须保持更新,以包含最新的安全更新和依赖项更改。定期检查基础映像中的更新,并相应地重新生成应用程序容器。</p>
|
|
|
|
|
<h2 id="shi-yong-di-san-fang-ying-xiang-shu-ju-ku"><strong>使用第三方映像:数据库</strong></h2>
|
|
|
|
|
<p>在 Docker 容器中运行数据库有助于简化开发过程并简化部署。Docker Hub 为 MySQL、PostgreSQL 和 MongoDB 等流行数据库提供了大量预制映像。</p>
|
|
|
|
|
<p><strong>示例:使用 MySQL 镜像</strong>
|
|
|
|
|
要使用 MySQL 数据库,请在 Docker Hub 上搜索官方镜像:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker search mysql
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>找到官方图片,然后拉动它:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker pull mysql
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>现在,您可以运行 MySQL 容器。指定所需的环境变量,例如 MYSQL_ROOT_PASSWORD,并选择性地将容器的端口映射到主机:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 -d mysql
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>此命令创建一个名为 some-mysql 的新容器,将 root 密码设置为 my-secret-pw,并将主机上的端口 3306 映射到容器上的端口 3306。</p>
|
|
|
|
|
<p><strong>示例:使用 PostgreSQL 镜像</strong>
|
|
|
|
|
对于 PostgreSQL,请执行与上述步骤类似的步骤。首先,搜索官方图片:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker search postgres
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>Pull the image: 拉取图像:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker pull postgres
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>运行 PostgreSQL 容器,指定环境变量,例如 POSTGRES_PASSWORD:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run --name some-postgres -e POSTGRES_PASSWORD=my-secret-pw -p 5432:5432 -d postgres
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<h2 id="shi-yong-docker-de-jiao-hu-shi-ce-shi-huan-jing"><strong>使用 Docker 的交互式测试环境</strong></h2>
|
|
|
|
|
<p>Docker 允许您创建隔离的一次性环境,这些环境可以在完成测试后删除。这使得使用第三方软件、测试不同的依赖项或版本以及快速试验变得更加容易,而不会损坏本地设置。</p>
|
|
|
|
|
<p><strong>使用 Docker 创建交互式测试环境</strong>
|
|
|
|
|
为了演示如何设置交互式测试环境,让我们以 Python 编程语言为例。我们将使用 Docker Hub 上提供的公共 Python 映像。</p>
|
|
|
|
|
<p>要使用 Python 映像启动交互式测试环境,只需运行以下命令:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run -it --rm python
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>在这里,-it 标志确保使用 tty 在交互模式下运行容器,而 --rm 标志将在容器停止后将其删除。</p>
|
|
|
|
|
<p>现在,您应该位于容器内的交互式 Python shell 中。您可以像往常一样使用 pip 执行任何 Python 命令或安装其他软件包。</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>print("Hello, Docker!")
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>完成交互式会话后,只需键入 exit() 或按 CTRL+D 即可退出容器。容器将按照 --rm 标志的指定自动删除。</p>
|
|
|
|
|
<p><strong>交互式测试环境的更多示例</strong>
|
|
|
|
|
可以使用 Docker Hub 上提供的多个第三方映像,并创建各种交互式环境,例如:</p>
|
|
|
|
|
<p>Node.js:若要启动交互式 Node.js shell,可以使用以下命令:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run -it --rm node
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>Ruby:要启动交互式 Ruby shell,您可以使用以下命令:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run -it --rm ruby
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>MySQL:要启动临时 MySQL 实例,您可以使用以下命令:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker run -it --rm --name temp-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -p 3306:3306 mysql
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>这将启动一个临时的MySQL服务器,该服务器可以通过主机端口3306访问。一旦容器停止,它将被移除。</p>
|
|
|
|
|
<p>随意探索和测试各种软件,而不必担心损坏本地计算机或安装不必要的依赖项。将 Docker 用于交互式测试环境可以让您在处理各种第三方软件时更高效、更干净地工作。</p>
|
|
|
|
|
<h2 id="ming-ling-xing-shi-yong-cheng-xu"><strong>命令行实用程序</strong></h2>
|
|
|
|
|
<p>Docker 映像可以包括命令行实用程序或我们可以在容器内运行的独立应用程序。这在使用第三方映像时非常有用,因为我们想要使用的工具已经打包好,无需任何安装或配置即可运行。</p>
|
|
|
|
|
<p><strong>BusyBox</strong>
|
|
|
|
|
BusyBox 是一个小型 (1-2 Mb) 且简单的命令行应用程序,它提供了大量常用的 Unix 实用程序,例如 awk、grep、vi 等。要在 Docker 容器中运行 BusyBox,只需拉取映像并使用 Docker 运行它:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker pull busybox
|
|
|
|
|
</span><span>docker run -it busybox /bin/sh
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>进入容器后,您可以开始运行各种 BusyBox 实用程序,就像在常规命令行上一样。</p>
|
|
|
|
|
<p><strong>cURL</strong>
|
|
|
|
|
cURL 是一个众所周知的命令行工具,可用于使用各种网络协议传输数据。它通常用于测试 API 或从 Internet 下载文件。若要在 Docker 容器中使用 cURL,可以使用 Docker Hub 上提供的官方 cURL 映像:</p>
|
|
|
|
|
<pre style="background-color:#151515;color:#e8e8d3;"><code><span>docker pull curlimages/curl
|
|
|
|
|
</span><span>docker run --rm curlimages/curl https://example.com
|
|
|
|
|
</span></code></pre>
|
|
|
|
|
<p>在此示例中,--rm 标志用于在命令运行完毕后删除容器。当您只需要运行单个命令,然后清理容器时,这很有用。</p>
|
|
|
|
|
<p><strong>其他命令行实用程序</strong>
|
|
|
|
|
Docker 映像中提供了许多命令行实用程序,包括但不限于:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>wget:用于从 Web 上非交互式下载文件的免费实用程序。</li>
|
|
|
|
|
<li>imagemagick:用于图像处理和转换的强大软件套件。</li>
|
|
|
|
|
<li>jq:轻量级且灵活的命令行 JSON 处理器。</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<p>若要使用这些工具中的任何一个,可以在 Docker Hub 上搜索它们,并按照其各自存储库中提供的说明进行操作。</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">
|
2025-01-21 19:11:02 +08:00
|
|
|
|
<a href="https://blog.dich.bid/learn-docker-4/">
|
2025-01-02 21:19:48 +08:00
|
|
|
|
<span class="button__icon">←</span>
|
|
|
|
|
<span class="button__text">Docker学习笔记(四)</span>
|
|
|
|
|
</a>
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<span class="button next">
|
2025-01-21 19:11:02 +08:00
|
|
|
|
<a href="https://blog.dich.bid/learn-docker-6/">
|
2025-01-02 21:19:48 +08:00
|
|
|
|
<span class="button__text">Docker学习笔记(六)</span>
|
|
|
|
|
<span class="button__icon">→</span>
|
|
|
|
|
</a>
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<footer class="footer">
|
|
|
|
|
<div class="footer__inner">
|
|
|
|
|
<div class="copyright">
|
|
|
|
|
<span>©
|
|
|
|
|
2025
|
|
|
|
|
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>
|