370 lines
490 KiB
HTML
370 lines
490 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html lang="zh"><head><title>Docker Dockerfile</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 Dockerfile"/><meta property="og:description" content="Docker Dockerfile."/><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 Dockerfile."/><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-Dockerfile"><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="nightIco
|
|||
|
<p>Dockerfile 是一个文本文件,包含了一系列指令和参数,用于自动化构建 Docker 镜像。每条指令都会在镜像中创建一个新的层,涵盖了从基础镜像开始、复制文件、运行命令、设置环境变量等多种操作。通过 Dockerfile,可以确保镜像的构建过程是可重复且无差异的,这对于持续集成和持续部署(CI/CD)的实施至关重要。</p>
|
|||
|
<h2 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></h2>
|
|||
|
<p>Dockerfile 的开发是一个循环的过程,涉及编写、构建、测试、优化和维护,旨在创建高效、可维护且可重复使用的 Dockerfile,以生成高质量的 Docker 镜像。</p>
|
|||
|
<p>以下是 Dockerfile 开发的常规工作流程:</p>
|
|||
|
<pre><code class="mermaid">graph TD
|
|||
|
A[需求分析] --> B[编写 Dockerfile]
|
|||
|
B --> C[构建镜像]
|
|||
|
C --> D[测试镜像]
|
|||
|
D --> E{满足需求?}
|
|||
|
E -->|是| F[优化和迭代]
|
|||
|
E -->|否| G[维护和更新]
|
|||
|
F --> H[分享和部署]
|
|||
|
G --> B
|
|||
|
H --> I[结束]
|
|||
|
</code></pre>
|
|||
|
<p>关键步骤:</p>
|
|||
|
<ol>
|
|||
|
<li><strong>定义基础镜像</strong>:从一个已有的镜像开始,这是所有后续操作的基础。</li>
|
|||
|
<li><strong>执行构建命令</strong>:安装软件包、修改配置文件等。</li>
|
|||
|
<li><strong>添加文件和目录</strong>:将所需文件和目录添加到镜像中。</li>
|
|||
|
<li><strong>设置工作目录</strong>:为 Dockerfile 中的指令指定工作目录。</li>
|
|||
|
<li><strong>配置环境变量</strong>:设定必要的环境变量。</li>
|
|||
|
<li><strong>暴露端口</strong>:声明容器监听的端口。</li>
|
|||
|
<li><strong>配置启动命令</strong>:设置容器启动时执行的命令。</li>
|
|||
|
</ol>
|
|||
|
<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><th>示例</th></tr></thead><tbody><tr><td><strong>FROM</strong></td><td>指定基础镜像,是构建新镜像的起点。</td><td><code>FROM ubuntu:18.04</code></td></tr><tr><td><strong>RUN</strong></td><td>在镜像构建过程中运行命令。</td><td><code>RUN apt-get update && apt-get install -y nginx</code></td></tr><tr><td><strong>CMD</strong></td><td>提供容器启动时的默认执行命令。</td><td><code>CMD ["nginx", "-g", "daemon off;"]</code></td></tr><tr><td><strong>ENTRYPOINT</strong></td><td>配置容器启动时运行的命令,可以与 CMD 指令配合使用。</td><td><code>ENTRYPOINT ["./app"]</code></td></tr><tr><td><strong>COPY</strong></td><td>将文件从构建上下文复制到镜像中。</td><td><code>COPY . /app</code></td></tr><tr><td><strong>ADD</strong></td><td>将文件从构建上下文或 URL 复制到镜像中,可自动解压压缩包。</td><td><code>ADD http://example.com/big.tar.xz /usr/src/things/</code></td></tr><tr><td><strong>ENV</strong></td><td>设置环境变量。</td><td><code>ENV MY_VAR=value</code></td></tr><tr><td><strong>EXPOSE</strong></td><td>声明容器运行时监听的端口。</td><td><code>EXPOSE 80</code></td></tr><tr><td><strong>VOLUME</strong></td><td>创建一个挂载点来持久化数据。</td><td><code>VOLUME /data</code></td></tr><tr><td><strong>WORKDIR</strong></td><td>为 Dockerfile 中的指令设置工作目录。</td><td><code>WORKDIR /app</code></td></tr><tr><td><strong>ARG</strong></td><td>定义构建时的变量,可以在构建命令中用 <code>--build-arg</code> 来覆盖。</td><td><code>ARG VERSION=latest</code></td></tr><tr><td><strong>USER</strong></td><td>指定运行容器时的用户名或 UID,以及可选的用户组或 GID。</td><td><code>USER www-data</code></td></tr><tr><td><strong>LABEL</strong></td><td>为镜像添加元数据。</td><td><code>LABEL version="1.0"</code></td></tr><tr><td><strong>ONBUILD</strong></td><td>为镜像指定触发器指令,当镜像作为基础镜像时,触发器将在派生镜像中执行。</td><td><code>ONBUILD RUN echo 'Doing something...'</code></td></tr><tr><td><strong>HEALTHCHECK</strong></td><td>指定一个命令,用于检查容器是否健康运行。</td><td>`HEALTHCHECK CMD curl —fail <a href="http://localhost:80/" class="external">http://localhost:80/<svg aria-hidden="true" class="external-icon" viewBox="0 0 512 512"><path d="M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"></path></svg></a></td></tr><tr><td><strong>SHELL</strong></td><td>用于覆盖默认的 shell 命令。</td><td><code>SHELL ["/bin/bash", "-c"]</code></td></tr><tr><td><strong>STOPSIGNAL</strong></td><td>设置停止容器时发送的系统调用信号。</td><td><code>STOPSIGNAL SIGTERM</code></td></tr></tbody></table></div>
|
|||
|
<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>
|
|||
|
<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:#6A737D;--shiki-dark:#6A737D;"># 指定基础镜像</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">FROM</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> python:3.8</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置工作目录</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">WORKDIR</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /app</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 复制依赖文件到容器中</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">COPY</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> requirements.txt ./</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 安装依赖</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 --no-cache-dir -r requirements.txt</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将当前目录下的所有文件复制到容器的工作目录中</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">COPY</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> . .</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 暴露端口</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">EXPOSE</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> 5000</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义容器启动时执行的命令</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;">"flask"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"run"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"--host=0.0.0.0"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</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>
|
|||
|
<p>镜像构建是 Docker 使用中的核心概念,它允许用户从一个基础镜像开始,通过一系列步骤添加自定义的层次,最终创建出一个新的镜像。深入了解镜像构建,包括二次构建(多阶段构建)等高级特性,可以帮助你更高效地使用 Docker,优化你的开发和部署流程。</p>
|
|||
|
<ul>
|
|||
|
<li><strong>Dockerfile</strong>:Dockerfile 是构建 Docker 镜像的蓝图,它包含了一系列的指令,每个指令都会在镜像中创建一个新的层。</li>
|
|||
|
<li><strong>镜像层</strong>:Docker 镜像是由多个只读层组成的。当你更改镜像并提交这些更改时,你实际上是在基础镜像上添加了一个新层。</li>
|
|||
|
<li><strong>构建上下文</strong>:构建上下文是指向 Docker 守护进程的一组文件和目录,这些文件和目录用于构建 Docker 镜像。构建上下文的路径可以在 <code>docker build</code> 命令中指定。</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>使用 <code>docker build</code> 命令和 Dockerfile 来构建镜像。在构建过程中,Docker 逐条处理 Dockerfile 中的指令,每条指令都会创建镜像的一个新层。构建完成后,你将获得一个可用于创建容器的镜像。</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;"> build</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -t</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my_image_name:my_tag</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> .</span></span></code></pre></figure>
|
|||
|
<p>这个命令将使用当前目录中的 Dockerfile 来构建镜像,并将其标记为 <code>my_image_name:my_tag</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 17.05 版本引入的一个特性,它允许在一个 Dockerfile 中定义多个构建阶段,每个阶段都可以使用不同的基础镜像。多阶段构建的主要优点是减小最终镜像的大小,提高构建效率,避免在最终镜像中包含不必要的文件。</p>
|
|||
|
<ul>
|
|||
|
<li><strong>定义多个阶段</strong>:可以通过在 Dockerfile 中多次使用 <code>FROM</code> 指令来定义多个构建阶段。</li>
|
|||
|
<li><strong>复制阶段间的文件</strong>:使用 <code>COPY --from=<stage></code> 指令可以从一个阶段复制文件到另一个阶段。</li>
|
|||
|
</ul>
|
|||
|
<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:#6A737D;--shiki-dark:#6A737D;"># 第一阶段:构建阶段</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用官方 Python 镜像作为构建镜像的基础</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">FROM</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> python:3.8-slim </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">as</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> builder</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置工作目录</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">WORKDIR</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /app</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 将应用依赖复制到容器中</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">COPY</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> requirements.txt .</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 安装应用依赖</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 --user -r requirements.txt</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 复制应用代码到容器中</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">COPY</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> . .</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 第二阶段:运行阶段</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 再次从一个干净的 Python 镜像开始</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">FROM</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> python:3.8-slim</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 创建一个非 root 用户</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">RUN</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> useradd -m myuser</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">USER</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> myuser</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 从构建阶段复制已安装的依赖</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">COPY</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> --from=builder /root/.local /home/myuser/.local</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">COPY</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> --from=builder /app /app</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置工作目录</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">WORKDIR</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /app</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置环境变量</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">ENV</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> PATH=/home/myuser/.local/bin:$PATH</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 暴露应用端口</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">EXPOSE</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> 5000</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置容器启动时执行的命令</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>
|
|||
|
<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>
|
|||
|
<ul>
|
|||
|
<li><strong>减少层的数量</strong>:合并多个 <code>RUN</code> 指令可以减少镜像层的数量,减小镜像大小。</li>
|
|||
|
<li><strong>使用 <code>.dockerignore</code> 文件</strong>:通过定义 <code>.dockerignore</code> 文件排除不必要的文件和目录,减少构建上下文的大小,加快构建速度。</li>
|
|||
|
<li><strong>利用构建缓存</strong>:Docker 会缓存每一层的结果,如果 Dockerfile 中的某一层没有变化,则在构建时会重用这一层的缓存。合理利用构建缓存可以显著提高构建效率。</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 提供了多种命令来管理本地存储的镜像:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>命令</th><th>描述</th><th>示例</th></tr></thead><tbody><tr><td><code>docker images</code> 或 <code>docker image ls</code></td><td>列出本地所有镜像</td><td><code>docker images</code></td></tr><tr><td><code>docker rmi <image></code></td><td>删除指定的镜像</td><td><code>docker rmi nginx:latest</code></td></tr><tr><td><code>docker inspect <image></code></td><td>显示镜像的详细信息</td><td><code>docker inspect ubuntu:18.04</code></td></tr><tr><td><code>docker pull <image></code></td><td>从远程仓库拉取指定的镜像</td><td><code>docker pull python:3.8-slim</code></td></tr><tr><td><code>docker push <image></code></td><td>将本地镜像推送到远程仓库</td><td><code>docker push myusername/myimage:tag</code></td></tr><tr><td><code>docker build -t <tag> .</code></td><td>根据当前目录的 Dockerfile 构建镜像</td><td><code>docker build -t myapp:v1 .</code></td></tr><tr><td><code>docker history <image></code></td><td>查看镜像的构建历史</td><td><code>docker history nginx:latest</code></td></tr><tr><td><code>docker tag <image> <tag></code></td><td>为镜像添加一个新标签</td><td><code>docker tag myimage:latest myimage:v2</code></td></tr><tr><td><code>docker save -o <path> <image></code></td><td>将镜像保存为 tar 归档文件</td><td><code>docker save -o myimage.tar myimage:latest</code></td></tr><tr><td><code>docker load -i <path></code></td><td>从 tar 归档文件中加载镜像</td><td><code>docker load -i myimage.tar</code></td></tr><tr><td><code>docker image prune</code></td><td>删除未被任何容器使用的悬挂镜像</td><td><code>docker image prune</code></td></tr><tr><td><code>docker image rm <image></code></td><td>删除一个或多个镜像</td><td><code>docker image rm myimage1 myimage2</code></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>
|
|||
|
<p>Docker 镜像仓库扮演着在 Docker 生态系统中极为关键的角色,它们不仅存储和分发容器镜像,还促进了开发和运维工作的协同。这些仓库可以是公开的,也可以是私有的,以满足不同的安全和隐私需求。</p>
|
|||
|
<p>Docker Hub 是最广为人知的 Docker 镜像仓库,提供了大量的公共镜像供下载和使用。它支持个人和组织管理镜像,并能与自动化构建和测试流程无缝集成。</p>
|
|||
|
<p>对于需要控制镜像访问权限的场景,私有 Docker 仓库是理想的选择。私有仓库可以部署在内部网络中,确保敏感镜像的安全。Docker Registry 是官方提供的开源仓库解决方案,支持本地部署和管理私有镜像。</p>
|
|||
|
<h3 id="使用-docker-hub-拉取镜像">使用 Docker Hub 拉取镜像<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-docker-hub-拉取镜像" 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 Hub 或私有仓库拉取镜像是常见操作。使用 <code>docker pull</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;"> pull</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my_username/my_image_name:my_tag</span></span></code></pre></figure>
|
|||
|
<h3 id="推送镜像到-docker-hub">推送镜像到 Docker Hub<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#推送镜像到-docker-hub" 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="1-创建-docker-hub-账号">1. 创建 Docker Hub 账号<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#1-创建-docker-hub-账号" 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 Hub 的账号,你需要先在 <a href="https://hub.docker.com/" class="external">Docker Hub<svg aria-hidden="true" class="external-icon" viewBox="0 0 512 512"><path d="M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"></path></svg></a> 上注册一个。</p>
|
|||
|
<h4 id="2-登录到-docker-hub">2. 登录到 Docker Hub<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#2-登录到-docker-hub" 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>docker login</code> 命令登录到你的 Docker Hub 账号。输入你的用户名和密码进行认证。</p>
|
|||
|
<pre><code>docker login
|
|||
|
</code></pre>
|
|||
|
<p>成功登录后,你的认证信息将被保存,以便后续操作无需重复登录。</p>
|
|||
|
<h4 id="3-标记你的镜像">3. 标记你的镜像<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#3-标记你的镜像" 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>在推送镜像之前,你需要为你的镜像设置一个标签(Tag),这个标签应该包含你的 Docker Hub 用户名、镜像的名字,以及(可选的)标签。</p>
|
|||
|
<pre><code>docker tag local-image-name:tag your-dockerhub-username/repository-name:tag
|
|||
|
</code></pre>
|
|||
|
<ul>
|
|||
|
<li><code>local-image-name:tag</code> 是你本地镜像的名称和标签。</li>
|
|||
|
<li><code>your-dockerhub-username</code> 是你在 Docker Hub 上的用户名。</li>
|
|||
|
<li><code>repository-name</code> 是你希望在 Docker Hub 上创建的仓库名。</li>
|
|||
|
<li><code>tag</code> 是你给镜像指定的标签,如果不指定,默认为 <code>latest</code>。</li>
|
|||
|
</ul>
|
|||
|
<p>例如,如果你的 Docker Hub 用户名是 <code>johndoe</code>,并且你想要推送一个名为 <code>myapp</code> 的镜像,标签为 <code>v1.0</code>,你可以执行:</p>
|
|||
|
<pre><code>docker tag myapp:latest johndoe/myapp:v1.0
|
|||
|
</code></pre>
|
|||
|
<h4 id="4-推送镜像到-docker-hub">4. 推送镜像到 Docker Hub<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#4-推送镜像到-docker-hub" 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>docker push</code> 命令将镜像推送到你的 Docker Hub 仓库:</p>
|
|||
|
<pre><code>docker push your-dockerhub-username/repository-name:tag
|
|||
|
</code></pre>
|
|||
|
<p>继续之前的例子:</p>
|
|||
|
<pre><code>docker push johndoe/myapp:v1.0
|
|||
|
</code></pre>
|
|||
|
<p>这个命令会将 <code>myapp</code> 镜像的 <code>v1.0</code> 标签版本推送到 Docker Hub 上的 <code>johndoe/myapp</code> 仓库中。</p>
|
|||
|
<h4 id="5-验证">5. 验证<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#5-验证" 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 Hub 的网站上登录你的账号,查看你的仓库列表,确认新推送的镜像已经出现在列表中。</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 Hub 类似,但需要确保你有权限访问该私人仓库,并且可能需要配置额外的认证信息。以下是推送镜像到私人仓库的一般步骤:</p>
|
|||
|
<h4 id="1-登录私人仓库">1. 登录私人仓库<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#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>
|
|||
|
<p>私有仓库通常需要认证,以保证仓库的安全性。使用 <code>docker login</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;"> login</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> myregistry.example.com</span></span></code></pre></figure>
|
|||
|
<h4 id="2标记你的镜像">2.标记你的镜像<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#2标记你的镜像" 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>docker tag</code> 命令完成的,标记格式通常为 <code>仓库地址/用户名/镜像名称:标签</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;"> tag</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my_image_name:my_tag</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> myregistry.example.com/my_username/my_image_name:my_tag</span></span></code></pre></figure>
|
|||
|
<p>这里:</p>
|
|||
|
<ul>
|
|||
|
<li><code>my_image_name:my_tag</code> 是你本地镜像的名称和标签。</li>
|
|||
|
<li><code>myregistry.example.com</code> 是你的私人仓库地址。</li>
|
|||
|
<li><code>my_username</code> 是你在该仓库的用户名或命名空间。</li>
|
|||
|
</ul>
|
|||
|
<h4 id="3-推送镜像">3. 推送镜像<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#3-推送镜像" 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>docker push</code> 命令将其推送到私人仓库。</p>
|
|||
|
<p>标记完成后,使用 <code>docker push</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;"> push</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> myregistry.example.com/my_username/my_image_name:my_tag</span></span></code></pre></figure>
|
|||
|
<h4 id="4-管理私人仓库中的镜像">4. 管理私人仓库中的镜像<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#4-管理私人仓库中的镜像" 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>私人仓库可能提供了一个用户界面或者 API,供你管理仓库中的镜像,包括查看、删除和设置访问权限等。</p>
|
|||
|
<h4 id="5-使用私人仓库中的镜像">5. 使用私人仓库中的镜像<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#5-使用私人仓库中的镜像" 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>docker pull</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;"> pull</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> myregistry.example.com/my_username/my_image_name:my_tag</span></span></code></pre></figure></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="#dockerfile" data-for="dockerfile">Dockerfile</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-0"><a href="#镜像仓库" data-for="镜像仓库">镜像仓库</a></li><li class="depth-1"><a href="#使用-docker-hub-拉取镜像" data-for="使用-docker-hub-拉取镜像">使用 Docker Hub 拉取镜像</a></li><li class="depth-1"><a href="#推送镜像到-docker-hub" data-for="推送镜像到-docker-hub">推送镜像到 Docker Hub</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&
|
|||
|
</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>
|