From f51f4ce7fca8b4e5f520855b9ce1519181ce927e Mon Sep 17 00:00:00 2001 From: xuwei-fit2cloud Date: Thu, 29 May 2025 17:04:04 +0800 Subject: [PATCH] refactor: optimize Dockerfile --- .dockerignore | 12 +++++++++ .gitignore | 2 +- Dockerfile | 62 +++++++++++++++++++++++++++++++++++++++++++ backend/.dockerignore | 8 ------ backend/Dockerfile | 52 ------------------------------------ 5 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile delete mode 100644 backend/.dockerignore delete mode 100644 backend/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..6b78531c --- /dev/null +++ b/.dockerignore @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3aaaa54c..576b2752 100644 --- a/.gitignore +++ b/.gitignore @@ -186,4 +186,4 @@ cython_debug/ .DS_Store test.py -components.d.ts + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5ccc1471 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/backend/.dockerignore b/backend/.dockerignore deleted file mode 100644 index c0de4abf..00000000 --- a/backend/.dockerignore +++ /dev/null @@ -1,8 +0,0 @@ -# Python -__pycache__ -app.egg-info -*.pyc -.mypy_cache -.coverage -htmlcov -.venv diff --git a/backend/Dockerfile b/backend/Dockerfile deleted file mode 100644 index d1fe0fdc..00000000 --- a/backend/Dockerfile +++ /dev/null @@ -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"]