refactor: optimize Dockerfile

This commit is contained in:
xuwei-fit2cloud
2025-05-29 17:04:04 +08:00
parent 900caf29d0
commit f51f4ce7fc
5 changed files with 75 additions and 61 deletions

12
.dockerignore Normal file
View File

@@ -0,0 +1,12 @@
# Python
backend/__pycache__
backend/app.egg-info
backend/*.pyc
backend/.mypy_cache
backend/.coverage
backend/htmlcov
backend/.venv
backend/.gitignore
backend/README.md
backend/scripts
backend/.DS_Store

2
.gitignore vendored
View File

@@ -186,4 +186,4 @@ cython_debug/
.DS_Store
test.py
components.d.ts

62
Dockerfile Normal file
View File

@@ -0,0 +1,62 @@
# Build stage
FROM python:3.11-slim-bookworm AS builder
# Set build environment variables
ENV PYTHONUNBUFFERED=1
ENV SQLBOT_HOME=/opt/sqlbot
ENV APP_HOME=${SQLBOT_HOME}/app
ENV UI_HOME=${SQLBOT_HOME}/frontend
ENV PYTHONPATH=${SQLBOT_HOME}/app
ENV PATH="${APP_HOME}/.venv/bin:$PATH"
ENV UV_COMPILE_BYTECODE=1
ENV UV_LINK_MODE=copy
ENV DEBIAN_FRONTEND=noninteractive
# Create necessary directories
RUN mkdir -p ${APP_HOME} ${UI_HOME}
WORKDIR ${APP_HOME}
# Install uv tool
COPY --from=ghcr.io/astral-sh/uv:0.7.8 /uv /uvx /bin/
# COPY ../frontend/dist /opt/sqlbot/frontend/dist
COPY frontend/dist ${UI_HOME}/dist
# Install dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=backend/uv.lock,target=uv.lock \
--mount=type=bind,source=backend/pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project
COPY ./backend ${APP_HOME}
# Final sync to ensure all dependencies are installed
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync
# Runtime stage
FROM python:3.11-slim-bookworm
# Set runtime environment variables
ENV PYTHONUNBUFFERED=1
ENV SQLBOT_HOME=/opt/sqlbot
ENV PYTHONPATH=${SQLBOT_HOME}/app
ENV PATH="${SQLBOT_HOME}/app/.venv/bin:$PATH"
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Copy necessary files from builder
COPY --from=builder ${SQLBOT_HOME} ${SQLBOT_HOME}
WORKDIR ${SQLBOT_HOME}/app
# Add health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000 || exit 1
# Run with uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4", "--proxy-headers"]

View File

@@ -1,8 +0,0 @@
# Python
__pycache__
app.egg-info
*.pyc
.mypy_cache
.coverage
htmlcov
.venv

View File

@@ -1,52 +0,0 @@
FROM python:3.11-slim-bookworm
ENV PYTHONUNBUFFERED=1
ENV SQLBOT_HOME=/opt/sqlbot
ENV APP_HOME=$SQLBOT_HOME/app
ENV UI_HOME=$SQLBOT_HOME/frontend
RUN mkdir -p $APP_HOME
RUN mkdir -p $UI_HOME
WORKDIR $APP_HOME
ENV PYTHONPATH=$APP_HOME
# COPY ../frontend/dist $UI_HOME/dist
COPY ./dist $UI_HOME/dist
# Install uv
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
COPY --from=ghcr.io/astral-sh/uv:0.5.11 /uv /uvx /bin/
# Place executables in the environment at the front of the path
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment
ENV PATH="$APP_HOME/.venv/bin:$PATH"
# Compile bytecode
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode
ENV UV_COMPILE_BYTECODE=1
# uv Cache
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#caching
ENV UV_LINK_MODE=copy
# Install dependencies
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers
RUN test -f "./uv.lock" && \
--mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project || echo "uv.lock file not found, skipping intermediate-layers"
COPY . $APP_HOME
# Sync the project
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync
CMD ["fastapi", "run", "--workers", "4", "main.py"]