Skip to Content
Docker:入门与实践,第三版
book

Docker:入门与实践,第三版

by Sean P. Kane, Karl Matthias
May 2025
Intermediate to advanced
418 pages
5h 47m
Chinese
O'Reilly Media, Inc.
Content preview from Docker:入门与实践,第三版

第 4 章 使用 Docker 映像 使用 Docker 映像

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

每个 Linux 容器都基于映像。 映像是重组为运行容器的基础定义,就像虚拟磁盘在启动时会变成虚拟机一样。Docker 或Open Container Initiative (OCI)映像为您使用 Docker 部署和运行的一切提供了基础。要启动容器,你必须下载公共镜像或创建自己的镜像。您可以将映像视为主要代表容器文件系统的单一资产。但实际上,每个映像都由一个或多个链接的文件系统层组成,这些文件系统层通常与用于创建该映像的每个构建步骤直接一一对应。

由于映像是由单个层构建起来的, 它们对 Linux 内核提出了特殊的要求,内核必须提供 Docker 运行存储后端所需的驱动程序。在镜像管理方面,Docker 在很大程度上依赖于这个存储后端,它与底层 Linux 文件系统通信,以构建和管理组合成单个可用镜像的多个层。支持的主要存储后端包括以下几种:

每个存储后端都为 提供了一个用于镜像管理的快速 "写时复制"(CoW)系统。我们将在第 11 章讨论各种后端的具体细节。现在,我们将使用默认的后端并探索映像是如何工作的,因为映像几乎是你使用 Docker 所做的其他一切的基础,包括以下内容:

  • 建筑图像

  • 上传(推送)图像到图像注册表

  • 从图像注册表下载(提取)图像

  • 从映像创建和运行容器

剖析 Dockerfile

要使用默认工具创建自定义 Docker 映像 ,您需要熟悉Dockerfile。该文件描述了创建映像所需的所有步骤,通常包含在应用程序源代码库的根目录中。

典型的Docker 文件可能与此处所示的文件类似,它为基于 Node.js 的应用程序创建了一个容器:

FROM node:18.13.0

ARG email="anna@example.com"
LABEL "maintainer"=$email
LABEL "rating"="Five Stars" "class"="First Class"

USER root

ENV AP /data/app
ENV SCPATH /etc/supervisor/conf.d

RUN apt-get -y update

# The daemons
RUN apt-get -y install supervisor
RUN mkdir -p /var/log/supervisor

# Supervisor Configuration
COPY ./supervisord/conf.d/* $SCPATH/

# Application Code
COPY *.js* $AP/

WORKDIR $AP

RUN npm install

CMD ["supervisord", "-n"]

剖析这个Dockerfile,可以让 初步了解一些控制映像组装方式的可能指令。Docker 文件中的每一行都会创建一个新的映像层,由 Docker 保存。该层包含了发出该命令后的所有更改。这意味着,当你构建新的映像时,Docker 只需要构建与之前的构建有偏差的层:你可以重复使用所有没有改变的层。

虽然你可以从普通的基础 Linux 映像构建一个 Node 实例,但你也可以探索Docker Hub以获取 Node 的官方映像。Node.js 社区维护了一系列Docker ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

人工智能技术与大数据

人工智能技术与大数据

Posts & Telecom Press, Anand Deshpande, Manish Kumar
超越Vibe编程

超越Vibe编程

Addy Osmani
写给系统管理员的Python脚本编程指南

写给系统管理员的Python脚本编程指南

Posts & Telecom Press, Ganesh Sanjiv Naik

Publisher Resources

ISBN: 9798341656826