From b3ed938e050130d7e06c10beb99098ec8e28a45f Mon Sep 17 00:00:00 2001 From: Sergio Cinos Date: Tue, 8 Sep 2020 10:50:28 +0200 Subject: [PATCH] Build docker image using multi-stage builds and Alpine --- Dockerfile | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4a338a53db3e7..3015de962c7cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,11 @@ -FROM node:12.18.0 +FROM node:12.18.0 as builder LABEL maintainer="Automattic" -WORKDIR /calypso +WORKDIR /calypso -ENV CONTAINER 'docker' -ENV PROGRESS=true +ENV CONTAINER 'docker' +ENV PROGRESS=true # Build a "base" layer # @@ -17,26 +17,45 @@ ENV PROGRESS=true # env-config.sh # used by systems to overwrite some defaults # such as the apt and npm mirrors -COPY ./env-config.sh /tmp/env-config.sh -RUN bash /tmp/env-config.sh +COPY ./env-config.sh /tmp/env-config.sh +RUN bash /tmp/env-config.sh # Build a "source" layer # # This layer is populated with up-to-date files from # Calypso development. COPY . /calypso/ -RUN yarn install --frozen-lockfile && yarn cache clean +RUN yarn install --frozen-lockfile # Build the final layer # # This contains built environments of Calypso. It will # change any time any of the Calypso source-code changes. -ARG commit_sha="(unknown)" -ENV COMMIT_SHA $commit_sha - -ARG workers -RUN WORKERS=$workers CALYPSO_ENV=production BUILD_TRANSLATION_CHUNKS=true yarn run build && rm -fr .cache - -USER nobody -CMD NODE_ENV=production node build/bundle.js +ARG commit_sha="(unknown)" +ENV COMMIT_SHA $commit_sha + +ARG workers +RUN WORKERS=$workers CALYPSO_ENV=production BUILD_TRANSLATION_CHUNKS=true yarn run build + + +FROM node:12.18.0-alpine as app + +ARG commit_sha="(unknown)" +ENV COMMIT_SHA=$commit_sha +ENV NODE_ENV=production +WORKDIR /calypso +RUN apk add --no-cache tini +COPY --from=builder --chown=nobody:nobody /calypso/node_modules /calypso/node_modules/ +COPY --from=builder --chown=nobody:nobody /calypso/packages /calypso/packages/ +COPY --from=builder --chown=nobody:nobody /calypso/config /calypso/config/ +COPY --from=builder --chown=nobody:nobody /calypso/client/node_modules /calypso/client/node_modules/ +COPY --from=builder --chown=nobody:nobody /calypso/client/server/devdocs/search-index.js /calypso/client/server/devdocs/ +COPY --from=builder --chown=nobody:nobody /calypso/client/server/bundler/*.json /calypso/client/server/bundler/ +COPY --from=builder --chown=nobody:nobody /calypso/client/webpack.config.js /calypso/client/ +COPY --from=builder --chown=nobody:nobody /calypso/build /calypso/build/ +COPY --from=builder --chown=nobody:nobody /calypso/public /calypso/public/ + +USER nobody +ENTRYPOINT ["/sbin/tini", "--"] +CMD ["node", "build/bundle.js"]