xianyu-backend-java/Dockerfile
2026-01-11 11:42:16 +08:00

154 lines
4.1 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 使用Python 3.11作为基础镜像
FROM python:3.11-slim-bookworm AS base
# 设置环境变量
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
TZ=Asia/Shanghai \
DOCKER_ENV=true \
PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
# 设置工作目录
WORKDIR /app
# ==================== Frontend Builder Stage ====================
FROM node:20-alpine AS frontend-builder
WORKDIR /frontend
# 复制前端依赖文件
COPY frontend/package.json frontend/pnpm-lock.yaml ./
# 安装 pnpm 并安装依赖
RUN npm install -g pnpm && pnpm install --frozen-lockfile
# 复制前端源码并构建
COPY frontend/ ./
RUN pnpm build
# ==================== Python Builder Stage ====================
FROM base AS builder
# 安装基础依赖
RUN apt-get update && \
apt-get install -y --no-install-recommends \
curl \
ca-certificates \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN python -m venv /opt/venv && \
/opt/venv/bin/pip install --no-cache-dir --upgrade pip
ENV VIRTUAL_ENV=/opt/venv
ENV PATH="$VIRTUAL_ENV/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin"
# 复制requirements.txt并安装Python依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# 复制项目文件(排除 frontend 目录)
COPY . .
# 复制前端构建产物到 static 目录
COPY --from=frontend-builder /frontend/dist ./static
# 项目已完全开源,无需编译二进制模块
# Runtime stage: only keep what is needed to run the app
FROM base AS runtime
# 设置标签信息
LABEL maintainer="zhinianboke" \
version="2.2.0" \
description="闲鱼自动回复系统 - 企业级多用户版本,支持自动发货和免拼发货" \
repository="https://github.com/zhinianboke/xianyu-auto-reply" \
license="仅供学习使用,禁止商业用途" \
author="zhinianboke" \
build-date="" \
vcs-ref=""
ENV NODE_PATH=/usr/lib/node_modules
RUN apt-get update && \
apt-get install -y --no-install-recommends \
nodejs \
npm \
tzdata \
curl \
ca-certificates \
# 图像处理依赖
libjpeg-dev \
libpng-dev \
libfreetype6-dev \
fonts-dejavu-core \
fonts-liberation \
# Playwright浏览器依赖
libnss3 \
libnspr4 \
libatk-bridge2.0-0 \
libdrm2 \
libxkbcommon0 \
libxcomposite1 \
libxdamage1 \
libxrandr2 \
libgbm1 \
libxss1 \
libasound2 \
libatspi2.0-0 \
libgtk-3-0 \
libgdk-pixbuf2.0-0 \
libxcursor1 \
libxi6 \
libxrender1 \
libxext6 \
libx11-6 \
libxft2 \
libxinerama1 \
libxtst6 \
libappindicator3-1 \
libx11-xcb1 \
libxfixes3 \
xdg-utils \
chromium \
xvfb \
x11vnc \
fluxbox \
# OpenCV运行时依赖
libgl1 \
libglib2.0-0 \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# 设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 验证Node.js安装并设置环境变量
RUN node --version && npm --version
COPY --from=builder /opt/venv /opt/venv
COPY --from=builder /app /app
ENV VIRTUAL_ENV=/opt/venv
ENV PATH="$VIRTUAL_ENV/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin"
RUN playwright install chromium && \
playwright install-deps chromium
# 创建必要的目录并设置权限
RUN mkdir -p /app/logs /app/data /app/backups /app/static/uploads/images && \
chmod 777 /app/logs /app/data /app/backups /app/static/uploads /app/static/uploads/images
# 配置系统限制防止core文件生成
RUN echo "ulimit -c 0" >> /etc/profile
# 注意: 为了简化权限问题使用root用户运行
# 在生产环境中,建议配置适当的用户映射
# 暴露端口
EXPOSE 8080
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
RUN chmod +x /app/entrypoint.sh
# 启动命令
CMD ["/app/entrypoint.sh"]