如何在 Ubuntu 16.04 上安装 Docker Compose

介绍

Docker是自动部署软件容器内的Linux应用程序的绝佳工具,但为了充分利用其潜力,应用程序的每个组件都应该在其自己的单独容器中运行。

Docker 社区提出了一个受欢迎的解决方案,名为 Fig,它允许您使用单个 YAML 文件来编排您的所有 Docker 容器和配置. 这变得如此受欢迎,以至于 Docker 团队决定基于 Fig 源创建 _Docker Compose,现在已经被剥夺了。

在本教程中,我们将向您展示如何安装 Docker Compose 的最新版本,以帮助您管理多容器应用程序。

前提条件

要遵循这篇文章,你需要一个 Ubuntu 16.04 服务器,具有以下内容:

  • 具有 sudo 特权的非 root 用户 (Initial Server Setup with Ubuntu 16.04)(https://andsky.com/tech/tutorials/initial-server-setup-with-ubuntu-16-04)解释了如何设置此设置)
  • Docker 与 [如何在 Ubuntu 16.04 上安装和使用 Docker 中的 步骤 1步骤 2 的说明一起安装(https://andsky.com/tech/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04)

一旦这些在场,你准备跟随。

<$>[注] 注: 尽管前提规定在 Ubuntu 16.04 上安装 Docker,但本文中的docker命令应该在其他操作系统上运行,只要安装了 Docker。

步骤 1 – 安装 Docker Compose

虽然我们可以从官方Ubuntu存储库中安装Docker Compose,但它是最新的版本背后的几个小版本,所以我们会从Docker的GitHub存储库中安装Docker Compose。下面的命令略有不同于你会在 Releases页面上找到的命令。

我们将检查 当前版本,如果需要,请在下面的命令中更新它:

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

接下来我们将设置许可证:

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

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

1docker-compose --version

这将打印我们安装的版本:

1[secondary_label Output]
2docker-compose version 1.18.0, build 8dd22a9

现在我们已经安装了 Docker Compose,我们已经准备好运行一个Hello World示例。

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

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

首先,我们将为 YAML 文件创建一个目录,并进入其中:

1mkdir hello-world
2cd hello-world

然后,我们将创建YAML文件:

1nano docker-compose.yml

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

1[label docker-compose.yml]
2my-test:
3 image: hello-world

YAML 文件中的第一行被用作容器名称的一部分,第二行指定使用哪个图像来创建容器,当我们运行docker-compose up命令时,它将以我们指定的名称搜索本地图像,hello-world

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

1docker images

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

1[secondary_label Output]
2REPOSITORY TAG IMAGE ID CREATED SIZE

现在,当我们还在~/hello-world目录中时,我们将执行以下命令:

1docker-compose up

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

1[secondary_label Output]
2Pulling my-test (hello-world:latest)...
3latest: Pulling from library/hello-world
4c04b14da8d14: Downloading [==================================================>] c04b14da8d14: Extracting [==================================================>]  c04b14da8d14: Extracting [==================================================>]  c04b14da8d14: Pull complete
5Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
6Status: Downloaded newer image for hello-world:latest
7. . .

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

1[secondary_label Output]
2. . .
3Creating helloworld_my-test_1...
4Attaching to helloworld_my-test_1
5my-test_1 |
6my-test_1 | Hello from Docker.
7my-test_1 | This message shows that your installation appears to be working correctly.
8my-test_1 |
9. . .

然后,他给出了他所做的一些解释:

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

Docker 容器只在命令活跃时运行,所以一旦你好完成运行,容器就停止运行,因此,当我们查看活跃过程时,将出现列头,但不会列出你好容器,因为它没有运行。

1docker ps
1[secondary_label Output]
2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

我们可以看到容器信息,我们将在下一步需要,使用a标志,显示所有容器,而不仅仅是活跃的:

1docker ps -a
1[secondary_label Output]
2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
306069fd5ca23 hello-world         "/hello"            35 minutes ago Exited (0) 35 minutes ago drunk_payne

这显示了我们需要删除容器时我们已经完成的信息。

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

为了避免使用不必要的磁盘空间,我们将删除本地图像。 要做到这一点,我们需要使用docker rm命令删除所有引用图像的容器,然后是 CONTAINER ID 或 NAME。

1docker rm 06069fd5ca23

一旦删除所有引用图像的容器,我们可以删除图像:

1docker rmi hello-world

结论

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

虽然 Hello World 示例证实了我们的安装,但简单的配置并不显示 Docker Compose 的主要好处之一 - 能够同时将一组 Docker 容器带上下。

Published At
Categories with 技术
comments powered by Disqus