介绍 (Introduction)
Docker is a great tool for automating the deployment of Linux applications inside software containers, but to take full advantage of its potential each component of an application should run in its own individual container. For complex applications with a lot of components, orchestrating all the containers to start up, communicate, and shut down together can quickly become unwieldy.
Docker是自动化Linux应用程序在软件容器中的部署的出色工具,但是要充分利用Docker的潜力,应用程序的每个组件都应在自己的单独容器中运行。 对于具有许多组件的复杂应用程序,将所有容器编排在一起以一起启动,通信和关闭可能很快变得很麻烦。
The Docker community came up with a popular solution called Fig, which allowed you to use a single YAML file to orchestrate all of your Docker containers and configurations. This became so popular that the Docker team decided to make Docker Compose based on the Fig source, which is now deprecated. Docker Compose lets users orchestrate the processes of Docker containers, including starting up, shutting down, and setting up intra-container linking and volumes.
Docker社区提出了一种流行的解决方案,称为Fig ,它允许您使用单个YAML文件来编排所有Docker容器和配置。 它变得如此流行,以至于Docker团队决定基于Fig源(现在已弃用)来制作Docker Compose 。 Docker Compose允许用户协调Docker容器的过程,包括启动,关闭和设置容器内链接和卷。
In this tutorial, you’ll install the latest version of Docker Compose to help you manage multi-container applications on a Debian 10 server.
在本教程中,您将安装最新版本的Docker Compose,以帮助您在Debian 10服务器上管理多容器应用程序。
先决条件 (Prerequisites)
To follow this article, you will need:
要阅读本文,您将需要:
A Debian 10 server and a non-root user with sudo privileges. This initial server setup with Debian 10 tutorial explains how to set this up.
Debian 10服务器和具有sudo特权的非root用户。 Debian 10教程中的初始服务器设置介绍了如何进行设置。
Docker installed with the instructions from Step 1 and Step 2 of How To Install and Use Docker on Debian 10
按照如何在Debian 10上安装和使用Docker的 步骤1和步骤2的说明安装Docker
Note: Even though the Prerequisites give instructions for installing Docker on Debian 10, the docker
commands in this article should work on other operating systems as long as Docker is installed.
注意:即使前提条件给出了在Debian 10上安装Docker的说明,但只要安装了Docker,本文中的docker
命令就应该在其他操作系统上运行。
第1步-安装Docker Compose (Step 1 — Installing Docker Compose)
Although you can install Docker Compose from the official Debian repositories, it is several minor versions behind the latest release, so in this tutorial you’ll install it from Docker’s GitHub repository. The command that follows 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
.
尽管您可以从Debian官方存储库中安装Docker Compose,但它比最新版本落后几个次要版本,因此在本教程中,您将从Docker的GitHub存储库中安装它。 后面的命令与“ 发行”页面上的命令略有不同。 通过使用-o
标志首先指定输出文件,而不是重定向输出,此语法避免了在使用sudo
时遇到的“权限被拒绝”错误。
Check the current release and, if necessary, update it in the command that follows:
检查当前发行版,并在必要时在以下命令中对其进行更新:
sudo curl -L https://github.com/docker/compose/releases/download/1.25.3/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo curl -L https://github.com/docker/compose/releases/download/ 1.25.3 / docker -compose-`uname -s`-`uname -m` -o / usr / local / bin / docker-撰写
Next we’ll set the permissions:
接下来,我们将设置权限:
- sudo chmod +x /usr/local/bin/docker-compose 须藤chmod + x / usr / local / bin / docker-compose
Then we’ll verify that the installation was successful by checking the version:
然后,我们将通过检查版本来验证安装是否成功:
- docker-compose --version docker-compose --version
This will print out the version we installed:
这将打印出我们安装的版本:
Output
docker-compose version 1.25.3, build d4d1b42b
Now that we have Docker Compose installed, we’re ready to run a “Hello World” example.
现在我们已经安装了Docker Compose,我们准备运行一个“ Hello World”示例。
第2步-使用Docker Compose运行容器 (Step 2 — Running a Container with Docker Compose)
The public Docker registry, Docker Hub, includes a Hello World image for demonstration and testing. It illustrates the minimal configuration required to run a container using Docker Compose: a YAML file that calls a single image. We’ll create this minimal configuration to run our hello-world
container.
公用Docker注册表Docker Hub包含一个Hello World映像,用于演示和测试。 它说明了使用Docker Compose运行容器所需的最低配置:一个调用单个映像的YAML文件。 我们将创建此最小配置以运行我们的hello-world
容器。
First, create a directory for the YAML file and switch to it:
首先,为YAML文件创建目录并切换到该目录:
- mkdir hello-world mkdir hello-world
- cd hello-world cd hello-world
Then create the YAML file:
然后创建YAML文件:
- nano docker-compose.yml 纳米docker-compose.yml
Put the following contents into the file, save the file, and exit the text editor:
将以下内容放入文件中,保存文件,然后退出文本编辑器:
my-test:
image: hello-world
The first line in the YAML file is used as part of the container name. The second line specifies which image to use to create the container. When we run the docker-compose up
command, it will look for a local image by the name we specified, hello-world
. With this in place, we’ll save and exit the file.
YAML文件中的第一行用作容器名称的一部分。 第二行指定用于创建容器的图像。 当我们运行docker-compose up
命令时,它将通过我们指定的名称hello-world
查找本地映像。 有了这个,我们将保存并退出文件。
You can look manually at images on our system with the docker images
command:
您可以使用docker images
命令手动查看我们系统上的docker images
:
- docker images 码头工人图像
When there are no local images at all, only the column headings display:
如果根本没有本地图像,则仅显示列标题:
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
Now, while still in the ~/hello-world
directory, execute the following command:
现在,仍然在~/hello-world
目录中,执行以下命令:
- docker-compose up 码头工人组成
The first time you run the command, if there’s no local image named hello-world
, Docker Compose will pull it from the Docker Hub public repository:
首次运行该命令时,如果没有名为hello-world
本地映像,则Docker Compose将从Docker Hub公共存储库中提取该映像:
Output
Pulling my-test (hello-world:)...
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest
. . .
After pulling the image, docker-compose
creates a container, attaches, and runs the hello program, which in turn confirms that the installation appears to be working:
拉取映像后, 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 |
. . .
Then it prints an explanation of what it did:
然后,它打印出对其所做的解释:
Output
To generate this message, Docker took the following steps:
my-test_1 | 1. The Docker client contacted the Docker daemon.
my-test_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
my-test_1 | (amd64)
my-test_1 | 3. The Docker daemon created a new container from that image which runs the
my-test_1 | executable that produces the output you are currently reading.
my-test_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it
my-test_1 | to your terminal.
Docker containers only run as long as the command is active, so once hello
finished running, the container stopped. Consequently, when we look at active processes, the column headers will appear, but the hello-world
container won’t be listed because it’s not running:
Docker容器仅在命令处于活动状态时才运行,因此一旦hello
完成运行,该容器便会停止。 因此,当我们查看活动的进程时,将显示列标题,但是由于未运行hello-world
容器,因此不会列出该容器:
- docker ps 码头工人ps
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
You can see the container information, which you’ll need in the next step, by using the -a
flag. This shows all containers, not just active ones:
您可以使用-a
标志查看下一步需要的容器信息。 这显示了所有容器,而不仅仅是活动容器:
- docker ps -a 码头工人ps -a
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06069fd5ca23 hello-world "/hello" 35 minutes ago Exited (0) 35 minutes ago hello-world_my-test_1
This displays the information you’ll need to remove the container when you’re done with it.
显示完成后需要删除容器的信息。
步骤3 —删除图像(可选) (Step 3 — Removing the Image (Optional))
To avoid using unnecessary disk space, we’ll remove the local image. To do so, we’ll need to delete all the containers that reference the image using the docker rm
command, followed by either the CONTAINER ID
or the NAME
. In the following example, we’re using the CONTAINER ID
from the docker ps -a
command we just ran. Be sure to substitute the ID of your container:
为了避免使用不必要的磁盘空间,我们将删除本地映像。 为此,我们需要使用docker rm
命令删除所有引用该图像的容器,后跟CONTAINER ID
或NAME
。 在以下示例中,我们使用刚运行的docker ps -a
命令中的CONTAINER ID
。 确保替换容器的ID:
docker rm 06069fd5ca23
码头工人RM 06069fd5ca23
Once all containers that reference the image have been removed, we can remove the image:
删除所有引用该图片的容器后,我们可以删除该图片:
- docker rmi hello-world docker rmi hello-world
结论 (Conclusion)
You’ve installed Docker Compose on Debian 10, tested your installation by running a Hello World example, and removed the test image and container.
您已经在Debian 10上安装了Docker Compose,通过运行Hello World示例测试了安装,并删除了测试映像和容器。
While the Hello World example confirmed your installation, this basic configuration does not show one of the main benefits of Docker Compose — being able to bring a group of Docker containers up and down all at the same time. To see how to use Docker Compose in more detail, take a look at How To Install WordPress With Docker Compose.
虽然Hello World示例确认您已安装,但此基本配置并未显示Docker Compose的主要优点之一-能够同时上下移动一组Docker容器。 要更详细地了解如何使用Docker Compose,请查看如何使用Docker Compose 安装WordPress 。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-docker-compose-on-debian-10