Skip to content

Commit

Permalink
fix(api/docker): optimise docker
Browse files Browse the repository at this point in the history
  • Loading branch information
kriptonian1 authored and rajdip-b committed Sep 14, 2024
1 parent b03ce8e commit 7e150d4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
7 changes: 6 additions & 1 deletion apps/api/.dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
node_modules/
node_modules/
dist/
.git
.gitignore
Dockerfile
.dockerignore
45 changes: 30 additions & 15 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20-alpine AS build
FROM node:20.0.0-alpine AS build

WORKDIR /app

Expand All @@ -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"]
# 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"]

0 comments on commit 7e150d4

Please sign in to comment.