Docker pipeline with Typescript + Express for production
Rubin

Rubin @rubiin

About: Self taught developer,FOSS lover, linux enthusiast

Location:
Kathmandu, Nepal
Joined:
Dec 17, 2017

Docker pipeline with Typescript + Express for production

Publish Date: Jun 8 '20
20 5

This post will guide you through how to setup docker for a express app on typescript. This also works for other frameworks besides express.
This post assumes you have used docker.
We will be using multi stage builds on docker as we have to
compile the express app before we can actually use it on production. For reference on multistage build, check it on here
https://docs.docker.com/develop/develop-images/multistage-build/

In a nutshell, as the name suggest multistage build is nothing more than a docker file split into several stages , each stage creating an intermediate image and passing the result to the next step and so on. The main difference using multi stage build vs using single build for compiling and running an app is the final image size.

## this is the stage one , also know as the build step

FROM node:12.17.0-alpine
WORKDIR /usr/src/app
COPY package*.json ./
COPY . .
RUN npm install
RUN npm run build

## this is stage two , where the app actually runs

FROM node:12.17.0-alpine

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY --from=0 /usr/src/app/dist ./dist
EXPOSE 3000
CMD npm start

Enter fullscreen mode Exit fullscreen mode

This is the dockerfile we will be using for building and running the app. Here we have defined two stages, one for compilation and another stage is for actually running the app.
The first stage code is nothing different from a typical nodejs dockerfile, the only difference is we are building or compiling the app with npm run build .Now emphasis on the second stage.
The important lines here are, we are only installing dependencies for production and also copying dist. The copy command here tells docker to copy the dist files from the build step on to the deployment stage.Then we are exposing the port like we usually do and atlast, running the npm command for starting the app.
The full project can be found at:
https://github.com/rubiin/dockerpipeline-with-typescript-express

Comments 5 total

  • Ivan Vilanculo
    Ivan VilanculoSep 28, 2020

    Hi, thanks for this useful post. The link to the full project you shared is broken.

  • Fernanda Vilela
    Fernanda VilelaNov 23, 2020

    Thank you! The simplest docker pipeline I found for ts.
    Could you upload your tsconfig and package.json, please?

  • Jephtah
    JephtahNov 23, 2020

    This post isn't really helpful without the package.json file. What are you running the build with? ts-node?

  • Dmitry Shvetsov
    Dmitry ShvetsovJan 30, 2021

    Why do you need to use the copy command twice in the first stage?

    COPY package*.json ./
    COPY . .
    
    Enter fullscreen mode Exit fullscreen mode
    • Dmitry Shvetsov
      Dmitry ShvetsovJan 30, 2021

      looks like you were trying to achieve cache usage. But then you need to swap npm Install and copy all other files to image. Like this:

      ## this is the stage one , also know as the build step
      
      FROM node:12.17.0-alpine
      WORKDIR /usr/src/app
      COPY package*.json ./
      
      # these two swapped
      RUN npm install
      COPY . .
      
      RUN npm run build
      
      Enter fullscreen mode Exit fullscreen mode
Add comment