我们前面的文章学习了 docker ,为什么还要 Compose 呢?Compose到底是个啥玩意?
Docker Compose 可以来轻松的高效的管理容器,定义运行多个容器
咱们一起来看看官方的介绍 docs
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. To learn more about all the features of Compose, see the list of features.
讲了三个点:
- Compose 可以定义和运行多个容器
- 需要使用给到 YAML 配置文件
- 单个命令就可以创建和启动所有的服务
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.
Docker Compose 可以运行在所有的环境中
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 the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary.
三个步骤:
- 需要定义好 Dockerfile ,保证它在任何环境都能运行
- 在 docker-compose.yml 文件中定义好 services,那么这个 yml 文件如何写呢?services 咋定义呢
- 使用
docker-compose binary
启动项目
总结上述官方说明:
Docker Compose 用于批量容器编排
如果一个项目中的多个微服务(几十个或者几百个),我们都一个一个的使用docker run
是不是很傻?而且对于运维来说也是一个非常不友好的事情,优化这样的问题,我们有了 Docker Compose
Compose 在 Docker 中默认就有吗?
Docker 中默认是没有 Compose 的,Compose 是 Docker 官方的开源项目,我们使用 Compose ,是需要自己另外安装的
Compose 的 yml 文件如何编写?
一起来看看官方文档的 yml 是怎样的结构:
A docker-compose.yml
looks like this:
version: "3.9" # optional since v1.27.0
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {