How to create Dockerfile for NestJS app

By Mark HayesLast updated
How to create Dockerfile for NestJS app

If you're a fan of JavaScript, you'll love NestJs. NestJs is a NodeJs framework that emphasizes architecture. It modularizes the entire application into a service-oriented structure, making it easy to dockerize services.

Today, we will explore how to Dockerize a NestJs application for easy deployment. Docker simplifies server deployment.

At first, you have to create file named Dockerfile. This file is where the magic happens. Below is the code from the file:

FROM node:20  

# Create app directory
WORKDIR /app

# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json /app/

# Install app dependencies
RUN npm install

# Bundle app source
COPY . /app/

# Creates a "dist" folder with the production build
RUN npm run build  

# Expose the port on which the app will run
EXPOSE 3000

# Start the server using the production build
CMD ["npm", "run", "start"]

A couple of points to note:

  • We are using node:20 as the base image. This includes both the runtime and the Linux operating system, ensuring smooth operation.
  • If you are using Prisma for database connections, you need to add RUN npx prisma generate before RUN npm run build. It will generate necessary models.

The rest of the steps should be fairly straightforward.

Build the Docker Image:

Open a Terminal or PowerShell window, navigate to your project directory, and execute the following command to build the Docker image:

docker build -t your-app-nape .

This command will create the Docker image tagged your-app-name, using the current directory as the build context.

Run Docker image

When the image is created, you can start a Docker container based on that image with the following command:

docker run --rm -p 3005:3000 your-app-name

This will start a container from the image and map port 3005 of the container to port 3000 on your local machine. Now you should be able to access your NestJS API at http://localhost:3000. You can assign any port instead of 3005. But you must make sure that the selected port is not used by another process.

Node images rarely get under 100 MB because the runtime ships with them. Compiled languages sidestep this — a Go binary in a scratch image is often under 10 MB, which is why hosting for Go looks different from hosting for Node.

Keep reading

More posts tagged “Tutorials”.
Dockerfile basics for deploying your first app in 2026

You've built something. Now you need to ship it — fast, reliably, and without becoming a DevOps engineer overnight. That's exactly what a Dockerfile is for. This guide walks you through the core concepts, real syntax, and practical patterns you need to containerize and deploy your first application. No fluff, no unnecessary theory.

Sun, Apr 5, 2026
Deploy a Telegram Bot That Stays Up 24/7

How to host a Telegram bot so it survives restarts: long polling vs webhooks, a Dockerfile, Postgres for data, and what 24/7 actually costs.

Mon, Aug 31, 2026
Docker deployment with Compose: DIY VPS vs fixed-cost managed

A production-grade Docker Compose setup for a VPS, everything it still leaves you to run, and how the same stack maps onto a managed platform with a fixed monthly cost.

Mon, Nov 30, 2026
Update Docker image: docker compose commands and Granite.so

Pulling a new image doesn't update a running container. Here is how to update a Docker image correctly with docker compose, and how to automate it with tags.

Wed, Nov 25, 2026
Cloud deployment services: Docker apps without outages

Your Docker app runs fine locally. Here is how the main cloud deployment models fail in production, what to ask a provider before you commit, and how to move your compose stack over safely.

Mon, Nov 23, 2026
Containerized deployment: migrate applications to cloud in a weekend

Your app already runs in Docker. Here is a weekend-sized plan to move it, its database, and its files off a hand-managed server onto a predictable managed stack.

Wed, Nov 18, 2026
Be first in line for updates
and special pricing
Get early access to new features and exclusive discounts delivered straight to your inbox