Docker Compose 允许您在单个文件中定义多容器环境,从而简化复杂应用程序的部署。它自动执行创建和启动容器的过程,使其成为开发人员和 DevOps 专业人员的必备工具。
在 Fedora 38 上安装 Docker Compose
步骤 1。在继续之前,必须更新系统的软件包列表。使用以下命令:
<span class="pln">sudo dnf clean all sudo dnf update</span>
第2步。安装 Docker。
我们将从安装 Docker 存储库开始。此存储库包含 Docker 引擎包:
<span class="pln">sudo dnf config</span><span class="pun">-</span><span class="pln">manager </span><span class="pun">--</span><span class="kwd">add</span><span class="pun">-</span><span class="pln">repo</span><span class="pun">=</span><span class="pln">https</span><span class="pun">:</span><span class="com">//download.docker.com/linux/fedora/docker-ce.repo</span>
添加存储库后,继续安装 Docker 引擎:
<span class="pln">sudo dnf install docker</span><span class="pun">-</span><span class="pln">ce</span>
安装 Docker 后,启动服务并使其在启动时启动:
<span class="pln">sudo systemctl start docker sudo systemctl enable docker</span>
若要确保 Docker 已正确安装并运行,请运行以下命令:
<span class="pln">sudo docker </span><span class="pun">--</span><span class="pln">version</span>
第 3 步。在 Fedora 38 上安装 Docker Compose。
Docker Compose 允许您轻松定义和运行多容器应用程序。它使用简单的 YAML 文件来配置应用程序的服务、网络和卷。我们将需要 Docker Compose 二进制文件。您可以在 Docker Compose GitHub 页面上查看最新版本:
<span class="pln">sudo curl </span><span class="pun">-</span><span class="pln">L </span><span class="str">"https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)"</span> <span class="pun">-</span><span class="pln">o </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">compose</span>
下载后,使二进制文件可执行:
<span class="pln">sudo chmod </span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">compose</span>
将 Docker Compose 二进制文件移动到系统 PATH 中包含的目录。这可确保您可以从任何位置执行它:
<span class="pln">sudo mv </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">compose </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">compose</span>
要确认 Docker Compose 安装成功,请检查版本:
<span class="pln">docker</span><span class="pun">-</span><span class="pln">compose </span><span class="pun">--</span><span class="pln">version</span>
这应该显示已安装的 Docker Compose 版本。
运行以下命令以验证安装:
<span class="pln">docker</span><span class="pun">-</span><span class="pln">compose</span>
第 4 步。使用 Docker Compose。
- A. 编写文件和 YAML 语法
Docker Compose 依赖于 YAML 文件进行配置。这些文件定义应用程序所需的服务、网络和卷。
- B. 在撰写文件中定义服务
在项目目录中创建一个文件来定义服务。例如,Web 应用程序的简单 Compose 文件可能如下所示:docker-compose.yml
<span class="pln">version</span><span class="pun">:</span> <span class="str">'3'</span><span class="pln"> services</span><span class="pun">:</span><span class="pln"> web</span><span class="pun">:</span><span class="pln"> image</span><span class="pun">:</span><span class="pln"> nginx</span><span class="pun">:</span><span class="pln">alpine db</span><span class="pun">:</span><span class="pln"> image</span><span class="pun">:</span><span class="pln"> postgres</span><span class="pun">:</span><span class="pln">alpine</span>
- C. 构建和启动容器
要创建并启动 Compose 文件中指定的容器,请导航到包含该文件的目录并运行:
<span class="pln">docker</span><span class="pun">-</span><span class="pln">compose up</span>
-
- D. 管理多容器应用程序
Docker Compose 简化了多容器应用程序的管理。使用 、 等命令有效地管理服务。
docker-compose start
docker-compose stop
docker-compose restart
- E. 使用 Docker Compose 的网络和卷
Docker Compose 会自动处理容器之间的网络,并允许您指定数据持久性的卷。
感谢您使用本教程在 Fedora 38 系统上安装 Docker Compose。如需更多帮助或有用信息,我们建议您查看 Docker 官方网站。