Skip to content

Instantly share code, notes, and snippets.

@lopezjurip
Last active May 28, 2020 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lopezjurip/a3f370add3f9ed0b317db3439c4d3731 to your computer and use it in GitHub Desktop.
Save lopezjurip/a3f370add3f9ed0b317db3439c4d3731 to your computer and use it in GitHub Desktop.
A great Dockerfile for Node.js with Typescript and Yarn (multi-step build) and NPM Token for private modules
##
# First stage: prepare image
FROM node:12-alpine AS base
ARG NPM_TOKEN
ARG WORKDIR=/usr/src/app
WORKDIR ${WORKDIR}
# Install container build dependencies
RUN apk --no-cache add g++ ca-certificates lz4-dev musl-dev openssl-dev \
make python gcc zlib-dev libc-dev bsd-compat-headers py-setuptools git bash
# Install npm build dependencies
COPY package.json yarn.lock ./
RUN echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
RUN yarn install --frozen-lockfile
##
# Second stage: build app
FROM base AS build
ARG WORKDIR=/usr/src/app
WORKDIR ${WORKDIR}
# Build
COPY . ./
ENV NODE_ENV=production
RUN yarn build
##
# Third stage: run app
FROM base AS release
ARG WORKDIR=/usr/src/app
WORKDIR ${WORKDIR}
# Get "compiled" files
COPY --from=build ${WORKDIR} ./
EXPOSE 4000
ENV NODE_ENV=production
CMD [ "yarn", "start" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment