Typescript Project Dockerfile

Typescript Project Dockerfile

There are tons of other examples of Dockerfile to build a Typescript project. A lot of them are not using the multi-stage build, this is a shame in my opinion.

Here are two examples using yarn or npm to make a production-ready, Typescript project docker image:

Using Yarn

Dockerfile
FROM node as builder

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json yarn.lock ./

RUN yarn install --frozen-lockfile

COPY . .

RUN yarn build

FROM node:slim

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json yarn.lock ./

RUN yarn install --production --frozen-lockfile

COPY --from=builder /usr/src/app/dist ./dist

EXPOSE 8080
CMD [ "node", "dist/index.js" ]

Using npm

Dockerfile
FROM node as builder

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./

RUN npm ci

COPY . .

RUN npm run build

FROM node:slim

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./

RUN npm ci --production

COPY --from=builder /usr/src/app/dist ./dist

EXPOSE 8080
CMD [ "node", "dist/index.js" ]

Subscribe to the newsletter

Get emails from me about web development and a lot of topics related to tech.