docker-compose安装和使用(自启、redis、mysql、rabbitmq、activemq、es、nginx、java应用)

1.在线安装docker-compose:

参考官网:https://docs.docker.com/compose/install/other/

docker-compose安装及简单入门

[Docker] docker-compose使用教程

Docker系列教程22-docker-compose.yml常用命令

shell 复制代码
# 安装(加速下载https://ghproxy.com/)
sudo curl -L https://ghproxy.com/https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

# 赋权
sudo chmod +x /usr/local/bin/docker-compose

# 查看版本
docker-compose -v

# 可以获取帮助
docker-compose -h

2、离线安装docker-compose:

参考:Docker - 离线安装 docker-compose(以CentOS系统为例)

(1)首先访问 docker-composeGitHub 版本发布页面:
(2)下载docker-compose-Linux-x86_64

由于服务器是 CentOS 系统,则下载 Linux 版本的 Compose

Assets内下载:docker-compose-Linux-x86_64

(3)将其移动到 /usr/local/bin ,并改名为"docker-compose"

将下载下来的"docker-compose-Linux-x86_64 "文件上传到服务器上,然后执行如下命令将其移动到 /usr/local/bin ,并改名为"docker-compose"。

shell 复制代码
sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
(4)添加可执行权限:
shell 复制代码
sudo chmod +x /usr/local/bin/docker-compose
(5)测试是否安装成功

最后使用 docker-compose -v 命令测试是否安装成功(如果提示"没有那个文件或目录"可以重启下服务器再试试):

shell 复制代码
docker-compose -v

3、卸载

直接删除下载的二进制文件即可,linux命令
sudo rm /usr/local/bin/docker-compose

4、使用命令:

注意:

1、要在docker-compos.yml文件所在的目录执行该命令才可关闭对应的容器

会停止并删除容器,docker网络等

2、compose文件启动是有顺序的,通过depends_on来指定该服务所依赖的服务,以提前依赖服务的启动顺序,但不是容器内部程序启动成功才去启动下一个容器,容器内部程序启动有耗时,如果你的程序之间互相有依赖,尽量分开写docker-compose文件,避免容器启动失败。

shell 复制代码
# 第一次部署jar的命令
docker-compose up -d zaxq-rabbitmq
# 以后更新jar后,直接重启即可
docker-compose restart zaxq-rabbitmq
# 查看日志
docker-compose logs zaxq-rabbitmq --tail 1000

# 关闭一组服务
docker-compose down
# 创建并启动一组容器(1、若某个容器的配置文件改动了,执行该命令会创建新的容器并启动,2、如果配置文件没有改动,只是某个容器关掉了,则会启动关闭的容器,3、否则对启动的容器没有影响)
docker-compose up -d

# 停止容器在创建
docker-compose stop zaxq-rabbitmq
# 删除容器在创建
docker-compose rm zaxq-rabbitmq

5、实战:

docker-compose.yml配置

vi docker-compose.yml

