设置docker容器开机按顺序启动

本文介绍了如何编辑docker-compose文件及创建脚本,通过cron任务配置实现docker容器开机按顺序自动启动的方法。
摘要由CSDN通过智能技术生成

设置docker容器开机按顺序启动

  1. 编辑docker-compose文件

    version: '2.3'
    services:
      #项目名称,用docker-compose做管理时,每个项目用这里配置的名称进行单独管理
    ## nacos  
      nacos:
        # 根据需要修改版本
        image: nacos/nacos-server:2.0.3
        container_name: nacos-standalone
        restart: always
        environment:
          - PREFER_HOST_MODE=hostname
          - MODE=standalone
        volumes:
          - ./nacos/logs/:/home/nacos/logs
          - ./nacos/data/:/home/nacos/data
        ports:
          - "8848:8848"
          - "7848:7848"
          - "9848:9848"
        healthcheck:
          test: ["CMD","curl","-f","http://localhost:8848/nacos"]
          interval: 10s
          timeout: 30s
          retries: 3
    ## rabbitmq
      rabbitmq:
        image: rabbitmq:3-management-alpine
        container_name: rabbitmq
        build: ./mq
        restart: always
        ports:
          - 5672:5672
          - 15672:15672
          - 1883:1883
    #    deploy:
    #      resources:
    #        limits:
    #          memory: 10240M
        environment:
          - RABBITMQ_DEFAULT_USER=admin
          - RABBITMQ_DEFAULT_PASS=admin
    ## minio	  
      minio:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: minio/minio
        container_name: minio
        hostname: "minio"
        restart: always
        environment: 
            MINIO_ACCESS_KEY: beex
            MINIO_SECRET_KEY: beex9527
        volumes:
            - ./minio-data:/data
            - ./minio-config:/root/.minio
        #开放端口
        ports:
            - 9000:9000
            - 9001:9001 # 控制台端口
        privileged: true
        command: server --console-address ':9001' /data  #指定容器中的目录 /data
        #网络连接模式
        network_mode: "bridge"
        privileged: true
    ## mongo
      mongo:
        image: mongo
        container_name: mongo
        restart: always
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: root
        ports:
          - 27017:27017
        volumes:
          - ./mongo-data/:/data/db
      mongo-express:
        image: mongo-express
        container_name: mongo-express
        restart: always
        ports:
          - 8081:8081
        environment:
          ME_CONFIG_MONGODB_ADMINUSERNAME: root
          ME_CONFIG_MONGODB_ADMINPASSWORD: root
    ## redis
      redis:
        image: redis:5.0.9-alpine
        container_name: redis-5
        # build: ./redis
        restart: always
        ports:
          - 6379:6379
        volumes:
          - ./redis-data:/data:rw
      gateway:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-gateway
        container_name: ic-gateway
        #打包路径,即docker build的路径
        build: ./api/gateway/
        restart: always
        #环境变量
        environment:
          CONFIG_SERVER: 172.19.188.43:8848           #配置中心地址
        #挂载目录
        volumes:
          - /etc/localtime:/etc/localtime:ro            #时间同步
          - ./api/gateway-lib:/home/beex-gateway/lib
          - ./api/gateway/:/home/beex-gateway
          - ./logs:/logs
        #开放端口
        ports:
          - "9990:9000"
          - "9777:8777"
        networks:
          - 246-multi-tenant
        depends_on:
          nacos:
            condition: service_healthy
        healthcheck:
          test: ["CMD","curl","-S","http://localhost:9000/doc.html#/beex-api-gateway/authenticate-controller/authenticateUsingGET"]
          interval: 20s
          timeout: 30s
          retries: 3
          start_period: 20s
    ## tenant
      tenant:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-tenant
        container_name: ic-tenant
        #打包路径,即docker build的路径
        build: ./api/tenant/
        restart: always
        #环境变量
        environment:
          CONFIG_SERVER: 172.19.188.43:8848           #配置中心地址
        #挂载目录
        volumes:
          - /etc/localtime:/etc/localtime:ro            #时间同步
          - ./api/tenant/:/home/beex-tenant
          - ./api/lib:/home/beex-tenant/lib
          - ./logs:/logs
        #开放端口
        ports:
          - "9989:8096"
        healthcheck:
          test: ["CMD","curl","-S","http://localhost:8096/renewal/test"]
          interval: 30s
          timeout: 30s
          retries: 3
          start_period: 30s
        depends_on:
          nacos:
            condition: service_healthy
          gateway:
            condition: service_healthy
        networks:
          - 246-multi-tenant
    ## core		
      core:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-core
        container_name: ic-core
        #打包路径,即docker build的路径
        build: ./api/core/
        restart: always
        environment:
          CONFIG_SERVER: 172.19.188.43:8848        #配置中心地址
          CONFIG_NAMESPACE:
        depends_on:
          nacos:
            condition: service_healthy
          tenant:
            condition: service_healthy
        #挂载目录
        volumes:
          - /etc/localtime:/etc/localtime:ro            #时间同步
          - ./api/core/:/home/beex-core
          - ./api/lib:/home/beex-core/lib
          - ./logs:/logs
          - ./api/core/db/migration:/db/migration
        #开放端口
        ports:
          - "9991:8090"
        networks:
          - 246-multi-tenant
      device:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-device
        container_name: ic-device
        #打包路径,即docker build的路径
        build: ./api/device
        restart: always
        environment:
          CONFIG_SERVER: 172.19.188.43:8848           #配置中心地址
        #挂载目录
        volumes:
          - /etc/localtime:/etc/localtime:ro            #时间同步
          - ./api/device/:/home/beex-device
          - ./api/lib:/home/beex-device/lib
          - ./logs:/logs
          - ./api/device/db/migration:/db/migration
        depends_on:
          nacos:
            condition: service_healthy
          tenant:
            condition: service_healthy
        #开放端口
        ports:
          - "9998:8089"
        networks:
          - 246-multi-tenant
      factory:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-factory
        container_name: ic-factory
        #打包路径,即docker build的路径
        build: ./api/factory/
        restart: always
        environment:
          CONFIG_SERVER: 172.19.188.43:8848           #配置中心地址
        #挂载目录
        volumes:
          - ./api/factory/:/home/beex-factory
          - ./api/lib:/home/beex-factory/lib
          - /etc/localtime:/etc/localtime:ro            #时间同步
          - ./logs:/logs
          - ./api/factory/db/migration:/db/migration
        #开放端口
        ports:
          - "9992:8092"
        networks:
          - 246-multi-tenant
        depends_on:
          nacos:
            condition: service_healthy
          tenant:
            condition: service_healthy
      file:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-file
        container_name: ic-file
        #打包路径,即docker build的路径
        build: ./api/file/
        restart: always
        environment:
          CONFIG_SERVER: 172.19.188.43:8848          #配置中心地址
        #挂载目录
        volumes:
          - /etc/localtime:/etc/localtime:ro
          - ./api/file/:/home/beex-file
          - ./api/lib:/home/beex-file/lib
          - ./logs:/logs
        #开放端口
        ports:
          - "9996:8091"
        networks:
          - 246-multi-tenant
        depends_on:
          nacos:
            condition: service_healthy
      fitting:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-fitting
        container_name: ic-fitting
        #打包路径,即docker build的路径
        build: ./api/fitting/
        restart: always
        environment:
          CONFIG_SERVER: 172.19.188.43:8848         #配置中心地址
        #挂载目录
        volumes:
          - /etc/localtime:/etc/localtime:ro            #时间同步
          - ./api/fitting/:/home/beex-fitting
          - ./api/lib:/home/beex-fitting/lib
          - ./logs:/logs
          - ./api/fitting/db/migration:/db/migration
        #开放端口
        ports:
          - "9997:8099"
        networks:
          - 246-multi-tenant
        depends_on:
          nacos:
            condition: service_healthy
          tenant:
            condition: service_healthy
      warehouse:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-warehouse
        container_name: ic-warehouse
        #打包路径,即docker build的路径
        build: ./api/warehouse/
        restart: always
        environment:
          CONFIG_SERVER: 172.19.188.43:8848           #配置中心地址
        #挂载目录
        volumes:
          - /etc/localtime:/etc/localtime:ro            #时间同步
          - ./api/warehouse/:/home/beex-warehouse
          - ./api/lib:/home/beex-warehouse/lib
          - ./logs:/logs
          - ./api/warehouse/db/migration:/db/migration
        #开放端口
        ports:
          - "9993:8093"
        networks:
          - 246-multi-tenant
        depends_on:
          nacos:
            condition: service_healthy
          tenant:
            condition: service_healthy
      shop:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-shop
        container_name: ic-shop
        #打包路径,即docker build的路径
        build: ./api/shop/
        restart: always
        environment:
          CONFIG_SERVER: 172.19.188.43:8848           #配置中心地址
        #挂载目录
        volumes:
          - /etc/localtime:/etc/localtime:ro            #时间同步
          - ./api/shop/:/home/beex-shop
          - ./api/lib:/home/beex-shop/lib
          - ./logs:/logs
          - ./api/shop/db/migration:/db/migration
        #开放端口
        ports:
          - "9994:8089"
        networks:
          - 246-multi-tenant
        depends_on:
          nacos:
            condition: service_healthy
          tenant:
            condition: service_healthy
      report:
        #镜像名,拉取镜像时也是用这个名字作为地址
        image: ic-report
        container_name: ic-report
        #打包路径,即docker build的路径
        build: ./api/report
        restart: always
        environment:
          CONFIG_SERVER: 172.19.188.43:8848           #配置中心地址
        #挂载目录
        volumes:
          - /etc/localtime:/etc/localtime:ro
          - ./api/report/:/home/beex-report
          - ./api/lib:/home/beex-report/lib
          - ./logs:/logs
          - ./api/report/db/migration:/db/migration
        #开放端口
        ports:
          - "9995:8094"
        depends_on:
          nacos:
            condition: service_healthy
          tenant:
            condition: service_healthy
        networks:
          - 246-multi-tenant
    networks:
      246-multi-tenant:
        external: true
        driver: bridge
    
    
    
    1. 编写脚本文件docker-compose.sh

      #!/bin/bash
      
      # 1. 进入docker-compose.yaml文件所在目录
      cd /home/inscre
      # 2. 输出日志 到日志文件  记录脚本执行步骤
      echo '进入目录' >> /home/inscre/sh_log.txt
      # 验证是否进入到了对应目录
      ls >> /home/inscre/sh_log.txt
      # 由于docker-compose命令跟其他常规命令路径不一样,路径为 /usr/local/bin/docker-compose,常规命令路径为bin/bash等,所以crontab默认执#行的为常规路径命令。需要使用绝对路径。
      # /usr/local/bin/docker-compose  docker-compsoe 的绝对路径  
      # docker-compose.yaml docker-compose文件的所在目录 由于脚本开始就已经进入compsoe文件的当前目录  所以无需指定目录,直接指定文件名即可
      # down 需要执行的compose命令
      /usr/local/bin/docker-compose -f docker-compose.yaml down
      # 输出日志 到日志文件  记录脚本执行步骤
      echo "停止成功" /home/inscre/sh_log.txt
      # 验证 上述 docker-compsoe down命令是否执行成功
      docker ps >> /home/inscre/sh_log.txt
      # 输出日志 到日志文件  记录脚本执行步骤
      echo "docker ps 完成" /home/inscre/sh_log.txt
      # 由于docker-compose命令跟其他常规命令路径不一样,路径为 /usr/local/bin/docker-compose,常规命令路径为bin/bash等,所以crontab默认执#行的为常规路径命令。需要使用绝对路径。
      # /usr/local/bin/docker-compose  docker-compsoe 的绝对路径  
      # docker-compose.yaml docker-compose文件的所在目录 由于脚本开始就已经进入compsoe文件的当前目录  所以无需指定目录,直接指定文件名即可
      # up -d 需要执行的compose命令
      /usr/local/bin/docker-compose -f docker-compose.yaml up -d
      # 输出日志 到日志文件  记录脚本执行步骤
      echo "docker-compose up -d 完成" /home/inscre/sh_log.txt
      # 验证 上述 docker-compsoe up -d 命令是否执行成功
      docker ps >> /home/inscre/sh_log.txt
      # 输出日志 到日志文件  记录脚本执行步骤
      echo "docker ps 完成" /home/inscre/sh_log.txt
      # 输出日志 到日志文件  记录脚本执行步骤
      echo '重启完成' >> /home/inscre/sh_log.txt
      
      1. 进入脚本文件所在目录
      # 先进入脚本文件所在目录
      cd /home/inscre/
      
      1. 赋予脚本文件权限
      #赋予脚本文件权限
      chmod 777 docker-compose.sh
      
      1. 打开cron任务

        • 打开定时任务配置文件
         crontab -e
        
        • 在配置文件加入开机自启任务
        # @reboot 需要开机执行的脚本文件所在目录
        @reboot /home/inscre/docker-compose.sh
        
        • 退出编辑模式
        ctrl + X
        
        • 提示:是否保存当前文件。按Y键保存,N键不保存,C键取消

          Y
          
        • 重新载入配置

          service crond reload
          
        • 查看设置结果

        crontab -l
        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值