Skip to content

Commit

Permalink
Move vars to dynamic, add metrics (huggingface#1085)
Browse files Browse the repository at this point in the history
* Add healthcheck route

* Add prom client with basic metrics

* wip: serve metrics on a different port

* exclude server from tsconfig

* latest

* refacto(all): use class & singleton

* refacto(all): use class & singleton

* refacto(all): add request logs

* Make all the env vars dynamic (huggingface#1092)

* Add truncate to task model (huggingface#1090)

* retry text area height (huggingface#1091)

* Make all the env vars dynamic

* only check for db at runtime

* remove secret from build step

* improve dockerfile

* Wrap db in singleton

* add .env.local to dockerignore

* changes to dockerfile

* lint

* aborted generations

* move collections build check

* Use a single dockerfile

* lint

* fixes

* lint

* don't return db during building

* remove dev

---------

Co-authored-by: Victor Muštar <[email protected]>

* refacto(all): update default metrics port

* fix recursion error

* add version check

* revert vite preview

* Move request logs to debug level and add an env var to filter by log level in dev mode

* Set package version if needed

* Set log level for prod and dev

---------

Co-authored-by: rtrompier <[email protected]>
Co-authored-by: Victor Muštar <[email protected]>
  • Loading branch information
3 people authored May 3, 2024
1 parent e441e69 commit 4c26efb
Show file tree
Hide file tree
Showing 72 changed files with 1,303 additions and 562 deletions.
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
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

0 comments on commit 4c26efb

Please sign in to comment.