yml 复制代码
version: "3"
services:
    zaxq-eureka:
        image: java:8
        container_name: zaxq-eureka
        ports:
            - 9527:9527
        volumes:
            - /run/media/root/D/zaxq/java/log/eureka/:/logs/zaxq/eureka/       
            - /run/media/root/D/zaxq/java/jar/:/usr/jar/      
        working_dir: /usr/jar/
        command: bash -c 'java -jar -Duser.timezone=GMT+8 zaxq-eureka.jar --spring.profiles.active=test'
        environment:
            - SET_CONTAINER_TIMEZONE=true
            - CONTAINER_TIMEZONE=Asia/Shanghai
            
    zaxq-gateway:
        image: java:8
        container_name: zaxq-gateway
        ports:
            - 8082:8082
        volumes:
            - /run/media/root/D/zaxq/java/log/gateway/:/logs/zaxq/gateway/
            - /run/media/root/D/zaxq/java/tmp/gateway/:/data/zaxq/tmp/gateway/  
            - /run/media/root/D/zaxq/java/jar/:/usr/jar/      
        working_dir: /usr/jar/
        command: bash -c 'java -jar -Duser.timezone=GMT+8 zaxq-gateway.jar --spring.profiles.active=test'  
        environment:
            - SET_CONTAINER_TIMEZONE=true
            - CONTAINER_TIMEZONE=Asia/Shanghai        
        depends_on:
            - zaxq-eureka
    
    zaxq-auth:
        image: java:8    
        container_name: zaxq-auth
        ports:
            - 8089:8089
        volumes:
            - /run/media/root/D/zaxq/java/log/auth/:/run/media/root/D/zaxq/java/log/auth/
            - /run/media/root/D/zaxq/java/resources/auth/:/run/media/root/D/zaxq/java/resources/auth/
            - /run/media/root/D/zaxq/java/tmp/:/java-project/zaxq/tmp/
            - /run/media/root/D/zaxq/java/jar/:/usr/jar/      
        working_dir: /usr/jar/
        command: bash -c 'java -jar -Duser.timezone=GMT+8 zaxq-auth.jar --spring.profiles.active=test'   
        environment:
            - SET_CONTAINER_TIMEZONE=true
            - CONTAINER_TIMEZONE=Asia/Shanghai        
        depends_on:
            - zaxq-eureka
            - zaxq-gateway

    zaxq-rabbitmq:
        image: java:8
        container_name: zaxq-rabbitmq
        ports:
            - 1032:1032
        volumes:
            - /run/media/root/D/zaxq/java/log/rabbitmq/:/run/media/root/D/zaxq/java/log/rabbitmq/
            - /run/media/root/D/zaxq/java/resources/rabbitmq/:/run/media/root/D/zaxq/java/resources/rabbitmq/
            - /run/media/root/D/zaxq/java/jar/:/usr/jar/      
        working_dir: /usr/jar/
        command: bash -c 'java -jar -Duser.timezone=GMT+8 zaxq-rabbitmq.jar --spring.profiles.active=comm,test'   
        environment:
            - SET_CONTAINER_TIMEZONE=true
            - CONTAINER_TIMEZONE=Asia/Shanghai        
        depends_on:
          - zaxq-eureka
          - zaxq-gateway            
          - zaxq-basedata

6、制作成开机自启的服务

# 第一次部署jar的命令
docker-compose up -d zaxq-rabbitmq
# 以后更新jar后,直接重启即可
docker-compose restart zaxq-rabbitmq
# 查看日志
docker-compose logs zaxq-rabbitmq --tail 1000

# 关闭一组服务
docker-compose down
# 创建并启动所有容器(1、若某个容器的配置文件改动了,执行该命令会创建新的容器并启动,2、如果配置文件没有改动,只是某个容器关掉了,则会启动关闭的容器,3、否则对启动的容器没有影响)
docker-compose up -d

# 停止容器在创建
docker-compose stop zaxq-rabbitmq
# 删除容器在创建
docker-compose rm zaxq-rabbitmq

------------------------------------------------------------------------------
#制作成开机自启的服务 原文链接:https://blog.csdn.net/catoop/article/details/110000462

vi /usr/lib/systemd/system/pet-docker-compose.service
[Unit]
Description=zaxq-docker-compose.service
After=docker.service systemd-networkd.service systemd-resolved.service
Requires=docker.service


[Service]
Type=simple
Restart=on-failure
RestartSec=5
# WorkingDirectory=/path/to/docker/compose/project
ExecStart=/usr/local/bin/docker-compose -f /run/media/root/D/zaxq/java/jar/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f /run/media/root/D/zaxq/java/jar/docker-compose.yml down

[Install]
WantedBy=multi-user.target

# 授权
chmod +x /usr/lib/systemd/system/pet-docker-compose.service
# 设置开机自启
sudo systemctl enable pet-docker-compose.service
# 启动
sudo systemctl start zaxq-docker-compose.service

7、其他扩展部署redis、mysql、rabbitmq、activemq、es、nginx、java应用

docker-compose(redis、mysql、rabbitmq、activemq、es、nginx、java应用)

