From 7e150d4f1347eaff29c6c921250e9ef3938367bd Mon Sep 17 00:00:00 2001 From: kriptonian1 Date: Wed, 17 Jul 2024 08:55:25 +0530 Subject: [PATCH] fix(api/docker): optimise docker --- apps/api/.dockerignore | 7 ++++++- apps/api/Dockerfile | 45 ++++++++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/apps/api/.dockerignore b/apps/api/.dockerignore index 40b878db..a2b9f8e3 100644 --- a/apps/api/.dockerignore +++ b/apps/api/.dockerignore @@ -1 +1,6 @@ -node_modules/ \ No newline at end of file +node_modules/ +dist/ +.git +.gitignore +Dockerfile +.dockerignore \ No newline at end of file diff --git a/apps/api/Dockerfile b/apps/api/Dockerfile index 3bba611d..88ce4c87 100644 --- a/apps/api/Dockerfile +++ b/apps/api/Dockerfile @@ -1,4 +1,4 @@ -FROM node:20-alpine AS build +FROM node:20.0.0-alpine AS build WORKDIR /app @@ -8,25 +8,40 @@ ARG SENTRY_ENVIRONMENT=${SENTRY_ENVIRONMENT} ARG SENTRY_AUTH_TOKEN=${SENTRY_AUTH_TOKEN} ARG DATABASE_URL=${DATABASE_URL} -RUN npm i -g pnpm +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" -COPY package.json . -COPY turbo.json . -COPY pnpm-workspace.yaml . +RUN corepack enable -COPY apps/api/package.json apps/api/package.json -COPY apps/api/tsconfig.json apps/api/tsconfig.json +COPY --chown=root:root --chmod=755 package.json turbo.json pnpm-*.yaml ./ +COPY --chown=root:root --chmod=755 apps/api/package.json apps/api/tsconfig.json apps/api/ +COPY --chown=root:root --chmod=755 packages packages -COPY apps/api/src apps/api/src +RUN --mount=type=cache,id=pnpm,target=/pnpm/store \ + pnpm install --ignore-scripts --frozen-lockfile && \ + rm -rf /root/.npm /root/.node-gyp /tmp/npm-* -COPY packages packages +COPY --chown=root:root --chmod=755 apps/api/src apps/api/src -RUN pnpm install +RUN pnpm db:generate-types && \ + pnpm build:api && \ + pnpm sourcemaps:api -RUN pnpm db:generate-types -RUN pnpm build:api -RUN pnpm sourcemaps:api +USER node -EXPOSE ${API_PORT:-4200} +FROM node:20-alpine AS prod -ENTRYPOINT ["node", "apps/api/dist/main.js"] \ No newline at end of file +# Don't run production as root +USER node + +ENV NODE_ENV=dev + +WORKDIR /app + +COPY --chown=root:root --chmod=755 --from=build /app/node_modules /app/node_modules +COPY --chown=root:root --chmod=755 --from=build /app/apps/api/node_modules /app/apps/api/node_modules +COPY --chown=root:root --chmod=755 --from=build /app/apps/api/dist /app/apps/api/dist + +EXPOSE ${API_PORT} + +ENTRYPOINT ["node", "/app/apps/api/dist/main.js"] \ No newline at end of file