From bece6253d5a068490ccd11b1820adb167faaa4ed Mon Sep 17 00:00:00 2001 From: Fynn Petersen-Frey Date: Wed, 13 Jul 2022 14:42:00 +0200 Subject: [PATCH] Improve Docker setup and reduce memory usage of production containers (#338) --- server/apps/immich/src/main.ts | 8 ++++---- server/package.json | 2 +- server/start-microservices.sh | 2 +- server/start-server.sh | 2 +- web/Dockerfile | 22 +++++----------------- web/entrypoint.sh | 7 ++++++- web/src/hooks.ts | 2 +- 7 files changed, 19 insertions(+), 26 deletions(-) diff --git a/server/apps/immich/src/main.ts b/server/apps/immich/src/main.ts index 770e3c2e2d..51afa4288f 100644 --- a/server/apps/immich/src/main.ts +++ b/server/apps/immich/src/main.ts @@ -46,12 +46,12 @@ async function bootstrap() { customSiteTitle: 'Immich API Documentation', }); - // Generate API Documentation - const outputPath = path.resolve(process.cwd(), 'immich-openapi-specs.json'); - writeFileSync(outputPath, JSON.stringify(apiDocument), { encoding: 'utf8' }); - + await app.listen(3001, () => { if (process.env.NODE_ENV == 'development') { + // Generate API Documentation only in development mode + const outputPath = path.resolve(process.cwd(), 'immich-openapi-specs.json'); + writeFileSync(outputPath, JSON.stringify(apiDocument), { encoding: 'utf8' }); Logger.log('Running Immich Server in DEVELOPMENT environment', 'ImmichServer'); } diff --git a/server/package.json b/server/package.json index 7eccabdd05..0f6349b982 100644 --- a/server/package.json +++ b/server/package.json @@ -7,7 +7,7 @@ "license": "UNLICENSED", "scripts": { "prebuild": "rimraf dist", - "build": "nest build", + "build": "nest build immich && nest build microservices", "format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"", "start": "nest start", "start:dev": "nest start --watch", diff --git a/server/start-microservices.sh b/server/start-microservices.sh index 4e1f9c12eb..d76065712e 100644 --- a/server/start-microservices.sh +++ b/server/start-microservices.sh @@ -1 +1 @@ -npm start microservices \ No newline at end of file +node dist/apps/microservices/apps/microservices/src/main \ No newline at end of file diff --git a/server/start-server.sh b/server/start-server.sh index 9f35169bf6..5bbc6dd3cc 100644 --- a/server/start-server.sh +++ b/server/start-server.sh @@ -1 +1 @@ -npm start immich \ No newline at end of file +node dist/apps/immich/apps/immich/src/main \ No newline at end of file diff --git a/web/Dockerfile b/web/Dockerfile index 8d3c500ed3..b5db9c9e81 100644 --- a/web/Dockerfile +++ b/web/Dockerfile @@ -5,35 +5,23 @@ WORKDIR /usr/src/app RUN chown node:node /usr/src/app -COPY --chown=node:node package*.json ./ +RUN apk add --no-cache setpriv -RUN apk add --update-cache build-base python3 +COPY --chown=node:node package*.json ./ RUN npm ci COPY --chown=node:node . . EXPOSE 3000 -EXPOSE 24678 FROM base AS dev ENV CHOKIDAR_USEPOLLING=true +EXPOSE 24678 CMD ["npm", "run", "dev"] -FROM node:16-alpine3.14 as prod - -WORKDIR /usr/src/app - -RUN chown node:node /usr/src/app - -COPY --chown=node:node package*.json ./ -COPY --chown=node:node . . - -RUN apk add --update-cache build-base python3 - -RUN npm ci - -EXPOSE 3000 +FROM base as prod +ENV NODE_ENV=production # Issue build command in entrypoint.sh to capture user .env file instead of the builder .env file. diff --git a/web/entrypoint.sh b/web/entrypoint.sh index 291964f828..dc06f9b738 100644 --- a/web/entrypoint.sh +++ b/web/entrypoint.sh @@ -1 +1,6 @@ -npm run build && node /usr/src/app/build/index.js \ No newline at end of file +npm run build +if [ `id -u` -eq 0 ] && [ -n "$PUID" ] && [ -n "$PGID" ]; then + exec setpriv --reuid $PUID --regid $PGID --clear-groups node /usr/src/app/build/index.js +else + node /usr/src/app/build/index.js +fi \ No newline at end of file diff --git a/web/src/hooks.ts b/web/src/hooks.ts index 17f4647b75..afb4181647 100644 --- a/web/src/hooks.ts +++ b/web/src/hooks.ts @@ -1,7 +1,7 @@ import type { GetSession, Handle } from '@sveltejs/kit'; import * as cookie from 'cookie'; import { api } from '@api'; -import { AxiosError } from 'axios'; +import AxiosError from 'axios'; export const handle: Handle = async ({ event, resolve }) => { const cookies = cookie.parse(event.request.headers.get('cookie') || '');