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

Move vars to dynamic, add metrics #1085

Merged
merged 18 commits into from
May 3, 2024
Merged
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
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ node_modules/
.svelte-kit/
.env*
!.env
!.env.local
.env.local
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,5 @@ ALLOWED_USER_EMAILS=`[]` # if it's defined, only these emails will be allowed to

USAGE_LIMITS=`{}`
ALLOW_INSECURE_COOKIES=false # recommended to keep this to false but set to true if you need to run over http without tls
METRICS_PORT=
LOG_LEVEL=info
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ MODELS=`[
"parameters": {
"temperature": 0.1,
"stop": ["<|eot_id|>"],
"truncate": 1024,
},
"unlisted": true
}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
branches:
- "*"
paths:
- "Dockerfile.local"
- "Dockerfile"
- "entrypoint.sh"
workflow_dispatch:
release:
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.local
file: Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down Expand Up @@ -116,7 +116,7 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.local
file: Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down
66 changes: 56 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# syntax=docker/dockerfile:1
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
# you will also find guides on how best to write your Dockerfile
ARG INCLUDE_DB=false

# stage that install the dependencies
FROM node:20 as builder-production

WORKDIR /app
Expand All @@ -12,31 +15,74 @@ RUN --mount=type=cache,target=/app/.npm \

FROM builder-production as builder

ARG APP_BASE=
ARG PUBLIC_APP_COLOR=blue

RUN --mount=type=cache,target=/app/.npm \
npm set cache /app/.npm && \
npm ci

COPY --link --chown=1000 . .

RUN --mount=type=secret,id=DOTENV_LOCAL,dst=.env.local \
npm run build
RUN npm run build

FROM node:20-slim
RUN npm install -g pm2
# mongo image
FROM mongo:latest as mongo

RUN userdel -r node
# image to be used if INCLUDE_DB is false
FROM node:20-slim as local_db_false

# image to be used if INCLUDE_DB is true
FROM node:20-slim as local_db_true

RUN apt-get update
RUN apt-get install gnupg curl -y
# copy mongo from the other stage
COPY --from=mongo /usr/bin/mongo* /usr/bin/

ENV MONGODB_URL=mongodb://localhost:27017
RUN mkdir -p /data/db
RUN chown -R 1000:1000 /data/db

# final image
FROM local_db_${INCLUDE_DB} as final

# build arg to determine if the database should be included
ARG INCLUDE_DB=false
ENV INCLUDE_DB=${INCLUDE_DB}

# svelte requires APP_BASE at build time so it must be passed as a build arg
ARG APP_BASE=
# tailwind requires the primary theme to be known at build time so it must be passed as a build arg
ARG PUBLIC_APP_COLOR=blue

RUN useradd -m -u 1000 user

# install dotenv-cli
RUN npm install -g dotenv-cli

# switch to a user that works for spaces
RUN userdel -r node
RUN useradd -m -u 1000 user
USER user

ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH

WORKDIR /app

COPY --from=builder-production --chown=1000 /app/node_modules /app/node_modules
COPY --link --chown=1000 package.json /app/package.json
COPY --from=builder --chown=1000 /app/build /app/build
# add a .env.local if the user doesn't bind a volume to it
RUN touch /app/.env.local

# get the default config, the entrypoint script and the server script
COPY --chown=1000 package.json /app/package.json
COPY --chown=1000 .env /app/.env
COPY --chown=1000 entrypoint.sh /app/entrypoint.sh
COPY --chown=1000 gcp-*.json /app/

CMD pm2 start /app/build/index.js -i $CPU_CORES --no-daemon
#import the build & dependencies
COPY --from=builder --chown=1000 /app/build /app/build
COPY --from=builder --chown=1000 /app/node_modules /app/node_modules

RUN chmod +x /app/entrypoint.sh

CMD ["/bin/bash", "-c", "/app/entrypoint.sh"]
28 changes: 0 additions & 28 deletions Dockerfile.local

This file was deleted.

20 changes: 5 additions & 15 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,18 @@ ENV_LOCAL_PATH=/app/.env.local

if test -z "${DOTENV_LOCAL}" ; then
if ! test -f "${ENV_LOCAL_PATH}" ; then
echo "DOTENV_LOCAL was not found in the ENV variables and .env.local is not set using a bind volume. We are using the default .env config."
echo "DOTENV_LOCAL was not found in the ENV variables and .env.local is not set using a bind volume. Make sure to set environment variables properly. "
fi;
else
echo "DOTENV_LOCAL was found in the ENV variables. Creating .env.local file."
cat <<< "$DOTENV_LOCAL" > ${ENV_LOCAL_PATH}
fi;

if [ "$INCLUDE_DB" = "true" ] ; then
echo "INCLUDE_DB is set to true."

MONGODB_CONFIG="MONGODB_URL=mongodb://localhost:27017"
if ! grep -q "^${MONGODB_CONFIG}$" ${ENV_LOCAL_PATH}; then
echo "Appending MONGODB_URL"
touch /app/.env.local
echo -e "\n${MONGODB_CONFIG}" >> ${ENV_LOCAL_PATH}
fi

mkdir -p /data/db
mongod &
echo "Starting local MongoDB instance"

nohup mongod &
fi;

npm run build
npm run preview -- --host 0.0.0.0 --port 3000
export PUBLIC_VERSION=$(node -p "require('./package.json').version")

dotenv -e /app/.env -c -- node /app/build/index.js -- --host 0.0.0.0 --port 3000
Loading
Loading