容器化部署ruoyi项目
bash
#需要的镜像nginx、java、mysql、redis、
#导入maven镜像、Java镜像和node镜像
docker load -i java-8u111-jdk.tar
docker load -i maven-3.8.8-sapmachine-11.tar
docker load -i node-18.20.3-alpine3.20.tar
#拉取MySQL和nginx镜像
docker pull mysql:5.7
docker pull nginx:1.16.1
#准备数据库
docker run -itd --name ruoyi-mysql -v ./sql:/docker-entrypoint-initdb.d -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=ry-vue mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
#准备redis
docker run -itd --name ruoyi-redis
#获取ruoyi的源码包
git clone https://gitee.com/y_project/RuoYi-Vue.git && cd RuoYi-Vue
#修改后端的代码
vim /opt/RuoYi-Vue/ruoyi-admin/src/main/resources/application.yml
bash
vim /opt/RuoYi-Vue/ruoyi-admin/src/main/resources/application-druid.yml
bash
#打包后端代码
docker run -it --rm -v .:/opt/app -v /root/.m2:/root/.m2 -w /opt/app maven:3.8.8-sapmachine-11 mvn package
#打包前端代码
cd 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"
#打包好的的代码
java : ruoyi-admin/target
前端 : ruoyi-ui/dist
#整理打包好的代码
#后端
mkdir -p /opt/ruoyi-xa2401/{ruoyi-java,ruoyi-nginx}
cp ruoyi-admin/target/java包 /opt/ruoyi-xa2401/ruoyi-java
vim /opt/ruoyi-xa2401/ruoyi-java/Dockerfile
FROM java:8u111-jdk
COPY ./ruoyi-admin.jar /opt
CMD ["java","-jar","/opt/ruoyi-admin.jar"]
#前端
cp -r ruoyi-ui/dist /opt/ruoyi-xa2401/ruoyi-nginx
cp -r nginx.conf /opt/ruoyi-xa2401/ruoyi-nginx # nginx配置文件在最后面
vim opt/ruoyi-xa2401/ruoyi-nginx/Dockerfile
FROM nginx:1.16.1
COPY dist /usr/share/nginx/dist
COPY nginx.conf /etc/nginx/nginx.conf
CMD ["nginx","-g","daemon off;"]
#构建镜像并运行
docker build -t ruoyi-java:1.0 .
docker build -t ruoyi-nginx:1.0 .
docker run -it --name ruoyi-java --rm --link=ruoyi-mysql:mysql.ruoyi --link=ruoyi-redis:redis.ruoyi ruoyi-java:1.0
docker run -it --name ruoyi-nginx --link=ruoyi-java:java.host -p 80:80 ruoyi-nginx:1.0
nginx的配置文件
bash
# 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;
}
}
}
测试
用docker compose编排
bash
services:
nginx:
build:
context: ./nginx
ports:
- "80:80"
links:
- "java:java.host"
depends_on:
- java
java:
build:
context: ./java
links:
- "redis:redis.ruoyi"
- "database:mysql.ruoyi"
depends_on:
- redis
- database
redis:
image: redis
database:
image: mysql:5.7
container_name: mysql
volumes:
- ./sql:/docker-entrypoint-initdb.d
- ./databases:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=ry-vue
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
=123456
- MYSQL_DATABASE=ry-vue
command:
-
--character-set-server=utf8mb4
-
--collation-server=utf8mb4_unicode_ci