多服务部署工具,通过一个docker-compose.yml模板文件,定义一组关联的应用容器为一个项目。
简单来说,就是当我们一个项目有多个服务需要启动时,通过docker-compose直接一键启动,就可以将所有相关的服务同时启动或者停止。
参考官方安装文档: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
说明 | 命令 |
---|---|
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 | 停止服务 |
使用docker-compose来启动springboot项目。
使用到springboot、redis、mysql组件。
redis配置文件: /docker/redis/redis.conf
# 开启 AOF 持久化
appendonly yes
# 指定 AOF 文件名
appendfilename "appendonly.aof"
# 指定 AOF 文件保存目录
dir /data
# 关闭 RDB 持久化
save ""
# 开启 RDB 持久化
#save 900 1
#save 300 10
#save 60 10000
# 禁用 Redis 的保护模式,允许外部连接
protected-mode no
# 允许其他服务访问
bind 0.0.0.0
# 指定 Redis 监听的端口
port 6379
# 指定日志生成的文件
logfile redis.log
mysql的配置文件:/docker/mysql/conf/my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
springboot: 使用7.6.3中打包的镜像springboot:1.0
参考本系列连接跳转
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:
# 先检查配置是否有异常
docker-compose config -q
# 启动所有容器
#第一次测试可以不使用-d
docker-compose up
一切正常,转为后台运行
# 先移除所有的
docker-compose down
# 再后台运行
docker-compose -d up
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
配置文件:/docker/elasticsearch/config/elasticsearch.yml
network.host: 0.0.0.0
discovery.type: single-node
配置文件: /docker/kibana/config/kibana.yml
server.name: kibana
server.host: "0.0.0.0"
elasticsearch.hosts: [ "http://elasticsearch:9200" ] #
elasticsearch.username: "admin"
elasticsearch.password: "admin"
i18n.locale: "zh-CN"
[“http://elasticsearch:9200”]这里elasticsearch是指容器的服务名,不设置这个,会导致kibana连接不上elasticsearch。
docker-compose.yml
version: '3.3'
services:
elasticsearch:
image: elasticsearch:7.17.7 # 使用的镜像名称
container_name: elasticsearch # 容器名称
ports: # 指定暴露的端口
- 9200:9200
- 9300:9300
environment:
- ES_JAVA_OPTS= -Xms2g -Xmx2g # 指定 JVM 内存大小
volumes: # 指定挂载目录
- /docker/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- /docker/elasticsearch/data:/usr/share/elasticsearch/data
- /docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins
- /docker/elasticsearch/logs:/usr/share/elasticsearch/logs
networks:
- bridge
kibana:
image: kibana:7.17.7
container_name: kibana
ports:
- 5601:5601
depends_on: # 服务依赖
- elasticsearch
volumes:
- /docker/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
networks:
- bridge
networks:
bridge:
#启动:
docker-compose up -d
# 卸载
docker-compose down
http://bidata:5601 访问