镜像制作

This commit is contained in:
2025-11-24 11:50:15 +08:00
parent 12592c5a24
commit 07bd166257
53 changed files with 3822 additions and 2140 deletions

91
docker/Dockerfile.web Normal file
View File

@@ -0,0 +1,91 @@
# 前端服务运行镜像
# 注意dist目录需要在主机中先构建好
FROM nginx:alpine
# 设置环境变量
ENV TZ=Asia/Shanghai
# 安装必要的工具
RUN apk add --no-cache tzdata
# 创建配置目录
RUN mkdir -p /app/config /app/logs /usr/share/nginx/html/schoolNewsWeb
# 从主机复制已构建的dist目录
COPY schoolNewsWeb/dist/ /usr/share/nginx/html/schoolNewsWeb/
# 复制配置文件模板(可整个替换)
COPY schoolNewsWeb/public/app-config.js /app/config/app-config.js.template
# 确保dist中有默认配置文件如果build时没有复制
RUN if [ ! -f /usr/share/nginx/html/schoolNewsWeb/app-config.js ]; then \
cp /app/config/app-config.js.template /usr/share/nginx/html/schoolNewsWeb/app-config.js; \
fi
# 创建Nginx配置
RUN echo 'server {\n\
listen 80;\n\
server_name localhost;\n\
\n\
# 日志配置\n\
access_log /app/logs/nginx-access.log;\n\
error_log /app/logs/nginx-error.log;\n\
\n\
# 根路径重定向\n\
location = / {\n\
return 301 /schoolNewsWeb/;\n\
}\n\
\n\
# 前端应用\n\
location /schoolNewsWeb/ {\n\
alias /usr/share/nginx/html/schoolNewsWeb/;\n\
try_files $uri $uri/ /schoolNewsWeb/index.html;\n\
\n\
# 静态资源缓存\n\
location ~* \\.(js|css|png|jpg|jpeg|gif|ico|svg)$ {\n\
expires 1y;\n\
add_header Cache-Control "public, immutable";\n\
}\n\
}\n\
\n\
# 后端API代理\n\
location /schoolNewsServ/ {\n\
proxy_pass http://school-news-serv:8081;\n\
proxy_set_header Host $host;\n\
proxy_set_header X-Real-IP $remote_addr;\n\
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\
proxy_set_header X-Forwarded-Proto $scheme;\n\
\n\
# WebSocket支持\n\
proxy_http_version 1.1;\n\
proxy_set_header Upgrade $http_upgrade;\n\
proxy_set_header Connection "upgrade";\n\
\n\
# 超时设置\n\
proxy_connect_timeout 300s;\n\
proxy_send_timeout 300s;\n\
proxy_read_timeout 300s;\n\
}\n\
\n\
# 错误页面\n\
error_page 404 /schoolNewsWeb/index.html;\n\
error_page 500 502 503 504 /50x.html;\n\
location = /50x.html {\n\
root /usr/share/nginx/html;\n\
}\n\
}\n\
' > /etc/nginx/conf.d/default.conf
# 复制启动脚本
COPY schoolNewsWeb/docker/start.sh /app/start.sh
RUN chmod +x /app/start.sh
# 暴露端口
EXPOSE 80
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
CMD wget --quiet --tries=1 --spider http://localhost/schoolNewsWeb/ || exit 1
# 启动应用
CMD ["/app/start.sh"]