如何在Ubuntu18.04上安装DockerCompose

来自菜鸟教程
跳转至:导航、​搜索

介绍

Docker 是在软件容器中自动部署 Linux 应用程序的出色工具,但要充分利用其潜力,应用程序的每个组件都应在其自己的单独容器中运行。 对于具有大量组件的复杂应用程序,编排所有容器以一起启动、通信和关闭可能很快变得笨拙。

Docker 社区提出了一个名为 Fig 的流行解决方案,它允许您使用单个 YAML 文件来编排所有 Docker 容器和配置。 这变得如此流行,以至于 Docker 团队决定基于 Fig 源制作 Docker Compose,该源现已弃用。

Docker Compose 使用户可以更轻松地编排 Docker 容器的流程,包括启动、关闭以及设置容器内链接和卷。 在本教程中,您将安装最新版本的 Docker Compose 来管理多容器应用程序。

先决条件

要阅读本文,您将需要一个 Ubuntu 18.04 服务器,其中包含以下内容:

Note: This tutorial will guide you through installing Docker Compose v1, which uses docker-compose. Starting with Docker Compose v2, Docker has migrated towards using the compose CLI plugin command as documented in our latest Ubuntu 22.04 version of this tutorial, and away from the original docker-compose. While the installation differs, in general the actual usage involves dropping the hyphen from docker-compose calls to become docker compose. For full compatibility details, check the official Docker documentation on command compatibility between the new compose and the old docker-compose.


第 1 步 — 安装 Docker Compose

Although you can install Docker Compose from the official Ubuntu repositories, it is several minor versions behind the latest release, so you’ll install Docker Compose from Docker’s GitHub repository. The command below is slightly different than the one you’ll find on the Releases page. By using the -o flag to specify the output file first rather than redirecting the output, this syntax avoids running into a permission denied error caused when using sudo.

检查当前版本,如有必要,在以下命令中更新:

sudo curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

接下来设置权限:

sudo chmod +x /usr/local/bin/docker-compose

然后,您将通过检查版本来验证安装是否成功:

docker-compose --version

这将打印出您安装的版本:

Outputdocker-compose version 1.29.2, build 5becea4c

现在您已经安装了 Docker Compose,您可以运行“Hello World”示例了。

第 2 步 — 使用 Docker Compose 运行容器

公共 Docker 注册表 Docker Hub 包含一个 Hello World 映像,用于演示和测试。 它说明了使用 Docker Compose 运行容器所需的最低配置:一个调用单个图像的 YAML 文件:

首先,您将为 YAML 文件创建一个目录并移入其中:

mkdir hello-world
cd hello-world

然后,您将创建 YAML 文件:

nano docker-compose.yml

将以下内容放入文件中,保存文件,然后退出文本编辑器:

码头工人-compose.yml

my-test:
    image: hello-world

YAML 文件中的第一行用作容器名称的一部分。 第二行指定使用哪个图像来创建容器。 当您运行命令 docker-compose up 时,它将按照您指定的名称 hello-world 查找本地图像。 完成此操作后,您将保存并退出文件。

您可以使用 docker images 命令手动查看系统上的图像:

docker images

当根本没有本地图像时,仅显示列标题:

OutputREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

现在,虽然仍在 ~/hello-world 目录中,但您将执行以下命令:

docker-compose up

第一次运行命令时,如果没有名为 hello-world 的本地映像,Docker Compose 将从 Docker Hub 公共存储库中拉取它:

OutputPulling my-test (hello-world:latest)...
latest: Pulling from library/hello-world
c04b14da8d14: Downloading [==================================================>] c04b14da8d14: Extracting [==================================================>]  c04b14da8d14: Extracting [==================================================>]  c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest
. . .

注意: 如果遇到关于 Docker 套接字的权限错误,这意味着您跳过了 如何在 Ubuntu 18.04 上安装和使用 Docker 的第 2 步。 返回并完成该步骤将启用在没有 sudo 的情况下运行 docker 命令的权限。


拉取镜像后,docker-compose 创建一个容器,附加并运行 hello 程序,这反过来又确认安装似乎正在运行:

Output. . .
Creating helloworld_my-test_1...
Attaching to helloworld_my-test_1
my-test_1 |
my-test_1 | Hello from Docker.
my-test_1 | This message shows that your installation appears to be working correctly.
my-test_1 |
. . .

然后它打印出它做了什么的解释:

Output of docker-compose up. . .
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
. . .

Docker 容器仅在命令处于活动状态时才会运行,因此一旦 hello 完成运行,容器就会停止。 因此,当您查看活动进程时,会出现列标题,但不会列出 hello-world 容器,因为它没有运行。

docker ps
OutputCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES

您可以使用 -a 标志来查看下一步需要的容器信息,该标志显示所有容器,而不仅仅是活动容器:

docker ps -a
OutputCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
06069fd5ca23        hello-world         "/hello"            35 minutes ago      Exited (0) 35 minutes ago                       drunk_payne

这将显示您在完成后删除容器所需的信息。

第 3 步 — 删除图像(可选)

为避免使用不必要的磁盘空间,您将删除本地映像。 为此,您需要使用 docker rm 命令删除所有引用该图像的容器,后跟 CONTAINER ID 或 NAME。 下面,您使用的是刚刚运行的 docker ps -a 命令中的 CONTAINER ID。 请务必替换容器的 ID:

docker rm 06069fd5ca23

删除所有引用该图像的容器后,您可以删除该图像:

docker rmi hello-world

结论

您现在已经安装了 Docker Compose,通过运行 Hello World 示例测试了您的安装,并删除了测试映像和容器。

虽然 Hello World 示例确认了您的安装,但简单的配置并未显示 Docker Compose 的主要优点之一——能够同时启动和关闭一组 Docker 容器。 要了解 Docker Compose 的强大功能,您可以查看这个实际示例,How To Configure a Continuous Integration Testing Environment with Docker and Docker Compose on Ubuntu 16.04 (注:本文适用于 Ubuntu 16.04 而不是 18.04)