Update Ubuntu base image and improve Dockerfile (#609)

* Update Ubuntu base image and improve Dockerfile

* Add unzip to Docker image dependencies

Needed for the arm64 build

* reset tabs

* formalized lint rules for hadolint. however the Docker formatting is being handled by MS Docker extension which doesn't indent code as expected. WIP.

* found a workaround to keep formatting

---------

Co-authored-by: timothycarambat <rambat1010@gmail.com>
This commit is contained in:
Francisco Bischoff 2024-03-07 00:34:45 +00:00 committed by GitHub
parent 5907eb9939
commit 9ce3d1150d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 62 additions and 30 deletions

8
.hadolint.yaml Normal file
View File

@ -0,0 +1,8 @@
failure-threshold: warning
ignored:
- DL3008
- DL3013
format: tty
trustedRegistries:
- docker.io
- gcr.io

View File

@ -18,5 +18,6 @@
"Weaviate", "Weaviate",
"Zilliz" "Zilliz"
], ],
"eslint.experimental.useFlatConfig": true "eslint.experimental.useFlatConfig": true,
"docker.languageserver.formatter.ignoreMultilineInstructions": true
} }

View File

@ -1,12 +1,17 @@
# Setup base image # Setup base image
FROM ubuntu:jammy-20230522 AS base FROM ubuntu:jammy-20230916 AS base
# Build arguments
ARG ARG_UID=1000 ARG ARG_UID=1000
ARG ARG_GID=1000 ARG ARG_GID=1000
FROM base AS build-arm64 FROM base AS build-arm64
RUN echo "Preparing build of AnythingLLM image for arm64 architecture" RUN echo "Preparing build of AnythingLLM image for arm64 architecture"
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# Install system dependencies
# hadolint ignore=DL3008,DL3013
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \ DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
unzip curl gnupg libgfortran5 libgbm1 tzdata netcat \ unzip curl gnupg libgfortran5 libgbm1 tzdata netcat \
@ -25,8 +30,8 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
&& rm yarn_1.22.19_all.deb && rm yarn_1.22.19_all.deb
# Create a group and user with specific UID and GID # Create a group and user with specific UID and GID
RUN groupadd -g $ARG_GID anythingllm && \ RUN groupadd -g "$ARG_GID" anythingllm && \
useradd -u $ARG_UID -m -d /app -s /bin/bash -g anythingllm anythingllm && \ useradd -l -u "$ARG_UID" -m -d /app -s /bin/bash -g anythingllm anythingllm && \
mkdir -p /app/frontend/ /app/server/ /app/collector/ && chown -R anythingllm:anythingllm /app mkdir -p /app/frontend/ /app/server/ /app/collector/ && chown -R anythingllm:anythingllm /app
# Copy docker helper scripts # Copy docker helper scripts
@ -61,6 +66,10 @@ RUN echo "Done running arm64 specific installtion steps"
FROM base AS build-amd64 FROM base AS build-amd64
RUN echo "Preparing build of AnythingLLM image for non-ARM architecture" RUN echo "Preparing build of AnythingLLM image for non-ARM architecture"
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# Install system dependencies
# hadolint ignore=DL3008,DL3013
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \ DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
curl gnupg libgfortran5 libgbm1 tzdata netcat \ curl gnupg libgfortran5 libgbm1 tzdata netcat \
@ -79,8 +88,8 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
&& rm yarn_1.22.19_all.deb && rm yarn_1.22.19_all.deb
# Create a group and user with specific UID and GID # Create a group and user with specific UID and GID
RUN groupadd -g $ARG_GID anythingllm && \ RUN groupadd -g "$ARG_GID" anythingllm && \
useradd -u $ARG_UID -m -d /app -s /bin/bash -g anythingllm anythingllm && \ useradd -l -u "$ARG_UID" -m -d /app -s /bin/bash -g anythingllm anythingllm && \
mkdir -p /app/frontend/ /app/server/ /app/collector/ && chown -R anythingllm:anythingllm /app mkdir -p /app/frontend/ /app/server/ /app/collector/ && chown -R anythingllm:anythingllm /app
# Copy docker helper scripts # Copy docker helper scripts
@ -95,6 +104,8 @@ RUN chmod +x /usr/local/bin/docker-entrypoint.sh && \
############################################# #############################################
# COMMON BUILD FLOW FOR ALL ARCHS # COMMON BUILD FLOW FOR ALL ARCHS
############################################# #############################################
# hadolint ignore=DL3006
FROM build-${TARGETARCH} AS build FROM build-${TARGETARCH} AS build
RUN echo "Running common build flow of AnythingLLM image for all architectures" RUN echo "Running common build flow of AnythingLLM image for all architectures"
@ -102,43 +113,54 @@ USER anythingllm
WORKDIR /app WORKDIR /app
# Install frontend dependencies # Install frontend dependencies
FROM build as frontend-deps FROM build AS frontend-deps
COPY ./frontend/package.json ./frontend/yarn.lock ./frontend/ COPY ./frontend/package.json ./frontend/yarn.lock ./frontend/
RUN cd ./frontend/ && yarn install --network-timeout 100000 && yarn cache clean WORKDIR /app/frontend
RUN yarn install --network-timeout 100000 && yarn cache clean
WORKDIR /app
# Install server dependencies # Install server dependencies
FROM build as server-deps FROM build AS server-deps
COPY ./server/package.json ./server/yarn.lock ./server/ COPY ./server/package.json ./server/yarn.lock ./server/
RUN cd ./server/ && yarn install --production --network-timeout 100000 && yarn cache clean WORKDIR /app/server
RUN yarn install --production --network-timeout 100000 && yarn cache clean
WORKDIR /app
# Compile Llama.cpp bindings for node-llama-cpp for this operating system. # Compile Llama.cpp bindings for node-llama-cpp for this operating system.
USER root USER root
RUN cd ./server && npx --no node-llama-cpp download WORKDIR /app/server
RUN npx --no node-llama-cpp download
WORKDIR /app
USER anythingllm USER anythingllm
# Build the frontend # Build the frontend
FROM frontend-deps as build-stage FROM frontend-deps AS build-stage
COPY ./frontend/ ./frontend/ COPY ./frontend/ ./frontend/
RUN cd ./frontend/ && yarn build && yarn cache clean WORKDIR /app/frontend
RUN yarn build && yarn cache clean
WORKDIR /app
# Setup the server # Setup the server
FROM server-deps as production-stage FROM server-deps AS production-stage
COPY --chown=anythingllm:anythingllm ./server/ ./server/ COPY --chown=anythingllm:anythingllm ./server/ ./server/
# Copy built static frontend files to the server public directory # Copy built static frontend files to the server public directory
COPY --from=build-stage /app/frontend/dist ./server/public COPY --chown=anythingllm:anythingllm --from=build-stage /app/frontend/dist ./server/public
# Copy the collector # Copy the collector
COPY --chown=anythingllm:anythingllm ./collector/ ./collector/ COPY --chown=anythingllm:anythingllm ./collector/ ./collector/
# Install collector dependencies # Install collector dependencies
WORKDIR /app/collector
ENV PUPPETEER_DOWNLOAD_BASE_URL=https://storage.googleapis.com/chrome-for-testing-public ENV PUPPETEER_DOWNLOAD_BASE_URL=https://storage.googleapis.com/chrome-for-testing-public
RUN cd /app/collector && yarn install --production --network-timeout 100000 && yarn cache clean RUN yarn install --production --network-timeout 100000 && yarn cache clean
# Migrate and Run Prisma against known schema # Migrate and Run Prisma against known schema
RUN cd ./server && npx prisma generate --schema=./prisma/schema.prisma WORKDIR /app/server
RUN cd ./server && npx prisma migrate deploy --schema=./prisma/schema.prisma RUN npx prisma generate --schema=./prisma/schema.prisma && \
npx prisma migrate deploy --schema=./prisma/schema.prisma
WORKDIR /app
# Setup the environment # Setup the environment
ENV NODE_ENV=production ENV NODE_ENV=production

View File

@ -1,8 +1,9 @@
#!/bin/bash #!/bin/bash
{ cd /app/server/ &&\ {
npx prisma generate --schema=./prisma/schema.prisma &&\ cd /app/server/ &&
npx prisma migrate deploy --schema=./prisma/schema.prisma &&\ npx prisma generate --schema=./prisma/schema.prisma &&
node /app/server/index.js npx prisma migrate deploy --schema=./prisma/schema.prisma &&
node /app/server/index.js
} & } &
{ node /app/collector/index.js; } & { node /app/collector/index.js; } &
wait -n wait -n

View File

@ -4,10 +4,10 @@
response=$(curl --write-out '%{http_code}' --silent --output /dev/null http://localhost:3001/api/ping) response=$(curl --write-out '%{http_code}' --silent --output /dev/null http://localhost:3001/api/ping)
# If the HTTP response code is 200 (OK), the server is up # If the HTTP response code is 200 (OK), the server is up
if [ $response -eq 200 ]; then if [ "$response" -eq 200 ]; then
echo "Server is up" echo "Server is up"
exit 0 exit 0
else else
echo "Server is down" echo "Server is down"
exit 1 exit 1
fi fi