Skip to content

Commit 4c26efb

Browse files
nsarrazinrtrompiergary149
authored
Move vars to dynamic, add metrics (huggingface#1085)
* 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]>
1 parent e441e69 commit 4c26efb

File tree

72 files changed

+1303
-562
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1303
-562
lines changed

Diff for: .dockerignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ node_modules/
88
.svelte-kit/
99
.env*
1010
!.env
11-
!.env.local
11+
.env.local

Diff for: .env

+2
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,5 @@ ALLOWED_USER_EMAILS=`[]` # if it's defined, only these emails will be allowed to
154154

155155
USAGE_LIMITS=`{}`
156156
ALLOW_INSECURE_COOKIES=false # recommended to keep this to false but set to true if you need to run over http without tls
157+
METRICS_PORT=
158+
LOG_LEVEL=info

Diff for: .github/workflows/build-image.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
branches:
99
- "*"
1010
paths:
11-
- "Dockerfile.local"
11+
- "Dockerfile"
1212
- "entrypoint.sh"
1313
workflow_dispatch:
1414
release:
@@ -62,7 +62,7 @@ jobs:
6262
uses: docker/build-push-action@v5
6363
with:
6464
context: .
65-
file: Dockerfile.local
65+
file: Dockerfile
6666
push: ${{ github.event_name != 'pull_request' }}
6767
tags: ${{ steps.meta.outputs.tags }}
6868
labels: ${{ steps.meta.outputs.labels }}
@@ -116,7 +116,7 @@ jobs:
116116
uses: docker/build-push-action@v5
117117
with:
118118
context: .
119-
file: Dockerfile.local
119+
file: Dockerfile
120120
push: ${{ github.event_name != 'pull_request' }}
121121
tags: ${{ steps.meta.outputs.tags }}
122122
labels: ${{ steps.meta.outputs.labels }}

Diff for: Dockerfile

+56-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# syntax=docker/dockerfile:1
22
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
33
# you will also find guides on how best to write your Dockerfile
4+
ARG INCLUDE_DB=false
5+
6+
# stage that install the dependencies
47
FROM node:20 as builder-production
58

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

1316
FROM builder-production as builder
1417

18+
ARG APP_BASE=
19+
ARG PUBLIC_APP_COLOR=blue
20+
1521
RUN --mount=type=cache,target=/app/.npm \
1622
npm set cache /app/.npm && \
1723
npm ci
1824

1925
COPY --link --chown=1000 . .
2026

21-
RUN --mount=type=secret,id=DOTENV_LOCAL,dst=.env.local \
22-
npm run build
27+
RUN npm run build
2328

24-
FROM node:20-slim
25-
RUN npm install -g pm2
29+
# mongo image
30+
FROM mongo:latest as mongo
2631

27-
RUN userdel -r node
32+
# image to be used if INCLUDE_DB is false
33+
FROM node:20-slim as local_db_false
34+
35+
# image to be used if INCLUDE_DB is true
36+
FROM node:20-slim as local_db_true
37+
38+
RUN apt-get update
39+
RUN apt-get install gnupg curl -y
40+
# copy mongo from the other stage
41+
COPY --from=mongo /usr/bin/mongo* /usr/bin/
42+
43+
ENV MONGODB_URL=mongodb://localhost:27017
44+
RUN mkdir -p /data/db
45+
RUN chown -R 1000:1000 /data/db
46+
47+
# final image
48+
FROM local_db_${INCLUDE_DB} as final
49+
50+
# build arg to determine if the database should be included
51+
ARG INCLUDE_DB=false
52+
ENV INCLUDE_DB=${INCLUDE_DB}
53+
54+
# svelte requires APP_BASE at build time so it must be passed as a build arg
55+
ARG APP_BASE=
56+
# tailwind requires the primary theme to be known at build time so it must be passed as a build arg
57+
ARG PUBLIC_APP_COLOR=blue
2858

29-
RUN useradd -m -u 1000 user
3059

60+
# install dotenv-cli
61+
RUN npm install -g dotenv-cli
62+
63+
# switch to a user that works for spaces
64+
RUN userdel -r node
65+
RUN useradd -m -u 1000 user
3166
USER user
3267

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

71+
WORKDIR /app
3672

37-
COPY --from=builder-production --chown=1000 /app/node_modules /app/node_modules
38-
COPY --link --chown=1000 package.json /app/package.json
39-
COPY --from=builder --chown=1000 /app/build /app/build
73+
# add a .env.local if the user doesn't bind a volume to it
74+
RUN touch /app/.env.local
75+
76+
# get the default config, the entrypoint script and the server script
77+
COPY --chown=1000 package.json /app/package.json
78+
COPY --chown=1000 .env /app/.env
79+
COPY --chown=1000 entrypoint.sh /app/entrypoint.sh
4080
COPY --chown=1000 gcp-*.json /app/
4181

42-
CMD pm2 start /app/build/index.js -i $CPU_CORES --no-daemon
82+
#import the build & dependencies
83+
COPY --from=builder --chown=1000 /app/build /app/build
84+
COPY --from=builder --chown=1000 /app/node_modules /app/node_modules
85+
86+
RUN chmod +x /app/entrypoint.sh
87+
88+
CMD ["/bin/bash", "-c", "/app/entrypoint.sh"]

Diff for: Dockerfile.local

-28
This file was deleted.

Diff for: entrypoint.sh

+5-15
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,18 @@ ENV_LOCAL_PATH=/app/.env.local
22

33
if test -z "${DOTENV_LOCAL}" ; then
44
if ! test -f "${ENV_LOCAL_PATH}" ; then
5-
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."
5+
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. "
66
fi;
77
else
88
echo "DOTENV_LOCAL was found in the ENV variables. Creating .env.local file."
99
cat <<< "$DOTENV_LOCAL" > ${ENV_LOCAL_PATH}
1010
fi;
1111

1212
if [ "$INCLUDE_DB" = "true" ] ; then
13-
echo "INCLUDE_DB is set to true."
14-
15-
MONGODB_CONFIG="MONGODB_URL=mongodb://localhost:27017"
16-
if ! grep -q "^${MONGODB_CONFIG}$" ${ENV_LOCAL_PATH}; then
17-
echo "Appending MONGODB_URL"
18-
touch /app/.env.local
19-
echo -e "\n${MONGODB_CONFIG}" >> ${ENV_LOCAL_PATH}
20-
fi
21-
22-
mkdir -p /data/db
23-
mongod &
2413
echo "Starting local MongoDB instance"
25-
14+
nohup mongod &
2615
fi;
2716

28-
npm run build
29-
npm run preview -- --host 0.0.0.0 --port 3000
17+
export PUBLIC_VERSION=$(node -p "require('./package.json').version")
18+
19+
dotenv -e /app/.env -c -- node /app/build/index.js -- --host 0.0.0.0 --port 3000

0 commit comments

Comments
 (0)