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:入门与实践,第三版

第 8 章 探索 Docker Compose 探索 Docker Compose

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

到此为止,你应该 对docker 命令以及如何使用它来构建、启动、监控和调试应用程序有了一定的了解。一旦你能够自如地使用单个容器,不久你就会想要共享你的项目,并开始构建需要多个容器才能正常运行的更复杂的项目。在开发环境中尤其如此,在本地机器上运行一整堆容器可以轻松模拟许多生产环境。

但是,如果您运行的是整个容器堆栈,则每个容器都需要在正确设置的情况下运行,以确保底层应用程序配置正确并按预期运行。要每次都正确地完成这些设置可能很有难度,尤其是当你不是最初编写应用程序的人时。在开发过程中,为了帮助解决这个问题,人们通常会尝试编写 shell 脚本,以便始终如一地构建和运行多个容器。虽然这种方法可行,但对于新手来说可能难以理解,而且随着时间的推移,项目也会发生变化,很难维护。而且这种方法也不一定能在不同项目之间重复使用。

为了帮助解决这个问题, Docker 公司发布了一款主要面向开发人员的工具,名为 Docker Compose。这个工具包含在 Docker Desktop 中,但你也可以按照在线安装说明进行安装

备注

Docker Compose 最初是 一个用 Python 编写的单独应用程序,使用命令docker-compose 运行。该命令被称为 Docker Compose 第 1 版,最近已被 Docker Compose 第 2 版取代。Docker Compose v2 完全用 Go 重写,作为 Docker 客户端插件。如果docker compose version 返回一个结果,说明你已经安装了该插件。如果没有,我们强烈建议您现在就花点时间安装。

Docker Compose 是一款非常有用的工具,可以简化各种传统上非常繁琐且容易出错的开发任务。它可以轻松地帮助开发人员快速启动复杂的应用程序栈,编译应用程序,而无需设置复杂的本地开发环境,等等。

在本章中,我们将简要介绍如何使用 Compose 发挥其最大优势。我们将在以下所有示例中使用 GitHub 仓库。如果你想在我们讲解示例时运行这些示例,你应该运行下面的命令下载代码,如果你在第 6 章中还没有这样做的话:

$ git clone https://github.com/spkane/rocketchat-hubot-demo.git \
    --config core.autocrlf=input
备注

在下面的示例、shell 脚本和Docker-compose.yaml文件中,有些行可能被截断以适应页边距。如果你打算自己尝试这些示例,请确保使用该 Git 仓库中的文件。

该资源库包含我们启动一个完整的网络服务所需的配置,其中包括 MongoDB 数据存储、开源 Rocket.Chat 通信服务器、HubotChatOps机器人和一个zmachine-api 实例,以提供一点惊喜娱乐价值。

配置 Docker Compose

在深入使用docker compose 命令( )之前,我们有必要了解一下它所取代的临时工具。因此,让我们花点时间来看看一个 shell 脚本,它可以用来构建和部署我们服务的本地副本,以便通过 Docker 进行开发和本地测试。这个输出很长,也很详细,但对于证明 Docker Compose 为什么比 shell 脚本有巨大飞跃这一点很重要。

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