Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(api): Optimize dockerfile - WIP #5454

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 10 additions & 14 deletions apps/api/Dockerfile
@@ -1,11 +1,10 @@
FROM node:20-alpine3.19 as dev_base
RUN apk add g++ make py3-pip
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can exclude cache because we use dev_base in the final step RUN apk add --no-cache g++ make py3-pip


ENV NX_DAEMON=false
ENV NX_DAEMON=false \
NODE_ENV=production

RUN npm i pm2 -g
RUN npm --no-update-notifier --no-fund --global install [email protected]
RUN pnpm --version

USER 1000
WORKDIR /usr/src/app
Expand All @@ -14,9 +13,7 @@ WORKDIR /usr/src/app
FROM dev_base AS dev
ARG PACKAGE_PATH

COPY --chown=1000:1000 ./meta .
COPY --chown=1000:1000 ./deps .
COPY --chown=1000:1000 ./pkg .
COPY --chown=1000:1000 ./meta ./deps ./pkg ./
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interim stage, I think we don't have to unite these layers because we won't use dev, it's important to decrease dev_base and prod. Docker caches each line in the Dockerfile, and that the output of one line is the input of the next. So if a line generates new output all subsequent caches are invalidated. In this case we lost an opportunity to use cache even if only last folder will be changed we copy all of them.


RUN --mount=type=secret,id=BULL_MQ_PRO_NPM_TOKEN,uid=1000 export BULL_MQ_PRO_NPM_TOKEN=$(cat /run/secrets/BULL_MQ_PRO_NPM_TOKEN) && \
if [ -n "${BULL_MQ_PRO_NPM_TOKEN}" ] ; then echo 'Building with Enterprise Edition of Novu'; rm -f .npmrc ; cp .npmrc-cloud .npmrc ; fi
Expand All @@ -31,10 +28,9 @@ RUN --mount=type=secret,id=BULL_MQ_PRO_NPM_TOKEN,uid=1000 export BULL_MQ_PRO_NPM

WORKDIR /usr/src/app/apps/api

RUN cp src/.example.env dist/src/.env
RUN cp src/.env.test dist/src/.env.test
RUN cp src/.env.development dist/src/.env.development
RUN cp src/.env.production dist/src/.env.production
RUN <<EOF
cp src/.env.production dist/src/.env.production
EOF

WORKDIR /usr/src/app

Expand All @@ -51,7 +47,9 @@ FROM dev_base AS prod

ARG PACKAGE_PATH

ENV CI=true
ENV CI=true \
NODE_ENV=production \
NEW_RELIC_NO_CONFIG_FILE=true

WORKDIR /usr/src/app

Expand All @@ -66,7 +64,5 @@ RUN --mount=type=cache,id=pnpm-store-api,target=/root/.pnpm-store\
--frozen-lockfile \
--unsafe-perm

ENV NEW_RELIC_NO_CONFIG_FILE=true

WORKDIR /usr/src/app/apps/api
CMD [ "pm2-runtime","start", "dist/main.js" ]
CMD [ "dump-init", "node", "dist/main.js" ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this one be dumb-init?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed some best practices and found out about https://github.com/Yelp/dumb-init. Maybe we need it, maybe not. We will review it as soon as this is more testable.

Note that my goal is to remove PM2. I don't think it adds value at this point.