Skip to content

Instantly share code, notes, and snippets.

@misha-erm
Created January 4, 2021 12:10
Show Gist options
  • Save misha-erm/391dd8eb1b9bb914288e56c7a4fa89b4 to your computer and use it in GitHub Desktop.
Save misha-erm/391dd8eb1b9bb914288e56c7a4fa89b4 to your computer and use it in GitHub Desktop.
Dockerfile for node.js + typescript + yarn with layer caching
FROM node:14.15-alpine as base
WORKDIR /home/app
COPY package.json yarn.lock ./
# ---prepare dependencies---
FROM base as dependencies
# install production dependencies
RUN yarn install --production --frozen-lockfile
RUN mv node_modules prod_node_modules
# install all dependencies because they are required for compiling
RUN yarn install --frozen-lockfile
# ---build app---
# linting and tests can go there
FROM dependencies as build
COPY . .
# compile script can be a call to `tsc`
RUN NODE_ENV=production yarn compile
# ---prepare production-ready image---
FROM base as release
COPY --from=dependencies /home/app/prod_node_modules ./node_modules
COPY --from=build /home/app/dist ./dist
ENV NODE_ENV production
USER node
CMD ["node", "dist/server.js"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment