Skip to content

Commit

Permalink
增加 dockerFile支持pwa支持 和ngixn java端口分离
Browse files Browse the repository at this point in the history
  • Loading branch information
SQ committed Nov 7, 2023
1 parent 68cf9ff commit f6e0202
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
37 changes: 21 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
#编译jar包
FROM maven:3.9.4-eclipse-temurin-17-alpine AS builder
FROM nginx as nginx
MAINTAINER SQ


MAINTAINER SQ

WORKDIR /build/

# 时区与字符设置UTF-8并配置环境
ENV TZ=Asia/Shanghai
ENV LANG=C.UTF-8
ENV apiPort="8099"
ENV webPort="80"
ENV TimeZone=Asia/Shanghai

#复制源码信息
COPY pom.xml /build/
COPY src /build/src/
COPY src/main/resources/sqlite/sqmusic.db /cache/sqmusic.db


#打包
RUN mvn clean package

From eclipse-temurin:17-jre-alpine

WORKDIR /app
#运行镜像
FROM centos:7

RUN mkdr -p /usr/share/nginx/html/
RUN mkdr -p /etc/nginx/conf.d/
RUN echo "worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen $webPort; server_name localhost; location / { root html; index index.html index.htm; } location /sqmusic-api/ { proxy_pass http://localhost:$apiPort/; proxy_http_version 1.1; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}" > /etc/nginx/conf.d/default.conf

RUN yum update \
&& yum install -y java-17-openjdk nginx

COPY --from=builder /build/target/MusicServer2.0.jar /app/app.jar
COPY --from=nginx src/main/resources/static/ /usr/share/nginx/html/

RUN echo "worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen ${webPort}; server_name localhost; location / { root html; index index.html index.htm; } location /sqmusic-api/ { proxy_pass http://localhost:${apiPort}/; proxy_http_version 1.1; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}" > /usr/local/nginx/conf/nginx.conf

EXPOSE 8099
EXPOSE 80

VOLUME ["/music"]
#将启动脚本拷贝到容器里面的/usr/local/project下面
COPY run.sh /usr/local/project
#给run.sh可执行权限
RUN chmod 777 /usr/local/project/docker-run.sh

VOLUME ["/cache"]
#对外暴露80,8880端口,暴不暴露端口没有什么影响,重要的是要在启动的时候使用-p映射宿主机端口:容器端口,暴露端口你也得使用-p映射端口
EXPOSE ${apiPort} ${webPort}

CMD ["nginx -g daemon off;","java -jar app.jar --server.port=$apiPort"]
#nginx -c -t /software/nginx/nginx.conf

#通过脚本同时启动后端jar包和nginx
CMD ["sh","/usr/local/project/docker-run.sh","${apiPort}","${webPort}"]
25 changes: 25 additions & 0 deletions Dockerfile-java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM maven:3.9.4-eclipse-temurin-17-alpine AS builder
MAINTAINER SQ

WORKDIR /build/

COPY pom.xml /build/
COPY src /build/src/
COPY src/main/resources/sqlite/sqmusic.db /cache/sqmusic.db

RUN mvn clean package

From eclipse-temurin:17-jre-alpine

WORKDIR /app

COPY --from=builder /build/target/MusicServer2.0.jar /app/app.jar

EXPOSE 8099

VOLUME ["/music"]

VOLUME ["/cache"]

CMD ["java", "-jar", "app.jar"]

16 changes: 16 additions & 0 deletions docker-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash


echo "java-端口:$0";
echo "web-端口:$1";

# 启动nginx服务
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

#启动后端jar包,日志打印不额外存储
nohup java -Dserver.port=$0 -jar /app/app.jar >/dev/null 2>& 1 &

#使这个脚本一直处于运行状态,如果不这样,当这个脚本命令执行结束后,docker容器会立即停止运行,所以这里需要让这个脚本一直运行,使docker容器一直处于运行状态
while [[ true ]];do
sleep 1
done

0 comments on commit f6e0202

Please sign in to comment.