8.1 简介

作用:单击版的批量容器编排 eg:Compose是Docker官方的开源项目,需要安装

8.1.1 使用三步骤:

Using Compose is basically a three-step process:

前提:项目的(jar)包放到以下步骤1和2相同的文件夹

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

8.1.2 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: {}

8.1.3 Compose重要概念

  • 服务service:容器、应用。(web、redis、mysql)
  • 项目project,一组关联的容器(博客:web、mysql、redis)

8.2 安装

官网地址:https://docs.docker.com/compose/install/ eg:不同操作系统安装相应的版本

8.3 yml 配置规则

官网配置规则地址:https://docs.docker.com/compose/compose-file/#compose-file-structure-and-examples

  1. version: '' # 版本
  2. services: # 服务
  3. 服务1: web
  4. # 服务配置
  5. images
  6. build
  7. network
  8. .....
  9. 服务2: redis
  10. ....
  11. 服务3: redis
  12. ....
  13. # 其他配置 网络/卷、全局规则
  14. volumes:
  15. networks:
  16. configs:
  • denpends_on

image.png

8.5 后台启动

  1. docker -d
  2. docker-compose up -d

image.png

8.6 小结

未来项目只要有 docker-compose 文件。 按照这个规则,启动编排容器。!
公司: docker-compose。 直接启动。
网上开源项目: docker-compose 一键搞定。
假设项目要重新部署打包

  1. docker-compose up --build # 重新构建!