NestJS Kubernetes Deployment | Part 1: Containerization

hüseyin nurbaki
2 min readSep 25, 2021

--

Deploying NestJS applications on the Kubernetes Cluster series is divided into two main and two follow-up stories.
Part 1: Containerizing a NestJS application with Docker
Part 2: Kubernetes Deployment
Part 3: Bonus: Ingress Configuration
Part 4: Bonus: Kubernetes CronJob — NestJS

Follow official NestJS documents to create new app.

Getting Started 🍼

Create a Dockerfile at the root directory of your application with the following multi-stage build instructions

Build 🔨

Use the following command to build your NestJS Image.

$ docker build -t demo .

Running locally 🏃‍♂️🏠

Use the following command to create and start your container. When it is up and running, you will be able to access it over localhost.
If you face any trouble, check the Troubleshooting/Notes part at the end of the story.

$ docker run -d -p 80:3000 demo

Congratulations 🎉

Now your NestJS application is in a container.

Photo by Timo Volz on Unsplash

Troubleshooting/Notes 🔫

  • Default NestJS port is 3000. Update your Dockerfile and commands if you prefer some other port. See src/main.ts to validate your port number.
  • Check your application logs with the following command:
    $ docker logs — follow <CONTAINER_ID>
    Use $ docker container ls to obtain the container id.
  • Update the timezone of your container according to your needs. This Dockerfile uses Europe/Istanbul.
  • Update image name/tag according to your needs.
  • Change node alpine image if you need. This was slightly lighter than other node alpines. The size of the image of a NestJS application without additional modules created with this Dockerfile is less than 30MB.
  • Replace build commands If you prefer yarn.

Thank you for reading. Feel free to email for any questions or suggestions.
Cheers! 🎉

--

--