diff --git a/backend/Dockerfile b/backend/Dockerfile index 2b231d02..bc610a9f 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,4 +1,5 @@ -ARG PYTHON_BASE_IMAGE=python:3.12-slim +# syntax=docker/dockerfile:1.4 +ARG PYTHON_BASE_IMAGE=python:3.12-slim-bookworm FROM ${PYTHON_BASE_IMAGE} ARG APT_MIRROR=https://mirrors.aliyun.com @@ -7,7 +8,9 @@ ARG PIP_EXTRA_INDEX_URL WORKDIR /app -RUN set -eux; \ +RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ + --mount=type=cache,target=/var/lib/apt,sharing=locked \ + set -eux; \ if [ -f /etc/apt/sources.list.d/debian.sources ]; then \ sed -i "s|http://deb.debian.org/debian|${APT_MIRROR}/debian|g" /etc/apt/sources.list.d/debian.sources; \ sed -i "s|http://deb.debian.org/debian-security|${APT_MIRROR}/debian-security|g" /etc/apt/sources.list.d/debian.sources; \ @@ -16,26 +19,30 @@ RUN set -eux; \ sed -i "s|http://deb.debian.org/debian|${APT_MIRROR}/debian|g" /etc/apt/sources.list; \ sed -i "s|http://deb.debian.org/debian-security|${APT_MIRROR}/debian-security|g" /etc/apt/sources.list; \ fi; \ - apt-get update && apt-get install -y \ + apt-get update && apt-get install -y --no-install-recommends \ libgl1 \ libglib2.0-0 \ libxcb1 \ libx11-6 \ libxext6 \ - libreoffice \ + libreoffice-nogui \ fonts-noto-cjk \ curl \ - gnupg2 \ + ca-certificates \ + gnupg \ unixodbc-dev \ - && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \ - && curl https://packages.microsoft.com/config/debian/12/prod.list > /etc/apt/sources.list.d/mssql-release.list \ + && install -d -m 0755 /etc/apt/keyrings \ + && curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /etc/apt/keyrings/microsoft-prod.gpg \ + && chmod a+r /etc/apt/keyrings/microsoft-prod.gpg \ + && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/microsoft-prod.gpg] https://packages.microsoft.com/debian/12/prod bookworm main" > /etc/apt/sources.list.d/mssql-release.list \ && apt-get update \ - && ACCEPT_EULA=Y apt-get install -y msodbcsql18 \ + && ACCEPT_EULA=Y apt-get install -y --no-install-recommends msodbcsql18 \ && rm -rf /var/lib/apt/lists/* # Install Python dependencies COPY backend/requirements.txt . -RUN set -eux; \ +RUN --mount=type=cache,target=/root/.cache/pip \ + set -eux; \ PIP_ARGS="--no-cache-dir --retries 10 --timeout 60"; \ if [ -n "${PIP_INDEX_URL:-}" ]; then \ PIP_ARGS="$PIP_ARGS --index-url ${PIP_INDEX_URL}"; \ @@ -62,4 +69,3 @@ EXPOSE 5000 # Start Gunicorn # Using the config file we created earlier CMD ["gunicorn", "-c", "deploy/gunicorn_config.py", "run:app"] - diff --git a/docker-compose.yml b/docker-compose.yml index 950b975a..a24c7779 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,7 +23,7 @@ services: context: . dockerfile: backend/Dockerfile args: - PYTHON_BASE_IMAGE: ${PYTHON_BASE_IMAGE:-python:3.12-slim} + PYTHON_BASE_IMAGE: ${PYTHON_BASE_IMAGE:-python:3.12-slim-bookworm} PIP_INDEX_URL: ${PIP_INDEX_URL:-https://mirrors.aliyun.com/pypi/simple} PIP_EXTRA_INDEX_URL: ${PIP_EXTRA_INDEX_URL:-} container_name: hxops-backend @@ -65,7 +65,7 @@ services: context: ./backend dockerfile: Dockerfile args: - PYTHON_BASE_IMAGE: ${PYTHON_BASE_IMAGE:-python:3.12-slim} + PYTHON_BASE_IMAGE: ${PYTHON_BASE_IMAGE:-python:3.12-slim-bookworm} PIP_INDEX_URL: ${PIP_INDEX_URL:-https://mirrors.aliyun.com/pypi/simple} PIP_EXTRA_INDEX_URL: ${PIP_EXTRA_INDEX_URL:-} container_name: hxops-kb-worker diff --git a/scripts/deploy_compose.sh b/scripts/deploy_compose.sh index 2f4bffc4..0330d98f 100755 --- a/scripts/deploy_compose.sh +++ b/scripts/deploy_compose.sh @@ -174,6 +174,9 @@ fi if [[ "${PREBUILT_MODE}" == "true" ]]; then "${DC_CMD[@]}" "${compose_args[@]}" up -d --no-build "${services[@]}" else + # Dockerfile 使用 apt/pip cache mount,需启用 BuildKit + export DOCKER_BUILDKIT=1 + export COMPOSE_DOCKER_CLI_BUILD=1 "${DC_CMD[@]}" "${compose_args[@]}" up -d --build "${services[@]}" fi