version: "3"
services:
    redis:
        image: redis:6.2.6-alpine # 指定服务镜像,最好是与之前下载的redis配置文件保持一致
        container_name: redis # 容器名称
        #restart: on-failure # 重启方式
        restart: always
        environment:
          - TZ=Asia/Shanghai # 设置时区
        volumes: # 配置数据卷
            - /home/yyt/data/redis/data:/data
            - /home/yyt/data/redis/redis.conf:/etc/redis/redis.conf          
        ports: # 映射端口
          - 6379:6379
        sysctls: # 设置容器中的内核参数
          - net.core.somaxconn=1024
          #- vm.overcommit_memory=1
        #command: /bin/sh -c "cat /etc/sysctl.conf && echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf && redis-server /etc/redis/redis.conf --appendonly yes" # 指定配置文件并开启持久化
        command:
            - sh
            - -c
            - |
                sysctl vm.overcommit_memory=1
                redis-server /etc/redis/redis.conf --appendonly yes
        privileged: true
 

    mysql:
        #image: mysql:8.0
        image: mysql
        container_name: mysql
        restart: always
        ports:
            - 3307:3306
        volumes:
            - /home/yyt/data/mysql/config:/etc/my.cnf
            - /home/yyt/data/mysql/data:/var/lib/mysql/         
        environment:
            - MYSQL_ROOT_PASSWORD=ne7v51grgfs4rpe
            - TZ=Asia/Shanghai # 设置时区

    rabbitmq:
        image: rabbitmq
        # 固定主机名(防止持久化数据用户数据丢失问题)
        hostname: myrabbitmq
        container_name: rabbitmq
        privileged: true
        ports:
            - 5672:5672
            - 15672:15672
        volumes:
            - /home/yyt/zaxq/rabbitmq/conf/rabbitmq:/etc/rabbitmq
            - /home/yyt/zaxq/rabbitmq/data:/var/lib/rabbitmq
            - /home/yyt/zaxq/rabbitmq/log:/log/lib/rabbitmq
        environment:
            - RABBITMQ_DEFAULT_VHOST=/
            - RABBITMQ_DEFAULT_USER=admin
            - RABBITMQ_DEFAULT_PASS=zaxq@yyt
            - TZ=Asia/Shanghai
        # 这下面的命令全部到容器内部执行。 如果管理界面缺少overview等菜单 可能是浏览器的问题,推荐使用google浏览器  
        #command:
        #    - sh
        #    - -c
        #    - |
        #        chgrp rabbitmq /etc/rabbitmq ; chmod g+s /etc/rabbitmq
        #        rabbitmq-plugins enable rabbitmq_management
        #         rabbitmqctl add_user zaxq zaxq
        #         rabbitmqctl set_user_tags zaxq administrator
        #         rabbitmqctl add_vhost /zaxq
        #         rabbitmqctl set_permissions -p /zaxq zaxq ".*" ".*" ".*"
        
    activemq:
        image: webcenter/activemq:5.14.3
        container_name: activemq
        restart: always
        ports:
            - 8161:8161
            - 61616:61616
        volumes:
            - /home/yyt/data/activemq/data:/data/activemq
            - /home/yyt/data/activemq/log:/var/log/activemq         
        environment:
            - ACTIVEMQ_NAME=activemq
            - ACTIVEMQ_ADMIN_LOGIN=admin
            - ACTIVEMQ_ADMIN_PASSWORD=yyt@activemq

    es:
        image: elasticsearch:7.17.10
        container_name: es
        restart: always
        environment:
            - "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
            - "TZ=Asia/Shanghai"
            - "discovery.type=single-node"
            #- cluster.initial_master_nodes=es01
            - "ELASTIC_PASSWORD=fkuy29wjzb18n3e"
            - cluster.name=docker-cluster
            - node.name=es01
            - network.host=0.0.0.0
            #配置允许跨域访问
            - http.cors.enabled=true
            - http.cors.allow-origin=*
            - http.cors.allow-headers=Authorization,X-Requested-With,Content-Length,Content-Type
            # 开启安全校验 并 配置密码执行: elasticsearch-setup-passwords interactive
            - xpack.security.enabled=true
            - xpack.security.transport.ssl.enabled=true
        ulimits:
            memlock:
                soft: -1
                hard: -1
            nofile:
                soft: 65536
                hard: 65536
        ports:
            - "9200:9200"
        logging:
            driver: "json-file"
            options:
                max-size: "50m"
                max-file: "3"
        volumes:
            #- /home/yyt/data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
            - /home/yyt/data/elasticsearch/plugins:/usr/share/elasticsearch/plugins
            - /home/yyt/data/elasticsearch/data:/usr/share/elasticsearch/data
            - /home/yyt/data/elasticsearch/log:/usr/share/elasticsearch/log
            #- /home/yyt/data/elasticsearch/config/:/usr/share/elasticsearch/config/elastic-stack-ca.p12 
            #- /home/yyt/data/elasticsearch/config/:/usr/share/elasticsearch/config/elastic-certificates.p12


    zaxq-eureka:
        #image: openjdk:17.0.2-slim-buster
        # 使用自己制作的
        #image: jdk17
        #image: registry.cn-shenzhen.aliyuncs.com/xielin/java8:1.0
        image: mdsol/java8-jre
        container_name: zaxq-eureka
        ports:
            - 9527:9527
        volumes:
            - /home/yyt/zaxq/java/log/eureka/:/logs/zaxq/eureka/       
            - /home/yyt/zaxq/java/jar/:/usr/jar/    
        working_dir: /usr/jar/
        #command: bash -c 'java -jar   -Duser.timezone=GMT+8 zaxq-eureka.jar --spring.profiles.active=test'
        #command: bash -c "wait-for.sh 10.60.0.56:5672 -t 0 && java -jar   -Duser.timezone=GMT+8 zaxq-eureka.jar --spring.profiles.active=test"
        # /usr/local/bin/wait-for.sh 可以省略路径(-t 0 不设置超时时间),10.60.0.56:5672不能以rabbitmq容器名来代替ip,只有容器的服务名才行(容器名不行),即:有服务名则一定要使用服务名加端口,否则使用宿主机ip加端口。
        command:
            - sh
            - -c
            - |
                #wait-for.sh 172.31.4.137:5672 -t 0
                #wait-for.sh 172.31.4.137:6379 -t 0
                #wait-for.sh 172.31.4.137:3306 -t 0
                java -jar   -Duser.timezone=GMT+8 zaxq-eureka.jar --spring.profiles.active=test-gc
                
    web-api:
        image: openjdk:8
        container_name: ygt-tourism-web-api
        restart: always
        ports:
            - 10082:10082
        volumes:
            - /home/yyt/ygt_tourism/java/log/web-api/:/home/yyt/ygt_tourism/java/log/web-api/
            - /home/yyt/ygt_tourism/java/resources/public/:/home/yyt/ygt_tourism/java/resources/public/
            - /home/yyt/ygt_tourism/java/tmp/web-api/:/home/yyt/ygt_tourism/java/tmp/web-api/
            - /home/yyt/ygt_tourism/java/jar/:/usr/jar/
        working_dir: /usr/jar/
        command: bash -c 'java -jar -Duser.timezone=GMT+8 web-api.jar --spring.profiles.active=prod-yyt'
        environment:
            - SET_CONTAINER_TIMEZONE=true
            - CONTAINER_TIMEZONE=Asia/Shanghai
        networks:
            - ygt_tourism
            
