docker compose

Docker compose

https://docs.docker.com/compose/

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.

      docker命令只能构建和运行一个容器。

      但是容器一般只建议运行一个服务,一般应用后台依赖若干个服务,例如数据库、web服务器。

      一起维护同一个应用的若干容器,就成为一件麻烦的事情。

     docker compose的诞生就是为了解决这个问题。

 Usage

      容器的构建,还是使用dockerfile

      容器组合的定义使用新的配置文件 docker-compose.yml, 配置文件支持公共配置定义 和  定制配置覆盖。

Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.

  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.

  3. Run docker-compose up and Compose starts and runs your entire app.

A docker-compose.yml looks like this:

version: '3'
services:
  web:
    build: .
    ports:
    - "5000:5000"
    volumes:
    - .:/code
    - logvolume01:/var/log
    links:
    - redis
  redis:
    image: redis
volumes:
  logvolume01: {}

For more information about the Compose file, see the Compose file reference.

Compose has commands for managing the whole lifecycle of your application:

  • 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

功能

The features of Compose that make it effective are:

      1) 多个独立环境在单机上运行。

         --- 我理解应该是 docker的功能。

      2)当容器被创建时候保留数据。

         --- 我理解应该是 docker的功能。

     3)只重新创建改变的容器,即dockerfile有改变,才重新构建新的容器。

         --- 这是docker compose的新功能。

     4)变量支持 + 在不同的环境中移动组合。

        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.

       ----   docker-compse.yml 配置文件可以感知系统的环境变量, 和此工具自己定义的变量(在.env文件中), 以及在此配种自己定义变量。

               这就促使其拥有跨环境的感知能力, 应对生产环境、测试环境、开发环境的改变。

               为什么需要这个功能, 我理解不同环境下,软件定义的行为是略有差异, 例如开发环境下, 所有打印全开, 生产环境下值开启 critical级别打印。

使用场景

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.

Development environments

让开发者使用一个命令就可以启动完整的应用,

docker-compose up

此阶段不考虑后台应用的复杂化架构和部署, 例如 高可用 高并发, 需要依赖 代理服务器 和 副本策略。

Automated testing environments

对于功能测试, 需要快速启动测试环境下的应用, 然后运行测试脚本,和拆除测试环境。

使用docker-compose只需要三个命令:

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

Single host deployments

在生产环境下,也可以使用此工具,应对生产环境下的单机部署。

此功能应对简单应用的部署, 不过大型应用, 会借助 kubernetes mesos 做分布式部署。

原文地址:https://www.cnblogs.com/lightsong/p/12243733.html