本文使用docker-compose方式部署mysql、redis、minio、后端项目、前端项目。
整体目录:
说明:
- data:存放启动命令
- html:存放前端html、js代码
- jar:存放jar包
- minio:存放minio配置
- mysql:存放mysql配置
- nginx:存放nginx配置
- redis:存放redis配置
- daemon.json:存放国内镜像源
- 如果是在windows上部署docker服务,则需要安装git,使用git中的sh命令
data
bash
#docker-compose up -d
docker-compose -f D:\\gxc\\data\\docker-compose.yml up -d # docker-compose路径
echo "执行成功 $?"
bash
#!/bin/bash
# 确保Docker命令执行成功
# 删除之前的镜像,重新构建镜像
docker rm -f gxc_tenant
docker rmi gxc_tenant:1.0
docker build -t gxc_tenant:1.0 D:\\gxc\\jar
# 删除之前的镜像,重新构建镜像
docker rm -f vue_tenant
docker rmi vue_tenant:1.0
docker build -t vue_tenant:1.0 D:\\gxc\\html\\
sh compose-up.sh
echo "执行成功 $?"
docker-compose.yml
bash
version: '1.0'
services:
mysql:
restart: always
privileged: true
image: mysql:8.0
container_name: mysql
volumes:
- D:\\gxc\\mysql\\conf:/etc/mysql/conf.d
- D:\\gxc\\mysql\\log:/var/logs
- D:\\gxc\\mysql\\data:/var/lib/mysql
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
environment:
MYSQL_DATABASE: 'gxc_tenant' # 项目数据库名
MYSQL_ROOT_PASSWORD: '123456'
TZ: Asia/Shanghai
MYSQL_LC_CTYPE_MAPPINGS: on # 设置不区分大小写
ports:
- 13306:3306
- 13307:33060
networks:
data_security:
ipv4_address: 192.128.0.10
redis:
image: redis:latest
restart: always
container_name: redis
environment:
- TZ=Asia/Shanghai # 替换为你的本地时区
ports:
- 16379:6379
volumes: # 数据卷,目录挂载
- D:\\gxc\\redis\\conf\\redis.conf:/usr/local/etc/redis/redis.conf
- D:\\gxc\\redis\\data:/data
networks:
data_security:
ipv4_address: 192.128.0.20
command: redis-server /usr/local/etc/redis/redis.conf # 覆盖容器启动后默认执行的命令
minio:
image: "minio/minio"
container_name: minio
restart: always
ports:
- "19000:9000"
- "19001:9001"
volumes:
- D:\\gxc\\minio\\data:/data
- D:\\gxc\\minio\\config:/root/.minio
command: server /data --console-address ":9001"
environment:
- TZ=Europe/Berlin # 替换为你的本地时区
- MINIO_ROOT_USER=minioadmin # 登录web console的用户名
- MINIO_ROOT_PASSWORD=minioadmin # 登录web console的密码
networks:
data_security:
ipv4_address: 192.128.0.30
gxc_tenant:
image: gxc_tenant:1.0
container_name: gxc_tenant
ports:
- 8086:8086
restart: always
environment:
- TZ=Asia/Shanghai # 替换为你的本地时区
volumes:
- D:\\gxc\\jar\\:/eitc_tenant
# - /usr/share/fonts/Fonts:/usr/share/fonts # 读取本地字体
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
depends_on:
- mysql
- redis
- minio
networks:
data_security:
ipv4_address: 192.128.0.40
vue_tenant:
image: vue_tenant:1.0
container_name: vue_tenant
ports:
- 9086:9086
environment:
- TZ=Asia/Shanghai # 替换为你的本地时区
volumes:
- D:\\gxc\\nginx\\conf\\nginx.conf:/etc/nginx/nginx.conf
- D:\\gxc\\nginx\\conf\\conf.d:/etc/nginx/conf.d
- D:\\gxc\\nginx\\log:/var/log/nginx
# 因为项目代码要跟着镜像走,所以前端代码不做挂载
# - /home/gxc_tenant/html/dist:/usr/share/nginx/html
restart: always
depends_on:
- gxc_tenant
networks:
data_security:
ipv4_address: 192.128.0.50
privileged: true # 这个必须要,解决nginx的文件调用的权限问题
networks:
data_security: # 自定义网络名字
driver: bridge
ipam:
config:
- subnet: 192.128.0.0/24 #自定义固定容器ip 实现容器间通讯,增强docker-compose项目可移植性
html
dist 存放前端打的包
Dockerfile
bash
# 基于Node镜像来构建我们的前端环境
FROM nginx:latest
# 暴露80端口(Nginx默认端口)
EXPOSE 9086
# 从Node镜像中复制构建的文件到Nginx的服务目录
COPY ./dist /usr/share/nginx/html
# 启动Nginx服务器
CMD ["nginx", "-g", "daemon off;"]
vue-build.sh前端启用脚本
bash
#!/bin/bash
# 确保Docker命令执行成功
# 删除之前的镜像,重新构建镜像
docker rm -f vue_tenant
docker rmi vue_tenant:1.0
docker build -t vue_tenant:1.0 D:\\gxc\\html\\;
echo "执行成功 $?"
jar
Dockerfile
bash
# 使用OpenJDK作为基础镜像
FROM openjdk:8-jre-alpine
# 设置工作目录
WORKDIR /data_tenant
# 将本地的JAR包复制到容器的/app目录下
COPY gxc-tenant.jar /data_tenant/
# 设置环境变量,指定JAR包名
ENV JAR_FILE=gxc-tenant.jar
# RUN apk add --update ttf-dejavu fontconfig
# 暴露应用的端口(根据你的应用需要修改)
EXPOSE 8086
# 启动命令
# CMD ["java", "-jar", "$JAR_FILE"]
ENTRYPOINT java -jar /data_tenant/gxc-tenant.jar
创建桥接网络:
bash
docker network create --driver bridge --subnet 192.168.0.0/24 data_security
解决端口总是占用问题:本地没有占用端口,还是展示
bash
docker network prune