此文章是在阅读了 docker compose 官网后做的笔记,如果错请指正。
什么是docker 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.
其实官方描述就很简单到位,compose就是帮我们在单机环境下做容器编排的一个工具。通过ymal文件定义了各个容器信息,包括镜像、网络、数据卷等等信息,只要通过一个命令就可以把容器都给起来。
相比传统方式,我们部署多个容器,需要docker run … ,敲N多个命令。如果要配置网络,数据卷,那得敲个十几个命令不说,还得记住敲的内容是什么。compose就是帮助我们解决这个问题,他只需要一个yml文件,按照它定义的语法,告诉compose,它就自动帮我敲这些命令,从而达到帮我们部署多个容器目的。
有什么功能
1. 管理容器的生命周期
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
2. 单机环境下互相隔离
Multiple isolated environments on a single host
compose 使用 project name 在不同上下文之间互相隔离。因为每一个docker compose yml 是一个service
3. 保存数据
docker compose up
会找之前的容器是否有保存过数据(volume),如果有就会拿过来使用,这样就保证数据不会丢失。
4. 不会重复创建容器
只有容器被修改过,才会去重新创建一个新的容器。如果没有修改容器,docker compose up
跑多少次还是一样的。
5. 环境变量支持外部配置化
yml文件可以使用变量,通过外部文件指定值。
怎么用
1. 官方例子
官网例子已经很详细了,每一步都有相应命令,直接复制粘贴就能跑了。官网例子
- 创建好镜像。
- 定义好docker-compose.yml文件。将container的信息写在yml文件内。
- 运行
docker compose up
将服务起来
docker compose up -d
docker compose up -f 指定yml文件,如果有多个文件可以使用多个 -f
docker compose ps
docker compose run 启动单个服务,docker compose run web evn
docker compose stop 停止整个services
docker-compose down --volumes 移除container,并且删除数据卷
docker-compose exec redis sh 进入某个container
2. 环境变量文件
1.28 版本之前,compose 默认使用工作根目录下的.evn
结尾文件,--project-directory
参数可以覆盖默认配置文件
1.28 版本之后,compose 默认使用项目根目录下的.evn
结尾文件,--env-file
参数可以覆盖默认配置文件
语法
- key=value 形式
- #是注释
- 空行会被忽略
- 引号符不会做特别处理,意思是引号也是值的一部分
$ cat .env
TAG=v1.5
$ cat docker-compose.yml
version: '3'
services:
web:
image: "webapp:${TAG}"
3. profiles 是否启动某个服务
在某个服务下使用profile 属性,可以控制是否启动容器。
profiles: ["frontend", "debug"]
profiles:
- frontend
- debug
version: "3.9"
services:
frontend:
image: frontend
profiles: ["frontend"]
phpmyadmin:
image: phpmyadmin
depends_on:
- db
profiles:
- debug
backend:
image: backend
db:
image: mysql
启动命令
$ docker compose up
$ docker compose --profile debug up
$ COMPOSE_PROFILES=debug docker-compose up
$ docker compose --profile frontend --profile debug up
$ COMPOSE_PROFILES=frontend,debug docker compose up
4. 共用通用服务
方式1:多个配置文件
By default, Compose reads two files, a docker-compose.yml and an optional docker-compose.override.yml file.By convention, the docker-compose.yml contains your base configuration. The override file, as its name implies, can contain configuration overrides for existing services or entirely new services.
docker compose 可以使用多个配置文件定义相关service内容。比如每个环境需要部署的配置不一样,可以将不同的配置放着不同配置文件中,像Java 的application.yml一样,可以指定生效的是什么文件。compose 是通过-f
参数指定配置文件。第一个 -f 是主配置文件,后面的文件都必须以第一个指定的文件的相对路径存在。
$ cat docker-compose.yml
web:
image: example/my_web_app:latest
depends_on:
- db
- cache
db:
image: postgres:latest
cache:
image: redis:latest
$ cat docker-compose.override.yml
web:
build: .
volumes:
- '.:/code'
ports:
- 8883:80
environment:
DEBUG: 'true'
db:
command: '-d'
ports:
- 5432:5432
cache:
ports:
- 6379:6379
$ cat docker-compose.prod.yml
web:
ports:
- 80:80
environment:
PRODUCTION: 'true'
cache:
environment:
TTL: '500'
$ docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
方式二:使用extends 关键字
这种方式适合不同的 service yml 公用同一个服务。
首先将公用服务定义一个正常的service 的yml,然后在其他需要使用的地方通extends来指定使用什么容器。
$ cat common.yml
services:
app:
build: .
environment:
CONFIG_FILE_PATH: /code/config
API_KEY: xxxyyy
cpu_shares: 5
$ cat docker-compose.yml
services:
webapp:
extends:
file: common.yml
service: app
command: /code/run_web_app
ports:
- 8080:8080
depends_on:
- queue
- db
queue_worker:
extends:
file: common.yml
service: app
command: /code/run_worker
depends_on:
- queue
5. network
默认情况下,compose会为service里面的每个容器创建一个网络,并且将每个容器都链接到这个网络,其他容器链接到这个网络也能互相通信。常创建的网络名称是以项目文件名称来命名,也可以通过--project-name
指定。
- 指定使用已经存在的网络,external = true
- 通过networks自定义网络,还可以通过属性default设置为默认
- Links 链接到另外一个服务。a link alias (“SERVICE:ALIAS”), or just the service name.
6. scale 扩缩容
-
修改官网例子的docker-compose.yml文件,将端口设置去掉
-
启动
docker compose up -d
-
使用
docker-compose up --scale web=5 -d
扩容web 容器。注意:docker-compose才有 scale参数
-
缩容