Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Docker cache issue #2

Open
wants to merge 2 commits into
base: fix-cache
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docker-compose.local.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

services:
backend:
image: care_local
build:
context: .
dockerfile: docker/dev.Dockerfile
cache_from:
- type=gha
cache_to:
- type=gha,mode=max
env_file:
- ./docker/.local.env
volumes:
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

networks:
default:
name: care
Expand Down
6 changes: 3 additions & 3 deletions docker/dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ RUN ARCH=$(dpkg --print-architecture) && \

# use pipenv to manage virtualenv
RUN python -m venv /.venv
RUN --mount=type=cache,target=/root/.cache/pip pip install pipenv==2024.4.0
RUN --mount=type=cache,target=/root/.cache/pip pip install pipenv==2024.4.0 --cache-from=type=gha --cache-to=type=gha,mode=max

COPY Pipfile Pipfile.lock $APP_HOME/
RUN --mount=type=cache,target=/root/.cache/pip pipenv install --system --categories "packages dev-packages docs"
RUN --mount=type=cache,target=/root/.cache/pip pipenv install --system --categories "packages dev-packages docs" --cache-from=type=gha --cache-to=type=gha,mode=max

COPY . $APP_HOME/

RUN --mount=type=cache,target=/root/.cache/pip python3 $APP_HOME/install_plugins.py
RUN --mount=type=cache,target=/root/.cache/pip python3 $APP_HOME/install_plugins.py --cache-from=type=gha --cache-to=type=gha,mode=max

HEALTHCHECK \
--interval=10s \
Expand Down
91 changes: 18 additions & 73 deletions docker/prod.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,86 +1,31 @@
FROM python:3.13-slim-bookworm AS base

# Set build arguments and environment variables
ARG APP_HOME=/app
ARG TYPST_VERSION=0.11.0

ARG BUILD_ENVIRONMENT="production"
ARG APP_VERSION="unknown"
ARG ADDITIONAL_PLUGS=""

WORKDIR $APP_HOME

ENV BUILD_ENVIRONMENT=$BUILD_ENVIRONMENT
ENV APP_VERSION=$APP_VERSION
ENV ADDITIONAL_PLUGS=$ADDITIONAL_PLUGS
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PIPENV_VENV_IN_PROJECT=1
ENV PIPENV_CACHE_DIR=/root/.cache/pip
ENV PATH=$APP_HOME/.venv/bin:$PATH


# ---
FROM base AS builder
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
APP_VERSION=$APP_VERSION \
ADDITIONAL_PLUGS=$ADDITIONAL_PLUGS

RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential libjpeg-dev zlib1g-dev libgmp-dev libpq-dev git wget \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*
# Install dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*

# Download and install Typst for the correct architecture
RUN ARCH=$(dpkg --print-architecture) && \
if [ "$ARCH" = "amd64" ]; then \
TYPST_ARCH="x86_64-unknown-linux-musl"; \
elif [ "$ARCH" = "arm64" ]; then \
TYPST_ARCH="aarch64-unknown-linux-musl"; \
else \
echo "Unsupported architecture: $ARCH"; \
exit 1; \
fi && \
wget -qO typst.tar.xz https://github.com/typst/typst/releases/download/v${TYPST_VERSION}/typst-${TYPST_ARCH}.tar.xz && \
tar -xf typst.tar.xz && \
mv typst-${TYPST_ARCH}/typst /usr/local/bin/typst && \
rm -rf typst.tar.xz typst-${TYPST_ARCH}

# use pipenv to manage virtualenv
RUN pip install pipenv==2024.4.0

RUN python -m venv $APP_HOME/.venv
# Copy dependency files
COPY Pipfile Pipfile.lock $APP_HOME/
RUN --mount=type=cache,target=/root/.cache/pip pipenv install --deploy --categories "packages"

COPY plugs/ $APP_HOME/plugs/
COPY install_plugins.py plug_config.py $APP_HOME/
RUN --mount=type=cache,target=/root/.cache/pip python3 $APP_HOME/install_plugins.py

# ---
FROM base AS runtime

RUN addgroup --system django \
&& adduser --system --ingroup django django

RUN apt-get update && apt-get install --no-install-recommends -y \
libpq-dev libgmp-dev gettext wget curl gnupg \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*

RUN chown django:django $APP_HOME

COPY --from=builder --chmod=0755 /usr/local/bin/typst /usr/local/bin/typst

COPY --from=builder --chown=django:django $APP_HOME/.venv $APP_HOME/.venv

COPY --chmod=0755 --chown=django:django ./scripts/*.sh $APP_HOME

COPY --chown=django:django . $APP_HOME

USER django
# Install Python dependencies
RUN pip install --no-cache-dir pipenv && pipenv install --deploy --system

HEALTHCHECK \
--interval=30s \
--timeout=5s \
--start-period=10s \
--retries=12 \
CMD ["./healthcheck.sh"]
# Copy application code
COPY . $APP_HOME

EXPOSE 9000
# Expose port and set entrypoint
EXPOSE 8000
CMD ["python", "manage.py", "runserver"]
Loading