文章目录
- 9. compose编排
- 9.1 介绍
- 9.2 安装
- 9.3 compose常用命令
- 9.4 编排微服务
- 9.4.1 准备docker-compose.yml
- 9.4.2 启动服务
- 9.4.3 测试验证
9. compose编排
9.1 介绍
多服务部署工具,通过一个docker-compose.yml模板文件,定义一组关联的应用容器为一个项目。
简单来说,就是当我们一个项目有多个服务需要启动时,通过docker-compose直接一键启动,就可以将所有相关的服务同时启动或者停止。
9.2 安装
参考官方安装文档:https://docs.docker.com/compose/install/linux/
以下两种方式都可以:
第一种:包管理器安装
sudo yum install docker-compose-plugin
第二种:手工安装
curl -SL https://github.com/docker/compose/releases/download/v2.20.3/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# 通常不需要执行这句
#sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
#查看版本
docker-compose --version
#卸载方法
sudo rm /usr/local/bin/docker-compose
9.3 compose常用命令
说明 | 命令 |
---|---|
docker-compose -h | 帮助 |
docker-compose up | 启动所有docker-compose服务 |
docker-compose up -d | 启动所有docker-compose服务并后台运行 |
docker-compose down | 停止并删除容器,网络,卷,镜像 |
docker-compose exec yml里面的服务id | 进入容器内部,停止并删除容器,网络,卷,镜像 |
docker-compose ps | 展示当前运行的所有容器 |
docker-compose top | 展示当前运行的容器进程 |
docker-compose logs yml的服务id | 查看容器输出日志 |
docker-compose config | 检查配置 |
docker-compose config -q | 检查配置,有问题才输出 |
docker-compose restart | 重启服务 |
docker-compose start | 启动服务 |
docker-compose stop | 停止服务 |
9.4 编排微服务
使用docker-compose来启动springboot项目。
使用到springboot、redis、mysql组件。
9.4.1 准备docker-compose.yml
注意需要提前准备下面这些:
redis配置文件: /docker/redis/redis.conf
springboot: 使用7.6.3中打包的镜像springboot:1.0
mysql的配置文件:/docker/mysql/conf/my.cnf
version: "3"
services:
bootService:
build: .
image: springboot:1.0
container_name: springboot
environment:
SPRING_PROFILES_ACTIVE: prod
ports:
- "9002:9002"
volumes:
- /app/microService:/data
networks:
- bridge
depends_on:
- redis
- mysql
redis:
image: redis:6.2.13
container_name: redis
privileged: true
ports:
- 6379:6379
environment:
- TZ=Asia/Shanghai
- TAKE_FILE_OWNERSHIP=true
volumes:
- /docker/redis/data:/data
- /docker/redis/redis.conf:/etc/redis/redis.conf
networks:
- bridge
command: redis-server /etc/redis/redis.conf
mysql:
image: mysql:5.7
container_name: mysql
privileged: true
ports:
- "3306:3306"
networks:
- bridge
volumes:
- /docker/mysql/log:/var/log/mysql
- /docker/mysql/data:/var/lib/mysql
- /docker/mysql/conf/:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
command: --default-authentication-plugin=mysql_native_password #解决外部无法访问
networks:
bridge:
9.4.2 启动服务
# 先检查配置是否有异常
docker-compose config -q
# 启动所有容器
#第一次测试可以不使用-d
docker-compose up
一切正常,转为后台运行
# 先移除所有的
docker-compose down
# 再后台运行
docker-compose -d up
9.4.3 测试验证
Commands:
build Build or rebuild services
config Parse, resolve and render compose file in canonical format
cp Copy files/folders between a service container and the local filesystem
create Creates containers for a service.
down Stop and remove containers, networks
events Receive real time events from containers.
exec Execute a command in a running container.
images List images used by the created containers
kill Force stop service containers.
logs View output from containers
ls List running compose projects
pause Pause services
port Print the public port for a port binding.
ps List containers
pull Pull service images
push Push service images
restart Restart service containers
rm Removes stopped service containers
run Run a one-off command on a service.
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker Compose version information
wait Block until the first service container stops
例如重启服务:
# 查看服务
docker-compose ls
#重启服务
docker-compose restart bootService
# 启动服务
docker-compose start bootService
# 停止服务
docker-compose stop bootService