什么是Docker Compose
Docker Compose是运行多容器Docker应用程序的工具。要使用Compose配置应用程序的服务,我们使用配置文件,然后执行单个命令,可以创建并启动配置中指定的所有服务。
Docker Compose适用于许多不同的项目,如:
在本教程中,我们将看到如何在Ubuntu 16.04机器上安装Docker Compose。
安装Docker
为了安装Docker Compose,我们需要Docker。首先,为官方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此时可以安装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 ComposeDocker 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文件创建一个简单的项目。
更多Docker相关教程见以下内容:
Docker安装应用(CentOS 6.5_x64)