官方介绍
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:
- Define your app’s environment with a
Dockerfile
so it can be reproduced anywhere.- dockerfile 保证我们的项目在任何地方可以运行
- 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 怎么写?
- Run
docker-compose up
and Compose starts and runs your entire app.- 启动项目
作用:批量容器编排
compose 是docker 官方的开源项目。需要安装
dockerfile 让程序在任何地方运行。web 服务、Redis、MySQL、nginx……多个容器
即使有100个服务,只要文件没问题,就可以一键上线
官方docker-compose.yml 编写案例:
version: '2.0'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
Compose:重要概念
- 服务services :容器、应用(web、Redis、MySQL……)
- 项目project:一组关联的容器。博客(web、mysql、wp)
Compose安装
- 下载(加速: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

2. 授权并测试
```shell
chmod +x /usr/local/bin/docker-compose
#测试
docker-compose version
体验
官网: https://docs.docker.com/compose/gettingstarted/
- 写应用 app.py
- 写Dockerfile 应用打包为镜像
- 写docker-compose yaml 文件(定义整个服务,需要的环境:web、redis)完整的上线服务
- 启动compose项目(docker-compose up)
version: '3' //官方docker-compose.yml解读
services:
web: //第一种镜像: 自己构建的
build: .
ports:
- "5000:5000"
redis: //第二种镜像: 直接使用网上已经有的
image: "redis:alpine"
流程:
- 创建网络
- 执行docker-compose yaml 文件
- 启动服务