首先我们要在docker中安装好环境镜像
jdk. mysql. redis. nginx 镜像安装我们在上一篇文章中已说明,请大家自行查看。
下面我介绍部署步骤
- 部署后台jar
在你的工作目录下新建application 用来存放后台jar包
1.将打好的jar包上传
2.编写Dockerfile文件,上传到jar同目录
#Dockerfile
#指定镜像
FROM openjdk:8
#后续指定工作目录
WORKDIR /app
#添加JAR文件到容器中
COPY ruoyi-admin.jar ./application.jar
#设置环境变量
ENV TZ Asia/Shanghai
ENV JAVA_OPS -Duser.timezone=Asia/Shanghai
#运行端口
EXPOSE 8090
#启动命令
ENTRYPOINT ["java","-Dfile.encoding=utf-8","-D-Xms2g -Xmx2g","-Dserver.port=8090","-Dspring.profiles.active=druid","-Djava.security.egd=file:/dev/./urandom","-jar","./application.jar"]
- 部署Vue前台
在上篇文章中,我们已经将nginx的html目录和config目录挂载到了我们新建的工作目录/usr/local/workspace/nginx下
1.将我们前台打的dist下的文件上传到/usr/local/workspace/nginx/html目录下
2.编辑我们/usr/local/workspace/nginx/conf下的nginx.conf文件,配置内容如下
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html last;
index index.html index.htm;
}
#跨域配置
location /prod-api/{
proxy_pass http://172.31.3.64:8090;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- 编辑docker-compose.yml文件
version: "3.3"
services:
redis:
image: redis
restart: always
ports:
- "6379:6379"
volumes:
- ./redis/redis.conf:/etc/redis/redis.conf
- ./redis/data:/data
command: redis-server /etc/redis/redis.conf
nginx:
# 镜像名 如果需要指定版本 就把 latest 换成版本号
image: nginx:1.25
# 容器名
container_name: nginx-web
# 重启策略
restart: always
# 端口映射
#network_mode: host
ports:
- 80:80
volumes:
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/html:/usr/share/nginx/html
- ./nginx/log:/var/log/nginx # 将Nginx的日志目录挂载到宿主机
privileged: true # 这个必须要,解决nginx的文件调用的权限问题
api:
build: ./application
ports:
- '8090:8090'
container_name: ruoyi-api
restart: always
volumes:
- ./logs:/opt/logs
关于Dockerfile和Docker Compose的详细内容大家可以到菜鸟教程学习
安装部署中出现的错误:
1.502没有权限,并且加载不出验证码
这个是nginx配置代理的问题,大家就仔细检查一下,nginx.conf配置文件就行,我是没有添加图里我框住的内容,加上就行了
2.加上配置后,又报了一个404,还是没有显示验证码
404就是请求路径不对
我这里的原因是我前台vue打的是prod 的包,后台jar包我直接用的若依的配置文件打的包,默认没有项目前缀,所有一直报404,加上前缀就好了
浏览器测试