Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
Compose是用于定义和运行多容器Docker应用程序的工具。通过Compose,您可以使用YAML文件来配置应用程序的服务。然后,使用单个命令创建并启动配置中的所有服务。要了解有关Compose的所有功能的更多信息,请参阅功能列表。
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
Compose能在所有环境中工作: 生产、预生产、开发、测试以及CI工作流。您可以在常见用例中详细了解每种情况。
Using Compose is basically a three-step process:
- Define your app’s environment with a
Dockerfileso it can be reproduced anywhere. - Define the services that make up your app in
docker-compose.ymlso they can be run together in an isolated environment. - Run
docker-compose upand Compose starts and runs your entire app.
A docker-compose.yml looks like this:
version: '2.0'services:web:build: .ports:- "5000:5000"volumes:- .:/code- logvolume01:/var/loglinks:- redisredis:image: redisvolumes:logvolume01: {}
For more information about the Compose file, see the Compose file reference.
Compose has commands for managing the whole lifecycle of your application:
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 documentation
- Getting started with Compose
- Get started with Django
- Get started with Rails
- Get started with WordPress
- Frequently asked questions
- Command line reference
-
Features
The features of Compose that make it effective are:
- 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 uses a project name to isolate environments from each other. You can make use of this project name in several different contexts:
on a dev host, to create multiple copies of a single environment, such as when you want to run a stable copy for each feature branch of a project
on a CI server, to keep builds from interfering with each other, you can set the project name to a unique build number
on a shared host or dev host, to prevent different projects, which may use the same service names, from interfering with each other
The default project name is the basename of the project directory. You can set a custom project name by using the -p command line option or the COMPOSE_PROJECT_NAME environment variable.
Preserve volume data when containers are created
创建容器时持久化卷数据
Compose preserves all volumes used by your services. When docker-compose up runs, if it finds any containers from previous runs, it copies the volumes from the old container to the new container. This process ensures that any data you’ve created in volumes isn’t lost.
Compose保留服务使用的所有卷。在docker-combo up运行时,如果它找到以前运行的任何容器,它会将卷从旧容器复制到新容器。此过程可确保您在卷中创建的任何数据不会丢失。
If you use docker-compose on a Windows machine, see Environment variables and adjust the necessary environment variables for your specific needs.
如果您在Windows计算机上使用docker-compose,请参阅环境变量,并根据您的特定需求调整必要的环境变量。
Only recreate containers that have changed
Compose caches the configuration used to create a container. When you restart a service that has not changed, Compose re-uses the existing containers. Re-using containers means that you can make changes to your environment very quickly.
Compose缓存用于创建容器的配置。当您重新启动未更改的服务时,撰写将重新使用现有容器。重复使用容器意味着您可以非常快速地更改环境。
Variables and moving a composition between environments
Compose supports variables in the Compose file. You can use these variables to customize your composition for different environments, or different users. See Variable substitution for more details.
Compose支持compose文件中的变量。您可以使用这些变量为不同的环境或不同的用户自定义您的合成。有关更多详细信息,请参见变量替换。
You can extend a Compose file using the extends field or by creating multiple Compose files. See extends for more details.
Common use cases
Compose can be used in many different ways. Some common use cases are outlined below.
Compose可以以多种不同的方式使用。下面概述了一些常见的用例。
Development environments
When you’re developing software, the ability to run an application in an isolated environment and interact with it is crucial. The Compose command line tool can be used to create the environment and interact with it.
当您开发软件时,在隔离环境中运行应用程序并与之交互的能力至关重要。compose命令行工具可用于创建环境并与之交互。
The Compose file provides a way to document and configure all of the application’s service dependencies (databases, queues, caches, web service APIs, etc). Using the Compose command line tool you can create and start one or more containers for each dependency with a single command (docker-compose up).
compose file提供了一种记录和配置应用程序的所有服务依赖项 (数据库、队列、缓存、web服务api等) 的方法。使用compose命令行工具,您可以使用单个命令为每个依赖项创建和启动一个或多个容器 (docker-compose up)。
Together, these features provide a convenient way for developers to get started on a project. Compose can reduce a multi-page “developer getting started guide” to a single machine readable Compose file and a few commands.
总之,这些功能为开发人员提供了一种方便的方式来开始项目。compose可以将多页 “开发人员入门指南” 简化为单个机器可读的compose文件和一些命令。
Automated testing environments
An important part of any Continuous Deployment or Continuous Integration process is the automated test suite. Automated end-to-end testing requires an environment in which to run tests. Compose provides a convenient way to create and destroy isolated testing environments for your test suite. By defining the full environment in a Compose file, you can create and destroy these environments in just a few commands:
$ docker-compose up -d$ ./run_tests$ docker-compose down
Single host deployments
Compose has traditionally been focused on development and testing workflows, but with each release we’re making progress on more production-oriented features. You can use Compose to deploy to a remote Docker Engine. The Docker Engine may be a single instance provisioned with Docker Machine or an entire Docker Swarm cluster.
For details on using production-oriented features, see compose in production in this documentation.
有关使用面向生产的功能的详细信息,请参阅本文档中的 “在生产中编写”。
Release notes
To see a detailed list of changes for past and current releases of Docker Compose, 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.
Docker Compose正在积极开发中。如果你需要帮助,想做出贡献,或者只是想和志同道合的人谈论这个项目,我们有许多开放的沟通渠道。
- To report bugs or file feature requests: use the issue tracker on Github.
- To talk about the project with people in real time: join the
#docker-composechannel on the Docker Community Slack. - To contribute code or documentation changes: submit a pull request on Github.
