官方介绍

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 works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
三步骤:
Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
    1. dockerfile 保证我们的项目在任何地方可以运行
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
    1. services 是 什么服务
    2. docker-compose.yml 怎么写?
  3. Run docker-compose up and Compose starts and runs your entire app.
    1. 启动项目

作用:批量容器编排
compose 是docker 官方的开源项目。需要安装
dockerfile 让程序在任何地方运行。web 服务、Redis、MySQL、nginx……多个容器
即使有100个服务,只要文件没问题,就可以一键上线
官方docker-compose.yml 编写案例:

  1. version: '2.0'
  2. services:
  3. web:
  4. build: .
  5. ports:
  6. - "5000:5000"
  7. volumes:
  8. - .:/code
  9. - logvolume01:/var/log
  10. links:
  11. - redis
  12. redis:
  13. image: redis
  14. volumes:
  15. logvolume01: {}

Compose:重要概念

  • 服务services :容器、应用(web、Redis、MySQL……)
  • 项目project:一组关联的容器。博客(web、mysql、wp)

Compose安装

  1. 下载(加速:http://get.daocloud.io/) ```shell sudo curl -L “https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose

可能有点慢,用下面这个也行

curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.2/docker-compose-`uname -s-uname -m` > /usr/local/bin/docker-compose

  1. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/376408/1598600879134-11cc26fa-fed1-4a07-9c0c-b2b22d97b105.png#align=left&display=inline&height=133&margin=%5Bobject%20Object%5D&name=image.png&originHeight=266&originWidth=919&size=37108&status=done&style=none&width=459.5)
  2. 2. 授权并测试
  3. ```shell
  4. chmod +x /usr/local/bin/docker-compose
  5. #测试
  6. docker-compose version

image.png

体验

官网: https://docs.docker.com/compose/gettingstarted/

  1. 写应用 app.py
  2. 写Dockerfile 应用打包为镜像
  3. 写docker-compose yaml 文件(定义整个服务,需要的环境:web、redis)完整的上线服务
  4. 启动compose项目(docker-compose up)
    1. version: '3' //官方docker-compose.yml解读
    2. services:
    3. web: //第一种镜像: 自己构建的
    4. build: .
    5. ports:
    6. - "5000:5000"
    7. redis: //第二种镜像: 直接使用网上已经有的
    8. image: "redis:alpine"

流程:

  1. 创建网络
  2. 执行docker-compose yaml 文件
  3. 启动服务