From 8bf331496a3dd81bd7fab47a827adb47484dc931 Mon Sep 17 00:00:00 2001 From: Bram Borggreve Date: Sun, 18 Aug 2024 01:09:33 +0100 Subject: [PATCH 1/2] chore: optimize docker image size --- Dockerfile | 82 +++++++++++++++++++++++++++++-------------- apps/api/package.json | 5 +++ apps/api/project.json | 3 +- docker-compose.yml | 14 ++++++++ prisma/schema.prisma | 3 ++ 5 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 apps/api/package.json diff --git a/Dockerfile b/Dockerfile index f79c1ad..26226b0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,37 +1,67 @@ -################################################################################ -# BASE -# This is the stage that the other stages in this file are based on. -# - defines the Node version -# - set global configuration -# - set default work dir -################################################################################ -FROM node:20.10-alpine as base +# Base image +FROM node:22-bookworm-slim AS base -RUN apk add --update --no-cache git python3 make g++ +# Set the working directory +WORKDIR /workspace -RUN npm install -g pnpm +# Configure pnpm so it can be cached +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" -ENV HUSKY=0 +# Install pnpm with corepack +RUN corepack enable +RUN corepack prepare pnpm@9.6.0 --activate -# Create app directory -WORKDIR /workspace +# Install openssl for Prisma. Add sqlite3 here if you need it. +RUN apt-get update && apt-get install -y openssl && rm -rf /var/lib/apt/lists/* + +# Setup development node_modules +FROM base AS dev-deps + +ADD package.json pnpm-lock.yaml ./ + +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile + +# Build the app +FROM base AS build + +# Get the node_modules from the dev-deps stage +COPY --from=dev-deps /workspace/node_modules /workspace/node_modules + +## Add and generate the Prisma client +#ADD prisma prisma +#RUN pnpx prisma generate +# +# Add the rest of the app +ADD . . + +# Build the app +RUN pnpm run build + +# Show the contents of the workspace +RUN find /workspace -type f + +# Setup production node_modules +FROM base AS prod-deps + +ENV NODE_ENV=production -# Copy package.json and the lock file -COPY package.json pnpm-lock.yaml /workspace/ +COPY --from=build /workspace/dist/apps/api/package.json /workspace +COPY --from=build /workspace/dist/apps/api/pnpm-lock.yaml /workspace -# Install app dependencies -RUN pnpm install --frozen-lockfile --prefer-frozen-lockfile +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --no-frozen-lockfile --no-optional +#RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install prisma -# Copy source files -COPY . . -# Build apps -RUN pnpm build +# Build the production image +FROM base -COPY prisma/schema.prisma /workspace/prisma/schema.prisma +ENV NODE_ENV=production -# Expose default port -EXPOSE 3000 +COPY --from=prod-deps /workspace/node_modules /workspace/node_modules +COPY --from=prod-deps /workspace/package.json /workspace/package.json +COPY --from=build /workspace/node_modules/.prisma /workspace/node_modules/.prisma +COPY --from=build /workspace/dist /workspace/dist +COPY --from=build /workspace/prisma /workspace/prisma -# Start server -CMD pnpm prestart; pnpm start +CMD [ "node", "dist/apps/api/main.js" ] diff --git a/apps/api/package.json b/apps/api/package.json new file mode 100644 index 0000000..f4e132f --- /dev/null +++ b/apps/api/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "prisma": "5.15.1" + } +} diff --git a/apps/api/project.json b/apps/api/project.json index cfa1ce2..f2afc8a 100644 --- a/apps/api/project.json +++ b/apps/api/project.json @@ -17,7 +17,8 @@ "tsConfig": "apps/api/tsconfig.app.json", "assets": ["apps/api/src/assets"], "isolatedConfig": true, - "webpackConfig": "apps/api/webpack.config.js" + "webpackConfig": "apps/api/webpack.config.js", + "generatePackageJson": true }, "configurations": { "development": {}, diff --git a/docker-compose.yml b/docker-compose.yml index e9f8acb..d94d427 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,19 @@ version: '3' services: + api: + build: + context: . + dockerfile: Dockerfile + ports: + - 3000:3000 + environment: + DATABASE_URL: postgresql://pubkey-stack:pubkey-stack@postgres:5432/pubkey-stack?schema=public + DATABASE_PROVISION: true + HOST: 0.0.0.0 + JWT_SECRET: 1dfe5003518560c6362eab48f8220edf8708bbc67efbd4ed8cdf621965e938ee + PORT: 3000 + SESSION_SECRET: 1dfe5003518560c6362eab48f8220edf8708bbc67efbd4ed8cdf621965e938ee + WEB_URL: http://localhost:4200 postgres: image: postgres:15-alpine # command: [ "postgres", "-c", "log_statement=all", "-c", "log_destination=stderr" ] diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 28cd432..ec30933 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,5 +1,8 @@ generator client { provider = "prisma-client-js" + // This is needed to make the Prisma Client work in a Docker container built using pnpm. + // Found here: https://github.com/prisma/prisma/issues/6603#issuecomment-1054227614 + output = "../node_modules/.prisma/client" } datasource db { From 98a1f42b25a673465f22950649c2cda792994674 Mon Sep 17 00:00:00 2001 From: Bram Borggreve Date: Sun, 18 Aug 2024 01:15:59 +0100 Subject: [PATCH 2/2] chore: update pnpm version --- .github/workflows/build-publish-docker.yml | 2 -- nx.json | 7 ++++++- package.json | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-publish-docker.yml b/.github/workflows/build-publish-docker.yml index 5136447..9dd2b4f 100644 --- a/.github/workflows/build-publish-docker.yml +++ b/.github/workflows/build-publish-docker.yml @@ -20,8 +20,6 @@ jobs: steps: - uses: pnpm/action-setup@v2 - with: - version: 8 - name: Checkout repository uses: actions/checkout@v3 diff --git a/nx.json b/nx.json index 723afd6..bb7bd14 100644 --- a/nx.json +++ b/nx.json @@ -1,6 +1,6 @@ { "$schema": "./node_modules/nx/schemas/nx-schema.json", - "workspaceLayout": { "projectNameAndRootFormat": "as-provided" }, + "workspaceLayout": {}, "targetDefaults": { "build": { "dependsOn": ["^build"], "cache": true }, "e2e": { "cache": true }, @@ -13,6 +13,11 @@ "@nx/eslint:lint": { "inputs": ["default", "{workspaceRoot}/.eslintrc.json", "{workspaceRoot}/.eslintignore"], "cache": true + }, + "@nx/js:tsc": { + "cache": true, + "dependsOn": ["^build"], + "inputs": ["default", "^default"] } }, "generators": { "@nx/react": { "application": { "babel": true }, "library": { "unitTestRunner": "none" } } }, diff --git a/package.json b/package.json index 35d5978..36a3754 100644 --- a/package.json +++ b/package.json @@ -189,5 +189,5 @@ "pnpm nx format --uncommitted" ] }, - "packageManager": "pnpm@9.6.0+sha256.dae0f7e822c56b20979bb5965e3b73b8bdabb6b8b8ef121da6d857508599ca35" + "packageManager": "pnpm@9.7.1+sha256.46f1bbc8f8020aa9869568c387198f1a813f21fb44c82f400e7d1dbde6c70b40" }