容器:Ubuntu 16.04 上的 Docker Compose容器:Ubuntu 16.04 上的 Docker Compose容器:Ubuntu 16.04 上的 Docker Compose容器:Ubuntu 16.04 上的 Docker Compose
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

容器:Ubuntu 16.04 上的 Docker Compose

发表 admin at 2025年2月28日
类别
  • 未分类
标签

什么是 Docker Compose

Docker Compose 是一个用于运行多容器 Docker 应用程序的工具。要使用 Compose 配置应用程序的服务,我们使用配置文件,然后执行单个命令,就可以创建并启动配置中指定的所有服务。

Docker Compose 对于许多不同的项目都很有用,例如:

  • 开发:使用 Compose 命令行工具,我们创建一个隔离的环境(并与之交互),该环境将托管正在开发的应用程序。
    通过使用 Compose 文件,开发人员可以记录并配置应用程序的所有服务依赖项。
  • 自动化测试:此用例需要一个运行测试的环境。Compose 提供了一种管理测试套件的隔离测试环境的便捷方法。完整的环境在 Compose 文件中定义。

Docker Compose 是根据 Fig 源代码制作的,这是一个社区项目,目前未使用。

在本教程中,我们将了解如何在 Ubuntu 16.04 计算机上安装 Docker Compose。

安装 Docker

我们需要 Docker 来安装 Docker Compose。首先,添加官方 Docker 存储库的公钥:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add-

接下来,将 Docker 存储库添加到

apt

来源列表:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

更新软件包数据库并安装 Docker

apt

:

sudo apt-get update
sudo apt install docker-ce

在安装过程结束时,应启动 Docker 守护进程并使其能够在启动时加载。我们可以使用以下命令检查其状态:

sudo systemctl status docker
---------------------------------

● docker.service - Docker Application Container Engine
 Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
 Active: active (running) 

安装 Docker 组合

此时就可以安装Docker Compose了。通过执行以下命令下载当前版本:



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

使下载的二进制可执行文件:

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

检查 Docker Compose 版本:

docker-compose -v

输出应该是这样的:

docker-compose version 1.14.0, build c7bdf9e

测试 Docker Compose

Docker Hub 包含一个用于演示目的的 Hello World 映像,说明使用 Docker Compose 运行容器所需的配置。

创建一个新目录并移入其中:

mkdir hello-world
cd hello-world

创建一个新的 YAML 文件:

$EDITOR docker-compose.yml

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

unixmen-compose-test:
 image: hello-world

注意:第一行用作容器名称的一部分。

保存并退出。

运行容器

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

sudo docker-compose up

如果一切正确,这应该是 Compose 显示的输出:

Pulling unixmen-compose-test (hello-world:latest)...
latest: Pulling from library/hello-world
b04784fba78d: Pull complete
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest
Creating helloworld_unixmen-compose-test_1 ... 
Creating helloworld_unixmen-compose-test_1 ... done
Attaching to helloworld_unixmen-compose-test_1
unixmen-compose-test_1 | 
unixmen-compose-test_1 | Hello from Docker!
unixmen-compose-test_1 | This message shows that your installation appears to be working correctly.
unixmen-compose-test_1 | 
unixmen-compose-test_1 | To generate this message, Docker took the following steps:
unixmen-compose-test_1 | 1. The Docker client contacted the Docker daemon.
unixmen-compose-test_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
unixmen-compose-test_1 | 3. The Docker daemon created a new container from that image which runs the
unixmen-compose-test_1 | executable that produces the output you are currently reading.
unixmen-compose-test_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it
unixmen-compose-test_1 | to your terminal.
unixmen-compose-test_1 | 
unixmen-compose-test_1 | To try something more ambitious, you can run an Ubuntu container with:
unixmen-compose-test_1 | $ docker run -it ubuntu bash
unixmen-compose-test_1 | 
unixmen-compose-test_1 | Share images, automate workflows, and more with a free Docker ID:
unixmen-compose-test_1 | https://cloud.docker.com/
unixmen-compose-test_1 | 
unixmen-compose-test_1 | For more examples and ideas, visit:
unixmen-compose-test_1 | https://docs.docker.com/engine/userguide/
unixmen-compose-test_1 | 
helloworld_unixmen-compose-test_1 exited with code 0

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

结论

关于在 Ubuntu 16.04 计算机上安装 Docker Compose 的教程到此结束。我们还了解了如何通过 YAML 格式的 Compose 文件创建一个简单的项目。

©2015-2025 Norria support@norria.com