LINUX入门:容器:Ubuntu 16.04安装Docker Compose详解

《LINUX入门:容器:Ubuntu 16.04安装Docker Compose详解》要点:
本文介绍了LINUX入门:容器:Ubuntu 16.04安装Docker Compose详解,希望对您有用。如果有疑问,可以联系我们。

什么是Docker Compose

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

Docker Compose适用于许多分歧的项目,如:

  • 开发:使用Compose命令行工具,我们创建(并交互)一个孤立的环境,这将承载正在开发的应用程序.
  • 通过使用Compose文件,开发人员将记录并配置所有应用程序的服务依赖关系.
  • 自动测试:此用例需要运行测试的环境.Compose提供了一种方便的方式来管理测试套件的隔离测试环境.完整的环境在Compose文件中定义.
  • Docker Compose是在Fig 配置文件中创建的,这个社区项目现在没有使用.

在本教程中,我们将看到如安在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 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文件创立一个简单的项目.

更多LINUX教程,尽在脚本之家PHP学院专栏。欢迎交流《LINUX入门:容器:Ubuntu 16.04安装Docker Compose详解》!

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。