您的位置:首页 > 运维架构 > Docker

Docker学习之Compose

2016-05-24 14:55 1066 查看

Docker Compose学习概览

compose是一款用来定义和运行 多容器docker应用 的工具,使用compose,你可以使用一个compose文件来配置你的应用服务,然后,使用单个命令行,就可以从这些配置文件中创建和启动所有的服务。

compose很适合开发、测试以及(stage environments),还包括 CI工作流,常见的用例见:Common Use Cases.

compose的使用依靠一下三个步骤:

First: 使用Dockerfile定义你所使用的app环境,这样便于随处重用.

       Second:  在 docker-compose.yml 中定义app中的服务,这样这些服务可以在独立的环境中运行.

       Last: 运行 docker-compose up  ,这样compose就会启动并且运行所有的app.

一个 docker-compose.yml 文件形如:

docker-compose.yml
version: '2'

services:

   web:

      build:

      ports:

      - "5000:5000"

       volumes:

      - .:/code

      - logvolume01: /var/log

      links:

      - redis:

           image: redis

volumes:

      logvolume01:  { }
compose中有管理你应用生命周期的命令:

> start, stop and rebuild services
> view the  status of running services
> stream the log output of running services
> run a one-off command on a service

compose 文档

Installing Compose
Getting Started
Get started with Django
Get started with Rails
Get started with WordPress
Frequently asked questions
Command line reference
Compose file reference

compose特色

Multiple
isolated environments on a single host
Preserve
volume data when containers are created
Only
recreate containers that have changed
Variables
and moving a composition between environments

Multiple isolated environments on a single host

compose使用项目名称来进行彼此之间的环境隔离,这里的项目名称可以在一下场景下使用:

> 在设备端主机(on dev host),创建单一环境的多份拷贝(例如,你想稳定的运行一个项目中每个特色分支)
> 在一个 CI服务器上,为了保持彼此编译的相互干扰,你可以设置项目名称为唯一的编译号
> 在一个共享主机或者设备端主机上,通过使用相同的服务器名称来不同项目防止彼此之间的干涉

默认的项目名称是项目目录名,你可以通过 -p 命令行参数设置一个惯用的项目名称或者 COMPOSE_PROJECT_NAME 环境变量

Preserve volume data when containers are created

compose 保存你服务所使用的所有 volumes,当 docker-compose up 命令运行起来后,如果发现先前的任何容器已经在运行,就将旧的容器中的 volumes拷贝到新的容器中区. 这个过程确保你在volumes中创建的任何数据不会丢失.

Only recreate containers that have changed

compose缓存配置通常用来进行容器的创建,当你重启一个并未改变的服务,compose会重用已经存在的容器,重用容器意味着对你的环境做出快速改变

Variables and moving a composition between environments

compose文件支持变量,你可以使用这些变量针对不同的环境或者用户定制你的应用,你可以使用 extends 扩展一个compose文件或者创建多个 compose文件

常见使用用例

 1. 开发环境
  当你开发软件时,在一个独立的环境中运行和交互一个应用的要求是非常严格的, compose命令行工具可以用来创建环境和交互。compose 文件提供一种独立的文档化和配置方法帮助所有的应用服务(例如,数据库、队列、缓存、web service APIs等等)。使用compose命令行工具你可以创建或者启动一个或者多个容器,彼此之间使用独立的命令行命令 (docker-compose up)。
  这些特色为开发者提供了一个便利的方法启动一个项目。将厚重的类似 “developer getting started guide”文件简化为单个机器可读的 compose文件和很少的命令。

  2. 自动测试环境
    任何持续发布或者持续集成过程的重要组成部分便是自动测试组件。自动 end-to-end  测试需要一个运行这个测试的环境。compose提供一个便捷的方式来创建和销毁独立的测试环境来帮助测试组件。通过在 compose文件中定义全部的环境的参数,这样就可以通过以下命令进行这些环境的创建和销毁:

$  docker-compose  up  -d
$  ./run_tests
$  docker-compose down

3. 单个主机部署
  compose传统上聚焦在开发和测试工作流上,在每个发布版本上,都在面向产品的特色上做出了很多改进,你可以使用compose来部署到一个远程的 Docker Engine上。Docker Engine可能会是一个 Docker Machine或者 一个全部 Docker Swarm集群提供的实例。

Release Notes

To see a detailed list of changes for past and current releases of Docker Compose, please
refer to the CHANGELOG.

Getting help

Docker Compose is under active development. If you need help, would like to contribute, or simply want to talk about the project with like-minded individuals, we have a number of open channels for communication.

To report bugs or file feature requests: please use the issue tracker on Github.

To talk about the project with people in real time: please join the
#docker-compose
 channel
on freenode IRC.

To contribute code or documentation changes: please submit a pull request on Github.

For more information and resources, please visit the Getting Help project page.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  docker