1.安装
# 下载2.25版本的只有2.20版本以上支持include特性
sudo curl -L "https://github.com/docker/compose/releases/download/v2.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 授权可执行
sudo chmod +x /usr/local/bin/docker-compose
# 查看版本号
docker-compose --version
# 卸载
sudo rm /usr/local/bin/docker-compose
# 指定文件启动
docker-compose -f docker-compose.yml up -d
# 查看运行中的容器
docker-compose ps
# 停止容器而不删除
docker-compose stop
# 停止并删除正在运行的容器
docker-compose down
2.使用
由于服务很多,会出现所有服务同时链接nacos,导致重试次数过多,而出现服务启动失败现象,为了避免这种情况出现,使用docker-compose healthCheck机制,并将所有需要注册到nacos上的微服务添加依赖,等待被依赖服务启动成功,再启动相应的服务。
1. /opt/docker-deploy目录下创建docker-compose.yml内容如下
version: '3.8'
include:
- ./middleware/mysql-compose.yml
- ./middleware/nacos-compose.yml
- ./middleware/rabbitmq-compose.yml
- ./middleware/redis-compose.yml
- ./middleware/xxljob-compose.yml
- ./application/auth1-compose.yml
- ./application/auth2-compose.yml
2. 在 /opt/docker-deploy/middleware
1.nacos-compose.yml
version: '3.8'
services:
nacos-server:
restart: always
network_mode: host
container_name: nacos
environment:
- MODE=standalone
- NACOS_SERVER_IP=127.0.0.1
volumes:
- /opt/nacos/logs:/home/nacos/logs
- /opt/nacos/data:/home/nacos/data
image: nacos/nacos-server:v2.0.4
healthcheck:
test: [ "CMD", "curl", "-f", "http://127.0.0.1:8848/nacos/v1/console/health/readiness" ]
interval: 10s
timeout: 10s
retries: 10
depends_on:
mysql:
condition: service_healthy
2.nacos-compose.yml
version: '3.8'
services:
mysql:
restart: always
network_mode: host
privileged: true
container_name: mysql
volumes:
- /opt/mysql/log:/var/log/mysql
- /opt/mysql/data:/var/lib/mysql
- /opt/mysql/conf:/etc/mysql/conf.d
environment:
- MYSQL_ROOT_PASSWORD=op2023
image: mysql:5.7
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost", "-u", "root", "-p op2023" ]
interval: 10s
timeout: 10s
retries: 10
3.rabbitmq-compose.yml
services:
rabbitmq:
restart: always
network_mode: host
container_name: rabbitmq
environment:
- RABBITMQ_DEFAULT_USER=rabbit
- RABBITMQ_DEFAULT_PASS=op2023
volumes:
- /opt/rabbitmq/data:/var/lib/rabbitmq
image: rabbitmq:3.9.11-management
4.redis-compose.yml
services:
redis:
restart: always
container_name: redis
network_mode: host
volumes:
- /opt/redis/conf/redis.conf:/etc/redis/redis.conf
- /opt/redis/data:/data
image: redis:5.0.14
command: redis-server /etc/redis/redis.conf
5.xxljob-compose.yml
services:
xxl-job-admin:
environment:
- PARAMS=--server.port=10385 --spring.datasource.url=jdbc:mysql://localhost:3306/xxl_job?Unicode=true&characterEncoding=UTF-8 --spring.datasource.username=root --spring.datasource.password=op2023
network_mode: host
volumes:
- /opt/logs/xxljob:/data/applogs
container_name: xxl-job-admin
restart: always
image: xuxueli/xxl-job-admin:2.3.1
3. 在 /opt/docker-deploy/application
1.auth1-compose.yml
services:
iot-auth1:
container_name: auth1
restart: always
network_mode: host
environment:
- JVM_PARAMETERS=-Dfile.encoding=utf-8 -Xms512M -Xmx1024M -Dspring.profiles.active=dev -Dspring.cloud.nacos.config.max-retry=20 -Dspring.cloud.nacos.config.config-retry-time=30000 -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dnacos.suffix=nacos-dev
volumes:
- /opt/logs/auth2:/opt/logs
image: registry.cn-shanghai.aliyuncs.com/iot/auth1:5b0c1667
depends_on:
nacos-server:
condition: service_healthy
1.auth2-compose.yml
services:
iot-auth2:
container_name: auth2
restart: always
network_mode: host
privileged: true
pid: host
environment:
- JVM_PARAMETERS=-Dspring.profiles.active=dev -Xms512M -Xmx1536M -Dfile.encoding=utf-8 -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848
volumes:
- /opt/resources:/usr/share/myservice/resources
- /etc/netplan/:/var/opt/
- /opt/edgeGateway/:/run/
- /opt/appresource/media/business/:/home/
- /opt/sbin/:/mnt/
- /opt/logs/auth2:/opt/logs
image: registry.cn-shanghai.aliyuncs.com/iot/auth2:2f10e374
depends_on:
nacos-server:
condition: service_healthy
ubuntu自定义启动
背景:系统不重启的情况下,docker-compose重新启动能保证启动顺序,但是如果系统重启了,则容器的启动顺序不能保证,所以要配合定义ubuntu启动脚本来去实现。
# 1. /etc/systemd/system/下创建docker-compose-startup.service
# 2. docker-compose-startup.service文件内容是
[Unit]
Description=docker compose startup
After=docker.service
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'docker-compose -f /opt/docker-deploy/docker-compose.yml down && docker-compose -f /opt/docker-deploy/docker-compose.yml up -d'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
# 3. 运行以下命令来重新加载 systemd 守护进程的配置
sudo systemctl daemon-reload
# 4. 启用服务,使其在系统启动时自动运行
sudo systemctl enable docker-compose-startup.service
# 5. 检查服务状态
sudo systemctl status docker-compose-startup.service