1. 项目环境
centos7 + docker + mysql + redis + ruoyi项目
ruoyi项目链接:https://gitee.com/y_project/RuoYi-Vue.git
2. 进行项目前后端代码打包
后端打包:
修改mysql连接的相关配置文件 RuoYi-Vue/ruoyi-admin/src/main/resources/application-druid.yml
修改redis连接的相关配置文件 RuoYi-Vue/ruoyi-admin/src/main/resources/application.yml
使用maven打包(这里使用容器目录挂载方式进行打包)
cd RuoYi-Vue
docker run -it --rm -v .:/opt/app -v /root/.m2:/root/.m2 -w /opt/app maven:3.8.8-sapmachine-11 mvn package
前端打包:
使用node打包(这里使用容器目录挂载方式进行打包)
cd ruoyi-uicd ruoyi-ui
docker run -it --rm -v .:/opt/app -w /opt/app node:18.20.3-alpine3.20 /bin/sh -c "npm install --registry http://registry.npmmirror.com && npm run build:prod"
3. docker compose来构建服务
目录层级如下
将打包好的jar包放在java目录下用于构建镜像
将打包好的dist前端代码放到nginx中来构建镜像
sql目录用于存放用于导入的sql数据
database进行mysql数据目录的映射
./java/
dockerfile
FROM java:8u111-jdk
COPY . /opt/app
WORKDIR /opt/app
COPY start.sh /
CMD sh /start.sh
java.sh
sleep 10java -jar ruoyi-admin.jar
等待10秒是因为Java构建镜像需要关联mysql,需等待mysql镜像构建完成
./nginx/
nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;charset utf-8;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 4096;include /etc/nginx/mime.types;default_type application/octet-stream;server {listen 80;server_name localhost;#charset koi8-r;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/dist;try_files $uri $uri/ /index.html;index index.html index.htm;}location /prod-api/{proxy_pass http://java.host:8080/; #注意这里的写法proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}}
dockerfile
FROM nginx:1.16.1COPY ./nginx.conf /etc/nginx/nginx.confCOPY ./dist /usr/share/nginx/distEXPOSE 80
docker-compose.yml文件
# 指定服务
services:nginx:# nginx镜像需要构建,将代码和配置文件拷贝进去build:context: ./nginxports:- "80:80"links:- java:java.hostdepends_on:- javajava:build:context: ./javalinks:- redis:redis.host- database:mysql.hostdepends_on:- database- redisredis:image: redisdatabase:image: mysql:5.7container_name: mysqlvolumes:# mysql官方镜像的功能,将sql文件挂载到这个目录下,会自动进行导入操作。- ./sql:/docker-entrypoint-initdb.d- ./database:/var/lib/mysqlenvironment:- MYSQL_ROOT_PASSWORD=123456- MYSQL_DATABASE=ry-vuecommand:- --character-set-server=utf8mb4- --collation-server=utf8mb4_unicode_ci
4. 启动docker compose
docker compose up
构建完成后出现下面现象,ruoyi项目就启动成功了,访问本机ip的80端口就行