From d79701ea2c8c8bbfbeab03d4aeab69ac6674d04f Mon Sep 17 00:00:00 2001 From: Isaac Date: Tue, 14 Feb 2023 23:22:52 +0000 Subject: [PATCH] perf(docker): decrease image size by 35% by using multi-stage builds --- Dockerfile | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6e676e8..46c8cf0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,32 +1,24 @@ # syntax=docker/dockerfile:1 -FROM node:18-alpine +FROM node:18-alpine AS build +WORKDIR /build +COPY package.json pnpm-lock.yaml ./ +COPY --link scripts scripts +# install python etc so node-gyp works for the optional dependencies +RUN apk add --no-cache make gcc g++ python3 +# install pnpm to make dependency installation faster (because it has a lockfile) +RUN npm install -g pnpm +# install dependencies, CI=true to skip pre/postinstall scripts +RUN CI=true pnpm install --prod --frozen-lockfile +COPY --link . . +FROM node:18-alpine AS final ENV NODE_ENV=production \ HTTP_HOST=0.0.0.0 \ HTTP_PORT=80 \ SETTINGS_HOST=127.0.0.1 \ SETTINGS_PORT=8169 - WORKDIR /usr/bot - -COPY package.json pnpm-lock.yaml ./ - -COPY --link scripts scripts - - # install python etc so node-gyp works for the optional dependencies -RUN apk add --no-cache --virtual .build-deps make gcc g++ python3 \ - # install pnpm to make dependency installation faster (because it has a lockfile) - && npm install -g pnpm \ - # install dependencies, CI=true to skip pre/postinstall scripts - && CI=true pnpm install --prod --frozen-lockfile \ - # pnpm isn't needed now - && npm uninstall pnpm --global \ - # remove node-gyp stuff - && apk del .build-deps - -COPY --link . . - +COPY --from=build /build ./ EXPOSE ${HTTP_PORT} - ENTRYPOINT [ "/bin/sh", "/usr/bot/scripts/start.sh" ] \ No newline at end of file