共计 3208 个字符,预计需要花费 9 分钟才能阅读完成。
导读 | Docker Compose 是一个运行多容器 Docker 应用的工具。Compose 通过一个配置文件来配置一个应用的服务,然后通过一个命令创建并启动所有在配置文件中指定的服务。 在本教程中,我们将看到如何在 Ubuntn 16.04 上安装 Docker Compose。 |
我们需要安装 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"
更新包数据库,并使用 apt 安装 Docker
$ 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 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 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
容器只能在命令(LCTT 译注:此处应为容器中的命令)还处于活动状态时运行,因此当测试完成运行时,容器将停止运行。
本文是关于在 Ubuntu 16.04 中安装 Docker Compose 的教程。我们还看到了如何通过一个 YAML 格式的 Compose 文件构建一个简单的项目。
via: https://www.unixmen.com/container-docker-compose-ubuntu-16-04/
作者:Giuseppe Molica 译者:Locez 校对:wxy
本文由 LCTT 原创编译,Linux 中国 荣誉推出
![post-qrcode](https://www.xgss.net/wp-content/uploads/2023/07/二维码-微信公众号256x256.jpg)