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
How to create Dockerfile for Laravel application

The easiest way to dockerize your Laravel Project

Tue, Jul 16, 2024
Docker for Next.js application

Step-by-step guide how to Dockerize your Next.js app

Thu, Jul 18, 2024
Docker: Streamline App Development & Deployment

Discover how Docker simplifies app development and deployment. Learn to containerize, scale, and manage your applications efficiently with this powerful platform.

Wed, Aug 7, 2024
Microcontainers — Lightweight, Portable Docker Containers

Docker packages your application and its dependencies into a single image for use in a container, but these images often become unnecessarily large. For example, a simple Node.js application using an official image can swell to 644 MB, even though the application itself takes up less than 1 MB. Optimizing the image can significantly reduce this excess size. So what can we do to reduce the image size?

Wed, Aug 28, 2024
Build with Dockerfile: caching layers to speed up CI

This article covers how Docker layer caching actually works, how to structure your Dockerfile to maximize cache hits, and how to wire it all up in CI so the cache persists between builds.

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