Compose

Fig是一个基于Docker的用于快速搭建开发环境的工具,目前Fig团队已经加入Docker公司。Fig通过一个配置文件来管理多个Docker容器,非常适合组合使用多个容器进行开发的场景。Fig可以和Docker一起来构建基于Docker的复杂应用,CoreOS的功能强大但是配置比较复杂,而Fig相对而言比较简单,但是很难在多台服务器上做扩展,如何使用Fig构建多个容器的复杂应用并且把这些应用部署到基于CoreOS 的生产环境可以参考这篇文章。从Docker官方收购Fig也可以看到该项目的重要性,Fig目前尚未发布1.0版本。

Compose is a tool for defining and runnign multi-container Docker Applications.

The Compose file is a YAML file defining services, networks and volumes. The default path for a Compose file is ./docker-compose.yml.

Using Compose with Swarm

The actual extent of integration depends on which version of the Compose file format you are using.

Version 2, the recommended format. This is specified with a version: '2' entry at the root of the YAML.

Docker Compose and Docker Swarm aim to have full integration, meaning you can point a Compose app at a Swarm cluster and have it all just work as if you were using a single Docker host.

As long as the Swarm cluster is configured to use the overlay driver, or custom driver which supports multi-host networking.

Extending services and Composes file

Compose supports two methods of sharing common configuration

  1. Extending an entire Compose file by using multiple Compose files.
  2. Extending individual services with the extends field.

By default, Compose reads two files, a docker-compose.yml and an optional docker-compose.override.yml f ile.

docker-compose -f docker-compose.yml -f docker-compose.admin.yml \ run dbadmin db-backup

Networking in Composes

only version 2 of teh Compose file format

By default Compose sets up a single network for your app ,Each container for a service joins the default network andis both reachable by other containers on that network. and discoverable by them at a hostname identical to the container name.

version: '2'

services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres


When you run docker-compose up, the following happens:

A network called myapp_default is created.
A container is created using web’s configuration. It joins the network myapp_default under the name web.
A container is created using db’s configuration. It joins the network myapp_default under the name db.

customize network:
networks key field.


### Command-line Reference

Define and run multi-container applications with Docker.

Usage: docker-compose [-f=...] [options] [COMMAND] [ARGS...] docker-compose -h|--help

Options: -f, --file FILE Specify an alternate compose file (default: docker-compose.yml) -p, --project-name NAME Specify an alternate project name (default: directory name) --verbose Show more output -v, --version Print version and exit

Commands: build Build or rebuild services config Validate and view the compose file create Create services down Stop and remove containers, networks, images, and volumes events Receive real time events from containers help Get help on a command kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pulls service images restart Restart services rm Remove stopped containers run Run a one-off command scale Set number of containers for a service start Start services stop Stop services unpause Unpause services up Create and start containers version Show the Docker-Compose version information

Each configuration has a project name. If you supply a -p flag, you can specify a project name. If you don’t specify the flag, Compose uses the current directory name. See also the COMPOSE_PROJECT_NAME e

CLI Environment Variables

Sets the project name. This value is prepended along with the service name to the container container on start up. For example, if you project name is myapp and it includes two services db and web then compose starts containers named myapp_db_1 and myapp_web_1 respectively.

  • COMPOSE_PROJECT_NAME Setting this is optional. If you do not set this, the COMPOSE_PROJECT_NAME defaults to the basename of the project directory.

  • COMPOSE_FILE

Specify the file containing the compose configuration. If not provided, Compose looks for a file named docker-compose.yml in the current directory and then each parent directory in succession until a file by that name is found.

  • COMPOSE_API_VERSION

Specify the file containing the compose configuration. If not provided, Compose looks for a file named docker-compose.yml in the current directory and then each parent directory in succession until a file by that name is found.

  • DOCKER_HOST

Sets the URL of the docker daemon. As with the Docker client, defaults to unix:///var/run/docker.sock

  • DOCKER_TLS_VERIFY

When set to anything other than an empty string, enables TLS communication with the docker daemon.

  • DOCKER_CERT_PATH

Configures the path to the ca.pem, cert.pem, and key.pem files used for TLS verification. Defaults to ~/.docker

  • COMPOSE_HTTP_TIMEOUT

Configures the time (in seconds) a request to the Docker daemon is allowed to hang before Compose considers it failed. Defaults to 60 seconds.

Controlling startup order in Compose

Your can control the order of service startup with the depends_on options.