networks:
    ygt_tourism:         

附件

mysql的config配置

touch config

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid

character-set-server=utf8
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000

[client]
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/
rabbitmq的config配置

touch rabbitmq.conf

loopback_users.guest = false
listeners.tcp.default = 5672
default_pass = zaxq@yyt
default_user = admin
default_vhost = /
相关推荐
打码人的日常分享5 分钟前
商用密码应用安全性评估,密评整体方案,密评管理测评要求和指南,运维文档,软件项目安全设计相关文档合集(Word原件)
运维·安全·web安全·系统安全·规格说明书
追风赶月、1 小时前
【Linux】线程概念与线程控制
linux·运维·服务器
工业3D_大熊1 小时前
3D可视化引擎HOOPS Luminate场景图详解:形状的创建、销毁与管理
java·c++·3d·docker·c#·制造·数据可视化
szc17671 小时前
docker 相关命令
java·docker·jenkins
CP-DD1 小时前
Docker 容器化开发 应用
运维·docker·容器
Stark-C1 小时前
万物皆可Docker,在NAS上一键部署最新苹果MacOS 15系统
macos·docker·策略模式
老司机张师傅2 小时前
【微服务实战之Docker容器】第七章-Dockerfile解析
容器·dockerfile·虚悬镜像·docker学习
微信-since811922 小时前
[ruby on rails] 安装docker
后端·docker·ruby on rails
努力的悟空3 小时前
国土变更调查拓扑错误自动化修复工具的研究
运维·自动化
吴半杯3 小时前
gateway漏洞(CVE-2022-22947)
docker·kubernetes·gateway