|
| 1 | +# Stage 1: Base dependencies (common stage) |
| 2 | +FROM ubuntu:22.04 as common |
| 3 | + |
| 4 | +ENV DEBIAN_FRONTEND=noninteractive |
| 5 | + |
| 6 | +# Install common dependencies |
| 7 | +RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 8 | + ca-certificates \ |
| 9 | + software-properties-common \ |
| 10 | + curl \ |
| 11 | + wget \ |
| 12 | + jq \ |
| 13 | + tar \ |
| 14 | + openmpi-bin \ |
| 15 | + libopenmpi-dev && \ |
| 16 | + apt-get clean && \ |
| 17 | + rm -rf /var/lib/apt/lists/* |
| 18 | + |
| 19 | +# Stage 2: Build dependencies and compilation |
| 20 | +FROM common as build |
| 21 | + |
| 22 | +# Install Dependencies |
| 23 | +RUN wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null && \ |
| 24 | + apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main" && \ |
| 25 | + apt-get update && \ |
| 26 | + apt-get install -y --no-install-recommends \ |
| 27 | + cmake \ |
| 28 | + make \ |
| 29 | + git \ |
| 30 | + uuid-dev \ |
| 31 | + lsb-release \ |
| 32 | + gpg \ |
| 33 | + zip \ |
| 34 | + unzip \ |
| 35 | + gcc \ |
| 36 | + g++ \ |
| 37 | + ninja-build \ |
| 38 | + pkg-config \ |
| 39 | + python3-pip \ |
| 40 | + openssl && \ |
| 41 | + pip3 install awscli && \ |
| 42 | + apt-get clean && \ |
| 43 | + rm -rf /var/lib/apt/lists/* |
| 44 | + |
| 45 | +ARG CORTEX_CPP_VERSION=latest |
| 46 | +ARG CMAKE_EXTRA_FLAGS="" |
| 47 | + |
| 48 | +WORKDIR /app |
| 49 | + |
| 50 | +# Build arguments for remote cache |
| 51 | +ARG REMOTE_CACHE_URL="" |
| 52 | +ARG MINIO_ENDPOINT_URL="" |
| 53 | +ARG MINIO_ACCESS_KEY="" |
| 54 | +ARG MINIO_SECRET_KEY="" |
| 55 | + |
| 56 | +# Configure cache |
| 57 | +ENV LOCAL_CACHE_DIR="/vcpkg-cache" |
| 58 | +RUN mkdir -p ${LOCAL_CACHE_DIR} |
| 59 | + |
| 60 | +# Configure MinIO alias (if remote cache is provided) |
| 61 | +RUN if [ -n "${REMOTE_CACHE_URL}" ]; then \ |
| 62 | + echo "Setting up MinIO for remote cache..." && \ |
| 63 | + aws configure set default.s3.signature_version s3v4 && \ |
| 64 | + aws configure set aws_access_key_id ${MINIO_ACCESS_KEY} && \ |
| 65 | + aws configure set aws_secret_access_key ${MINIO_SECRET_KEY} && \ |
| 66 | + aws configure set default.region us-east-1; \ |
| 67 | + else \ |
| 68 | + echo "No remote cache provided, using local fallback..."; \ |
| 69 | + fi |
| 70 | + |
| 71 | +# Sync cache from MinIO (if remote cache is provided) |
| 72 | +RUN if [ -n "${REMOTE_CACHE_URL}" ]; then \ |
| 73 | + echo "Downloading cache from MinIO..." && \ |
| 74 | + aws --endpoint-url=${MINIO_ENDPOINT_URL} s3 sync s3://vcpkg-cache ${LOCAL_CACHE_DIR}; \ |
| 75 | + else \ |
| 76 | + echo "No remote cache provided, skipping download."; \ |
| 77 | + fi |
| 78 | + |
| 79 | +# Copy source code |
| 80 | +COPY ./engine /app/engine |
| 81 | +COPY ./docs/static/openapi/cortex.json /app/docs/static/openapi/cortex.json |
| 82 | + |
| 83 | +# Build project |
| 84 | +# Configure vcpkg binary sources |
| 85 | +RUN export VCPKG_BINARY_SOURCES="files,${LOCAL_CACHE_DIR},readwrite;default"; \ |
| 86 | + cd engine && make configure-vcpkg && make build CMAKE_EXTRA_FLAGS="-DCORTEX_CPP_VERSION=${CORTEX_CPP_VERSION} -DCMAKE_BUILD_TEST=OFF -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake ${CMAKE_EXTRA_FLAGS}" |
| 87 | + |
| 88 | + # Upload updated cache to MinIO (if remote cache is provided) |
| 89 | +RUN if [ -n "${REMOTE_CACHE_URL}" ]; then \ |
| 90 | + echo "Uploading cache to MinIO..." && \ |
| 91 | + aws --endpoint-url=${MINIO_ENDPOINT_URL} s3 sync ${LOCAL_CACHE_DIR} s3://vcpkg-cache; \ |
| 92 | + else \ |
| 93 | + echo "No remote cache provided, skipping upload."; \ |
| 94 | + fi |
| 95 | + |
| 96 | +# Stage 3: Runtime |
| 97 | +FROM common as runtime |
| 98 | + |
| 99 | +WORKDIR /app |
| 100 | + |
| 101 | +# Copy built binaries from the build stage |
| 102 | +COPY --from=build /app/engine/build/cortex /usr/local/bin/cortex |
| 103 | +COPY --from=build /app/engine/build/cortex-server /usr/local/bin/cortex-server |
| 104 | + |
| 105 | +COPY ./docker/download-cortex.llamacpp.sh /tmp/download-cortex.llamacpp.sh |
| 106 | +COPY ./docker/entrypoint.sh /usr/local/bin/entrypoint.sh |
| 107 | + |
| 108 | +# Get the latest version of Cortex Llama |
| 109 | +ARG CORTEX_LLAMACPP_VERSION=latest |
| 110 | +RUN chmod +x /tmp/download-cortex.llamacpp.sh && /bin/bash /tmp/download-cortex.llamacpp.sh ${CORTEX_LLAMACPP_VERSION} |
| 111 | + |
| 112 | +# Configure entrypoint |
| 113 | +RUN chmod +x /usr/local/bin/entrypoint.sh |
| 114 | + |
| 115 | +EXPOSE 39281 |
| 116 | + |
| 117 | +# Healthcheck |
| 118 | +HEALTHCHECK --interval=300s --timeout=30s --start-period=10s --retries=3 \ |
| 119 | + CMD curl -f http://127.0.0.1:39281/healthz || exit 1 |
| 120 | + |
| 121 | +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] |
0 commit comments