improve docker build caching

install dependencies before copying source into image
This commit is contained in:
Lukas 2022-02-25 18:53:14 +01:00 committed by GitHub
parent 3a9ce7615d
commit 3f3af9b957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 18 deletions

View File

@ -1,8 +1,10 @@
# protobuf build # protobuf build
FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder
WORKDIR /usr/src WORKDIR /usr/src
COPY messages/yarn.lock messages/package.json ./
RUN yarn install
COPY messages . COPY messages .
RUN yarn install && yarn proto RUN yarn proto
# typescript build # typescript build
FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder2 FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder2
@ -18,9 +20,9 @@ RUN yarn run tsc
FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d
WORKDIR /usr/src WORKDIR /usr/src
COPY back/yarn.lock back/package.json ./ COPY back/yarn.lock back/package.json ./
COPY --from=builder2 /usr/src/dist /usr/src/dist
ENV NODE_ENV=production ENV NODE_ENV=production
RUN yarn install --production RUN yarn install --production
COPY --from=builder2 /usr/src/dist /usr/src/dist
USER node USER node
CMD ["yarn", "run", "runprod"] CMD ["yarn", "run", "runprod"]

View File

@ -1,24 +1,25 @@
# protobuf build
FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder
WORKDIR /usr/src
WORKDIR /usr/src/messages COPY messages/yarn.lock messages/package.json ./
RUN yarn install
COPY messages . COPY messages .
RUN yarn install && yarn ts-proto RUN yarn ts-proto
WORKDIR /usr/src/front # typescript build
FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder2
WORKDIR /usr/src
COPY front/yarn.lock front/package.json ./
RUN yarn install
COPY front . COPY front .
COPY --from=builder /usr/src/ts-proto-generated/protos src/Messages/ts-proto-generated
# move messages to front
RUN cp -r ../messages/ts-proto-generated/protos/* src/Messages/ts-proto-generated
RUN sed -i 's/import { Observable } from "rxjs";/import type { Observable } from "rxjs";/g' src/Messages/ts-proto-generated/messages.ts RUN sed -i 's/import { Observable } from "rxjs";/import type { Observable } from "rxjs";/g' src/Messages/ts-proto-generated/messages.ts
RUN cp -r ../messages/JsonMessages/* src/Messages/JsonMessages COPY --from=builder /usr/src/JsonMessages src/Messages/JsonMessages
RUN yarn run typesafe-i18n && yarn run build-iframe-api && yarn build
RUN yarn install && yarn run typesafe-i18n && yarn run build-iframe-api && yarn build
# final production image
FROM thecodingmachine/nodejs:14-apache FROM thecodingmachine/nodejs:14-apache
COPY --from=builder --chown=docker:docker /usr/src/front/dist dist
COPY front/templater.sh .
USER root USER root
RUN DEBIAN_FRONTEND=noninteractive apt-get update \ RUN DEBIAN_FRONTEND=noninteractive apt-get update \
&& apt-get install -y \ && apt-get install -y \
@ -26,6 +27,9 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
USER docker USER docker
COPY --from=builder2 --chown=docker:docker /usr/src/dist dist
COPY front/templater.sh .
ENV STARTUP_COMMAND_0="./templater.sh" ENV STARTUP_COMMAND_0="./templater.sh"
ENV STARTUP_COMMAND_1="envsubst < dist/env-config.template.js > dist/env-config.js" ENV STARTUP_COMMAND_1="envsubst < dist/env-config.template.js > dist/env-config.js"
ENV APACHE_DOCUMENT_ROOT=dist/ ENV APACHE_DOCUMENT_ROOT=dist/

View File

@ -1,8 +1,10 @@
# protobuf build # protobuf build
FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder
WORKDIR /usr/src WORKDIR /usr/src
COPY messages/yarn.lock messages/package.json ./
RUN yarn install
COPY messages . COPY messages .
RUN yarn install && yarn proto RUN yarn proto
# typescript build # typescript build
FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder2 FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d as builder2
@ -19,9 +21,9 @@ RUN yarn run tsc
FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d FROM node:14.18.2-buster-slim@sha256:20bedf0c09de887379e59a41c04284974f5fb529cf0e13aab613473ce298da3d
WORKDIR /usr/src WORKDIR /usr/src
COPY pusher/yarn.lock pusher/package.json ./ COPY pusher/yarn.lock pusher/package.json ./
COPY --from=builder2 /usr/src/dist /usr/src/dist
ENV NODE_ENV=production ENV NODE_ENV=production
RUN yarn install --production RUN yarn install --production
COPY --from=builder2 /usr/src/dist /usr/src/dist
USER node USER node
CMD ["yarn", "run", "runprod"] CMD ["yarn", "run", "runprod"]

View File

@ -11,9 +11,9 @@ RUN yarn run tsc
FROM node:14.15.4-buster-slim@sha256:cbae886186467bbfd274b82a234a1cdfbbd31201c2a6ee63a6893eefcf3c6e76 FROM node:14.15.4-buster-slim@sha256:cbae886186467bbfd274b82a234a1cdfbbd31201c2a6ee63a6893eefcf3c6e76
WORKDIR /usr/src WORKDIR /usr/src
COPY uploader/yarn.lock uploader/package.json ./ COPY uploader/yarn.lock uploader/package.json ./
COPY --from=builder2 /usr/src/dist /usr/src/dist
ENV NODE_ENV=production ENV NODE_ENV=production
RUN yarn install --production RUN yarn install --production
COPY --from=builder2 /usr/src/dist /usr/src/dist
USER node USER node
CMD ["yarn", "run", "runprod"] CMD ["yarn", "run", "runprod"]