From dd21f9ba467e6055c354e205617b93a0cf00ca9d Mon Sep 17 00:00:00 2001 From: Otabek Ismoilov <107093598+ismoilovdevml@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:53:13 +0500 Subject: [PATCH] [OPTIMIZE] optimize docker builds --- Dockerfile | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index a4ae353..0a993c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,44 @@ -FROM node:16-alpine AS build + +FROM node:18.20.4-alpine as base + +ARG PORT=3000 + +EXPOSE ${PORT} WORKDIR /app COPY package*.json ./ -RUN npm install --legacy-peer-deps -COPY . . +FROM base as dependencies + +COPY package-lock.json /app/ + +RUN echo "Installing dependencies" \ + apk --update --no-cache add --virtual build-dependencies make g++ && \ + npm install --silent --production + +FROM dependencies as develop + +ENV NODE_ENV=development + +RUN npm install --silent + +COPY ./ /app + RUN npm run build -FROM node:16-alpine +FROM base as release -WORKDIR /app +ENV NODE_ENV=production + +COPY --from=dependencies /app/node_modules /app/node_modules +COPY --from=develop /app/build /app/build + +RUN adduser -D -h /app -u 5000 user && \ + chown -R user:user /app -COPY --from=build /app ./ +USER user -EXPOSE 3000 +ENTRYPOINT ["npm", "run"] -CMD ["npm", "start"] \ No newline at end of file +CMD ["start"]