From 42aaa73b032253056f1b22a8cc4c6046b5ee57b5 Mon Sep 17 00:00:00 2001 From: Michael C Date: Wed, 24 Jul 2024 15:28:54 -0400 Subject: [PATCH] make dockerfile more cacheable in builds add py cache in amd64 --- .github/workflows/docker_build.yml | 14 +++++++ Dockerfile | 59 ++++++++++++++---------------- Dockerfile.armv7 | 56 +++++++++++++--------------- 3 files changed, 66 insertions(+), 63 deletions(-) diff --git a/.github/workflows/docker_build.yml b/.github/workflows/docker_build.yml index e73757ab..64af00ad 100644 --- a/.github/workflows/docker_build.yml +++ b/.github/workflows/docker_build.yml @@ -44,6 +44,20 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + - name: python3 build cache for docker + uses: actions/cache@v4 + with: + path: python-build-cache + key: ${{ runner.os }}-python-build-cache-${{ hashFiles('Pipfile.lock') }} + + - name: inject python-build-cache into docker + uses: reproducible-containers/buildkit-cache-dance@v3 # v2.1.4 + with: + cache-map: | + { + "python-build-cache" : "/root/.cache" + } + # Build container - name: Build Container uses: docker/build-push-action@v5 diff --git a/Dockerfile b/Dockerfile index 73f9fd50..4b97e8fd 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1,55 +1,50 @@ # syntax=docker/dockerfile:1 -# Create pipenv image to convert Pipfile to requirements.txt -FROM python:3.11-slim AS pipenv - -# Copy Pipfile and Pipfile.lock -COPY Pipfile Pipfile.lock ./ - -# Install pipenv and convert to requirements.txt -RUN pip3 install --no-cache-dir --upgrade pipenv; \ - pipenv requirements > requirements.txt - FROM python:3.11-slim AS python-reqs +# run static install +# Install gcc for building python dependencies +RUN apt-get update && \ + apt-get install -y gcc +RUN pip3 install --no-cache-dir --upgrade pipenv -# Copy requirements.txt from pipenv stage -COPY --from=pipenv /requirements.txt requirements.txt +# Install pipenv and convert to requirements.txt +COPY Pipfile Pipfile.lock ./ +RUN pipenv requirements > requirements.txt -# Install gcc for building python dependencies; install TCM dependencies -RUN apt-get update && \ - apt-get install -y gcc && \ - pip3 install --no-cache-dir -r requirements.txt +# install TCM dependencies +RUN --mount=type=cache,target=/root/.cache \ + pip3 install -r requirements.txt # Set base image for running TCM -FROM python:3.11-slim +FROM python:3.11-slim AS final LABEL maintainer="CollinHeist" \ description="Automated Title card maker for Emby, Jellyfin, and Plex" \ version="v2.0-alpha.10.0" - -# Set working directory, copy source into container -WORKDIR /maker -COPY . /maker - -# Copy python packages from python-reqs -COPY --from=python-reqs /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages - # Script environment variables ENV TCM_IS_DOCKER=TRUE \ TZ=UTC -# Finalize setup +# Install static dependencies RUN \ # Create user and group to run TCM set -eux && \ - rm -f Pipfile Pipfile.lock && \ groupadd -g 314 titlecardmaker && \ - useradd -u 314 -g 314 titlecardmaker && \ - # Install imagemagick and curl (for healthcheck) - apt-get update && \ + useradd -u 314 -g 314 titlecardmaker +# Install imagemagick and curl (for healthcheck) +RUN apt-get update && \ apt-get install -y --no-install-recommends curl imagemagick libmagickcore-6.q16-6-extra && \ - cp modules/ref/policy.xml /etc/ImageMagick-6/policy.xml && \ # Remove apt cache and setup files rm -rf /tmp/* /var/tmp/* /var/lib/apt/lists/* +# Copy python packages from python-reqs +COPY --from=python-reqs /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages + +# Set working directory, copy source into container +WORKDIR /maker +COPY . /maker + +# Finalize setup +RUN cp modules/ref/policy.xml /etc/ImageMagick-6/policy.xml && \ + rm -f Pipfile Pipfile.lock # Expose TCM Port EXPOSE 4242 @@ -60,4 +55,4 @@ HEALTHCHECK --interval=3m --timeout=10s --start-period=3m \ # Entrypoint CMD ["python3", "-u", "-m", "uvicorn", "app-main:app", "--host", "0.0.0.0", "--port", "4242"] -ENTRYPOINT ["bash", "./start.sh"] +ENTRYPOINT ["bash", "./start.sh"] \ No newline at end of file diff --git a/Dockerfile.armv7 b/Dockerfile.armv7 index 08b8f302..83149772 100644 --- a/Dockerfile.armv7 +++ b/Dockerfile.armv7 @@ -1,19 +1,6 @@ # syntax=docker/dockerfile:1 -# Create pipenv image to convert Pipfile to requirements.txt -FROM python:3.11-slim as pipenv - -# Copy Pipfile and Pipfile.lock -COPY Pipfile Pipfile.lock ./ - -# Install pipenv and convert to requirements.txt -RUN pip3 install --no-cache-dir --upgrade pipenv; \ - pipenv requirements > requirements.txt - FROM python:3.11-slim as python-reqs -# Copy requirements.txt from pipenv stage -COPY --from=pipenv /requirements.txt requirements.txt - # Install dependencies for building python dependencies; install TCM dependencies # cffi: libffi-dev # bcrypt: build-essential, cargo @@ -27,44 +14,51 @@ RUN apt-get update && \ apt-get install -y \ build-essential curl libffi-dev libssl-dev pkg-config python3-dev python3-setuptools \ libtiff5-dev libjpeg62-turbo-dev libopenjp2-7-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python3-tk libharfbuzz-dev libfribidi-dev libxcb1-dev +RUN curl https://sh.rustup.rs | sh -s -- -y - RUN curl https://sh.rustup.rs | sh -s -- -y -# add manual dockerfile cachine +# Copy Pipfile and Pipfile.lock +COPY Pipfile Pipfile.lock ./ +# Install pipenv and convert to requirements.txt +RUN pip3 install --no-cache-dir --upgrade pipenv; \ + pipenv requirements > requirements.txt + +# add manual dockerfile caching RUN --mount=type=cache,target=/root/.cache \ . "$HOME/.cargo/env" && \ pip3 install -r requirements.txt # Set base image for running TCM -FROM python:3.11-slim +FROM python:3.11-slim AS final LABEL maintainer="CollinHeist" \ description="Automated Title card maker for Emby, Jellyfin, and Plex" \ version="v2.0-alpha.10.0" - -# Set working directory, copy source into container -WORKDIR /maker -COPY . /maker - -# Copy python packages from python-reqs -COPY --from=python-reqs /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages - # Script environment variables ENV TCM_IS_DOCKER=TRUE \ TZ=UTC -# Finalize setup +# Install static dependencies RUN \ # Create user and group to run TCM set -eux && \ - rm -f Pipfile Pipfile.lock && \ groupadd -g 314 titlecardmaker && \ - useradd -u 314 -g 314 titlecardmaker && \ - # Install imagemagick and curl (for healthcheck) - apt-get update && \ + useradd -u 314 -g 314 titlecardmaker +# Install imagemagick and curl (for healthcheck) +RUN apt-get update && \ apt-get install -y --no-install-recommends curl imagemagick libmagickcore-6.q16-6-extra && \ - cp modules/ref/policy.xml /etc/ImageMagick-6/policy.xml && \ # Remove apt cache and setup files rm -rf /tmp/* /var/tmp/* /var/lib/apt/lists/* +# Copy python packages from python-reqs +COPY --from=python-reqs /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages + +# Set working directory, copy source into container +WORKDIR /maker +COPY . /maker + +# Finalize setup +RUN cp modules/ref/policy.xml /etc/ImageMagick-6/policy.xml && \ + rm -f Pipfile Pipfile.lock && \ + # Expose TCM Port EXPOSE 4242 @@ -75,4 +69,4 @@ HEALTHCHECK --interval=3m --timeout=10s --start-period=3m \ # Entrypoint CMD ["python3", "-u", "-m", "uvicorn", "app-main:app", "--host", "0.0.0.0", "--port", "4242"] -ENTRYPOINT ["bash", "./start.sh"] +ENTRYPOINT ["bash", "./start.sh"] \ No newline at end of file