如何在 CentOS 7 上安装 Docker Compose

在本教程中,我们将向您展示如何在您的 CentOS 7 服务器上安装 Docker Compose。 对于那些不知道的人,Docker Compose 是一个用于定义和配置多容器 docker 应用程序的命令行工具。 换句话说,我们可以说 docker-compose 用于链接多个容器并通过单个命令部署应用程序。

本文假设您至少具备 Linux 的基本知识,知道如何使用 shell,最重要的是,您将网站托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户下运行,如果不是,您可能需要添加 ‘sudo‘ 到命令以获取 root 权限。 我将向您展示在 CentOS 7 服务器上逐步安装 Docker Compose。

在 CentOS 7 上安装 Docker Compose

第 1 步。首先,让我们首先确保您的系统是最新的。

yum clean all
yum -y update

步骤 2. 使用 YUM 安装 Docker。

Docker 默认包含在 CentOS-Extras 存储库中。 要安装运行以下命令:

yum -y install docker
yum -y install device-mapper device-mapper-event device-mapper-libs device-mapper-event-libs

启动并启用 Docker 服务:

systemctl start docker.service
systemctl enable docker.service

并通过检查 Docker 的状态来验证您的工作:

systemctl status docker.service

步骤 3. 安装 Docker Compose。

安装 Docker 后,安装 Docker Compose。 首先,通过执行以下命令安装 EPEL 存储库:

yum install epel-release
yum install -y python-pip

然后你可以安装 Docker Compose:

pip install docker-compose

您还需要在 CentOS 7 上升级您的 Python 包以使 docker-compose 成功运行:

yum upgrade python*

使用以下命令检查 Docker Compose 版本:

docker-compose -v

步骤 4. 测试 Docker Compose。

现在我们已经安装了 Docker Compose,让我们用这个非常简单的例子来测试它,创建一个新目录并进入它:

mkdir hello-world
cd hello-world

创建一个新的 YAML 文件:

nano docker-compose.yml

在此文件中粘贴以下内容:

idroot-compose-test:
image: hello-world

接下来,在 hello-world 目录中执行以下命令:

sudo docker-compose up

输出应以以下内容开头:

Output of docker-compose up
Creating helloworld_idroot-compose-test_1...
Attaching to helloworld_idroot-compose-test_1
idroot-compose-test_1 | 
idroot-compose-test_1 | Hello from Docker.
idroot-compose-test_1 | This message shows that your installation appears to be working correctly.
idroot-compose-test_1 |

Docker 容器仅在命令处于活动状态时运行,因此容器将在测试完成运行时停止。

恭喜! 您已成功安装 Docker Compose。 感谢您使用本教程在您的 CentOS 7 系统上安装 Docker Compose。 如需更多帮助或有用信息,我们建议您查看 Docker 官方网站.

Save