Multi-stage builds

Multi-stage builds is a feature that was added in Docker v17.05. It allows you to use multiple FROM instructions to define multiple images as stages inside a single Dockerfile.

You can extract artifacts from the previous stage and add them to the next stage in a single instruction (and thus a single layer).

In our case, we can define two stages one for building our application, and the second one that just copies the dist and node_modules directory and specifies the CMD instruction:

FROM node:8-alpine as builderUSER nodeWORKDIR /home/nodeCOPY --chown=node:node . .RUN ["yarn"]COPY --chown=node:node . .RUN ["yarn", "run", "build"]RUN find . ! -name dist ! -name node_modules -maxdepth 1 -mindepth 1 -exec rm -rf {} \;FROM ...

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.