Skip to content

Commit c000188

Browse files
authored
Merge pull request #1 from a1cd/slimify-patch-1
Slimify patch 1
2 parents 1db8dec + db4adc0 commit c000188

File tree

2 files changed

+166
-1
lines changed

2 files changed

+166
-1
lines changed

.github/workflows/docker-build.yaml

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,108 @@ jobs:
419419
if-no-files-found: error
420420
retention-days: 1
421421

422+
build-slim-image:
423+
runs-on: ${{ matrix.runner }}
424+
permissions:
425+
contents: read
426+
packages: write
427+
strategy:
428+
fail-fast: false
429+
matrix:
430+
include:
431+
- platform: linux/amd64
432+
runner: ubuntu-latest
433+
- platform: linux/arm64
434+
runner: ubuntu-24.04-arm
435+
436+
steps:
437+
# GitHub Packages requires the entire repository name to be in lowercase
438+
# although the repository owner has a lowercase username, this prevents some people from running actions after forking
439+
- name: Set repository and image name to lowercase
440+
run: |
441+
echo "IMAGE_NAME=${IMAGE_NAME,,}" >>${GITHUB_ENV}
442+
echo "FULL_IMAGE_NAME=ghcr.io/${IMAGE_NAME,,}" >>${GITHUB_ENV}
443+
env:
444+
IMAGE_NAME: '${{ github.repository }}'
445+
446+
- name: Prepare
447+
run: |
448+
platform=${{ matrix.platform }}
449+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
450+
451+
- name: Checkout repository
452+
uses: actions/checkout@v4
453+
454+
- name: Set up QEMU
455+
uses: docker/setup-qemu-action@v3
456+
457+
- name: Set up Docker Buildx
458+
uses: docker/setup-buildx-action@v3
459+
460+
- name: Log in to the Container registry
461+
uses: docker/login-action@v3
462+
with:
463+
registry: ${{ env.REGISTRY }}
464+
username: ${{ github.actor }}
465+
password: ${{ secrets.GITHUB_TOKEN }}
466+
467+
- name: Extract metadata for Docker images (slim tag)
468+
id: meta
469+
uses: docker/metadata-action@v5
470+
with:
471+
images: ${{ env.FULL_IMAGE_NAME }}
472+
tags: |
473+
type=ref,event=branch
474+
type=ref,event=tag
475+
type=sha,prefix=git-
476+
type=semver,pattern={{version}}
477+
type=semver,pattern={{major}}.{{minor}}
478+
type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=slim
479+
flavor: |
480+
latest=${{ github.ref == 'refs/heads/main' }}
481+
suffix=-slim,onlatest=true
482+
483+
- name: Extract metadata for Docker cache
484+
id: cache-meta
485+
uses: docker/metadata-action@v5
486+
with:
487+
images: ${{ env.FULL_IMAGE_NAME }}
488+
tags: |
489+
type=ref,event=branch
490+
${{ github.ref_type == 'tag' && 'type=raw,value=main' || '' }}
491+
flavor: |
492+
prefix=cache-slim-${{ matrix.platform }}-
493+
latest=false
494+
495+
- name: Build Docker image (slim)
496+
uses: docker/build-push-action@v5
497+
id: build
498+
with:
499+
context: .
500+
push: true
501+
platforms: ${{ matrix.platform }}
502+
labels: ${{ steps.meta.outputs.labels }}
503+
outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
504+
cache-from: type=registry,ref=${{ steps.cache-meta.outputs.tags }}
505+
cache-to: type=registry,ref=${{ steps.cache-meta.outputs.tags }},mode=max
506+
build-args: |
507+
BUILD_HASH=${{ github.sha }}
508+
USE_SLIM=true
509+
510+
- name: Export digest
511+
run: |
512+
mkdir -p /tmp/digests
513+
digest="${{ steps.build.outputs.digest }}"
514+
touch "/tmp/digests/${digest#sha256:}"
515+
516+
- name: Upload digest
517+
uses: actions/upload-artifact@v4
518+
with:
519+
name: digests-slim-${{ env.PLATFORM_PAIR }}
520+
path: /tmp/digests/*
521+
if-no-files-found: error
522+
retention-days: 1
523+
422524
merge-main-images:
423525
runs-on: ubuntu-latest
424526
needs: [build-main-image]
@@ -640,3 +742,59 @@ jobs:
640742
- name: Inspect image
641743
run: |
642744
docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ steps.meta.outputs.version }}
745+
746+
merge-slim-images:
747+
runs-on: ubuntu-latest
748+
needs: [build-slim-image]
749+
steps:
750+
# GitHub Packages requires the entire repository name to be in lowercase
751+
# although the repository owner has a lowercase username, this prevents some people from running actions after forking
752+
- name: Set repository and image name to lowercase
753+
run: |
754+
echo "IMAGE_NAME=${IMAGE_NAME,,}" >>${GITHUB_ENV}
755+
echo "FULL_IMAGE_NAME=ghcr.io/${IMAGE_NAME,,}" >>${GITHUB_ENV}
756+
env:
757+
IMAGE_NAME: '${{ github.repository }}'
758+
759+
- name: Download digests
760+
uses: actions/download-artifact@v4
761+
with:
762+
pattern: digests-slim-*
763+
path: /tmp/digests
764+
merge-multiple: true
765+
766+
- name: Set up Docker Buildx
767+
uses: docker/setup-buildx-action@v3
768+
769+
- name: Log in to the Container registry
770+
uses: docker/login-action@v3
771+
with:
772+
registry: ${{ env.REGISTRY }}
773+
username: ${{ github.actor }}
774+
password: ${{ secrets.GITHUB_TOKEN }}
775+
776+
- name: Extract metadata for Docker images (default slim tag)
777+
id: meta
778+
uses: docker/metadata-action@v5
779+
with:
780+
images: ${{ env.FULL_IMAGE_NAME }}
781+
tags: |
782+
type=ref,event=branch
783+
type=ref,event=tag
784+
type=sha,prefix=git-
785+
type=semver,pattern={{version}}
786+
type=semver,pattern={{major}}.{{minor}}
787+
type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=slim
788+
flavor: |
789+
latest=${{ github.ref == 'refs/heads/main' }}
790+
suffix=-slim,onlatest=true
791+
792+
- name: Create manifest list and push
793+
working-directory: /tmp/digests
794+
run: |
795+
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
796+
$(printf '${{ env.FULL_IMAGE_NAME }}@sha256:%s ' *)
797+
798+
- name: Inspect image
799+
run: |
800+
docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ steps.meta.outputs.version }}

Dockerfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# use build args in the docker build command with --build-arg="BUILDARG=true"
44
ARG USE_CUDA=false
55
ARG USE_OLLAMA=false
6+
ARG USE_SLIM=false
67
# Tested with cu117 for CUDA 11 and cu121 for CUDA 12 (default)
78
ARG USE_CUDA_VER=cu128
89
# any sentence transformer model; models to use can be found at https://huggingface.co/models?library=sentence-transformers
@@ -43,6 +44,7 @@ FROM python:3.11-slim-bookworm AS base
4344
ARG USE_CUDA
4445
ARG USE_OLLAMA
4546
ARG USE_CUDA_VER
47+
ARG USE_SLIM
4648
ARG USE_EMBEDDING_MODEL
4749
ARG USE_RERANKING_MODEL
4850
ARG UID
@@ -54,6 +56,7 @@ ENV ENV=prod \
5456
# pass build args to the build
5557
USE_OLLAMA_DOCKER=${USE_OLLAMA} \
5658
USE_CUDA_DOCKER=${USE_CUDA} \
59+
USE_SLIM_DOCKER=${USE_SLIM} \
5760
USE_CUDA_DOCKER_VER=${USE_CUDA_VER} \
5861
USE_EMBEDDING_MODEL_DOCKER=${USE_EMBEDDING_MODEL} \
5962
USE_RERANKING_MODEL_DOCKER=${USE_RERANKING_MODEL}
@@ -120,6 +123,7 @@ RUN apt-get update && \
120123
COPY --chown=$UID:$GID ./backend/requirements.txt ./requirements.txt
121124

122125
RUN pip3 install --no-cache-dir uv && \
126+
if [ "$USE_SLIM" != "true" ]; then \
123127
if [ "$USE_CUDA" = "true" ]; then \
124128
# If you use CUDA the whisper and embedding model will be downloaded on first use
125129
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/$USE_CUDA_DOCKER_VER --no-cache-dir && \
@@ -134,10 +138,13 @@ RUN pip3 install --no-cache-dir uv && \
134138
python -c "import os; from faster_whisper import WhisperModel; WhisperModel(os.environ['WHISPER_MODEL'], device='cpu', compute_type='int8', download_root=os.environ['WHISPER_MODEL_DIR'])"; \
135139
python -c "import os; import tiktoken; tiktoken.get_encoding(os.environ['TIKTOKEN_ENCODING_NAME'])"; \
136140
fi; \
141+
else \
142+
uv pip install --system -r requirements.txt --no-cache-dir && \
143+
fi; \
137144
chown -R $UID:$GID /app/backend/data/
138145

139146
# Install Ollama if requested
140-
RUN if [ "$USE_OLLAMA" = "true" ]; then \
147+
RUN if [ [ "$USE_OLLAMA" = "true" ] && [ "$USE_SLIM" != "true" ] ]; then \
141148
date +%s > /tmp/ollama_build_hash && \
142149
echo "Cache broken at timestamp: `cat /tmp/ollama_build_hash`" && \
143150
curl -fsSL https://ollama.com/install.sh | sh && \

0 commit comments

Comments
 (0)