This website uses cookies to ensure you get the best experience on our website.

Skip to content
LOGO
  • Company

    About IGNEK

    Learn about our story and our mission.

    Careers

    We're hiring!

    We always looking for talented peoples. Join our team!

    Blogs

    The latest industry news, updates and info.

    Case Studies

    Take a closer look at the projects we've delivered

    Webinar

    Watch our latest organized webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Custom Enterprise Portal Development for flawless Solutions

    Dedicated Development Team

    Expand Teams, Optimize Development with Our Expertise

    Back-end Development

    Empowering your digital presence with robust backend development expertise

    Front-end Development

    Crafting engaging user experiences through expert frontend development

    Digital Transformation

    Embrace digital transformation by modernizing process

    SaaS Development

    Transform your business with SaaS Innovative Solutions

    Technologies

    Liferay Services

    Development & Customization

    Theme

    Upgradation

    Migration

    Proof of Concept

    Performance Tunning

    Architecture

    Support & Maintenance

    E-commerce

    Expert Advice

    React Services

    Enterprise Development

    Custom Application

    Single Page Application

    Architecture

    API Integration

    Migration

    Consulting

    Maintenance & Support

    Spring Boot Services

    Web Service

    MVC Web Development

    API Integration

    Security

    Migration

    Microservices

    Support & Maintenance

    AEM Services

    Consulting Service

    Site Development

    Migration & Upgradation

    Integration

    Maintenance & Support

    Custom Solutions

    Content Management

    Team Augementation

  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Transforming industries through cutting edge SaaS solutions.

    Customer Portal : Boost Business Productivity

    Streamline operations and enhance productivity with our Customer Portal solution.

    Liferay Based Intranet Portal

    Internal portal for company communication, collaboration and resources

    Integration

    Matomo Integration with Liferay

    Integrate Matomo to enable user interactions seamlessly.

    Microsoft Teams integration with Liferay

    Integrate to enable seamless collaboration, messaging within your portal.

    Jira Integration With Liferay

    Optimizes business operations by integrating with Jira.

  • Hire Us

    Liferay

    Get expert Liferay developers for seamless portal solutions.

    React JS

    Get expert ReactJS developers for dynamic web solutions.

    Web Developer

    Get custom web solutions from IGNEK's expert developers.

    Spring Boot

    Get top-notch Spring Boot developers for your project success!

    Healthcare

    Get top Healthcare Developers for cutting-edge medical software.

    MERN Stack

    Get expert MERN developers from IGNEK for custom, innovative project solutions.

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • info@ignek.com
    • Ahmedabad, Gujarat, India – 382470
SCHEDULE CALL
  • COMPANY
    • About
    • Case Studies
    • Blogs
    • Career
    • Webinar
  • SERVICES
    • Enterprise Portal Development
    • Dedicated Development Team
    • Back-end Development
    • Front-end Development
    • Digital Transformation
    • SaaS Development
  • SOLUTION
    • Customer Portal:  Boost Business Productivity
    • SaaS : Software as a Service
    • Liferay Based Employee Intranet Portal
  • TECHNOLOGY
    • Liferay Services
      • Liferay Development and Customization
      • Liferay Theme Development Service
      • Liferay DXP 7.4 Upgrade
      • Liferay Migration
      • Liferay Proof Of Concept
      • Liferay Performance Tuning
      • Liferay Architecture Design Service
      • Liferay Support And Maintenance Service
      • Liferay Ecommerce Development
      • Liferay Expert Advice
    • ReactJS Services
      • ReactJS Enterprise Application Development
      • ReactJS Custom Application Development
      • ReactJS Single Page Application Development (SPA)
      • ReactJS Applications Architecture
      • ReactJS API Integration
      • ReactJS Migration
      • ReactJS Consulting
      • ReactJS Maintenance and Support
    • Spring Boot Services
      • Spring Boot Web Development Service
      • Spring MVC Web Development
      • Spring Boot API Integration Service
      • Spring Boot Security
      • Spring Boot Migration Service
      • Spring Boot Microservices
      • Spring Boot Support & Maintenance Service
    • AEM Development Services
      • AEM Consulting Services
      • AEM Site Development
      • AEM Migration & Upgradation
      • AEM Integration Services
      • AEM Maintenance & Support
      • AEM Content Management
      • Custom AEM Solutions
      • AEM Team Augmentation
  • INTEGRATION
  • HIRE US
    • Hire Liferay Developer
    • Hire ReactJS Developer
    • Hire Spring Boot Developer
    • Hire Healthcare Developer
    • Hire Web Developer
    • Hire MERN Stack Developer
  • CONTACT US
  • Company

    About IGNEK

    Careers

    We're hiring!

    Blogs

    Case Studies

    Webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Dedicated Development Team

    Back-end Development

    Front-end Development

    Digital Transformation

    SaaS Development

    Technologies

    Liferay Liferay Services
    • Development & Customization
    • Theme
    • Upgradation
    • Migration
    • Proof of Concept
    • Performance Tuning
    • Architecture
    • Support & Maintenance
    • E-commerce
    • Expert Advice
    React React Services
    • Enterprise Development
    • Custom Application
    • Single Page Application
    • Architecture
    • API Integration
    • Migration
    • Consulting
    • Maintenance & Support
    Spring Boot Spring Boot Services
    • Web Service
    • MVC Web Development
    • API Integration
    • Security
    • Migration
    • Microservices
    • Support & Maintenance
    AEM AEM Services
    • Consulting Service
    • Site Development
    • Migration & Upgradation
    • Integration
    • Maintenance & Support
    • Custom Solutions
    • Content Management
    • Team Augmentation
  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Customer Portal : Boost Business Productivity

    Liferay Based Intranet Portal

    Integration

    Matomo Integration with Liferay

    Microsoft Teams integration with Liferay

    Jira Integration With Liferay

  • Hire Us

    Liferay

    React JS

    Web Developer

    Spring Boot

    Healthcare

    MERN Stack

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • IGNEK
    • Ahmedabad, Gujarat, India – 382470
  • SCHEDULE CALL

How to Build and Deploy NestJS Applications with Docker

  • NodeJS
  • October 28, 2024

Share On :

Introduction

In modern development, containerization has become a standard practice for building, shipping, and running applications reliably across different environments. Docker, a popular containerization tool, simplifies the deployment process by packaging your application and its dependencies into a container that can run on any platform. When working with NestJS, a progressive Node.js framework, Docker ensures your application runs consistently, whether on your local machine or in the cloud.

This blog will guide you through building and deploying a NestJS application using Docker. We will cover everything from setting up a basic NestJS app to creating a Dockerfile, building a Docker image, and running it in a container.

Step 1 : Setting up a Basic NestJS Application

If you don’t already have a NestJS project, you can quickly scaffold a new one using the Nest CLI:

				
					npm i -g @nestjs/cli
nest new my-nestjs-app

				
			

This command generates a new project with the basic structure required for a NestJS application. You can navigate into the project directory to review the structure:

				
					cd my-nestjs-app

				
			

After running the command, you’ll have a working NestJS project. You can start the development server using:

				
					npm run start:dev

				
			

Once your application is up and running, let’s move on to Dockerize it.

Step 2 : Setting Up the Dockerfile

A Dockerfile is a script that contains a series of instructions to build a Docker image. In our case, we want to create an image of our NestJS application, so let’s create a Dockerfile at the root of your project.

Here’s a basic Dockerfile for a NestJS app:

				
					# Step 1: Use a Node.js base image
FROM node:18-alpine

# Step 2: Set the working directory inside the container
WORKDIR /app

# Step 3: Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Step 4: Install project dependencies
RUN npm install --only=production

# Step 5: Copy the rest of the application code to the working directory
COPY . .

# Step 6: Build the NestJS application
RUN npm run build

# Step 7: Expose the port the app will run on
EXPOSE 3000

# Step 8: Define the command to run the app
CMD ["npm", "run", "start:prod"]


				
			

Dockerfile Breakdown

  • Base image : We are using node:18-alpine, a lightweight Node.js image based on Alpine Linux, which reduces the size of the image.
  • Work directory : This specifies the directory in the container where your app’s code will reside (/app).
  • Dependency installation : We copy package.json and package-lock.json files first and then run npm install. This helps Docker cache this step, avoiding unnecessary installs during minor code changes.
  • Application code : We then copy the rest of the application files to the container.
  • Build the app : We run npm run build to compile the TypeScript files.
  • Expose port : This exposes port 3000, which is the default port for a NestJS application.
  • Start the app : We use the production command (npm run start:prod) to start the application inside the container.
Step 3 : Creating a Docker Ignore File

Before building the Docker image, it’s essential to exclude files that don’t need to be inside the Docker container, such as node_modules from the local environment, and build artifacts. To do this, create a .dockerignore file at the root of your project:

				
					touch .dockerignore

				
			

Add the following contents to it:

				
					node_modules
dist
Dockerfile
.dockerignore
.env


				
			

This ensures that unnecessary files are excluded from the Docker image.

Step 4 : Building the Docker Image

With the Dockerfile and .dockerignore set up, you’re ready to build the Docker image. Open a terminal in your project’s root directory and run:

				
					docker build -t my-nestjs-app .

				
			

This command tells Docker to build an image from the Dockerfile and tag it as

				
					my-nestjs-app.

				
			

You can verify that the image was built successfully by running:

				
					docker images

				
			

This will display a list of available Docker images, including my-nestjs-app.

Step 5 : Running the Docker Container

Once the Docker image is built, you can run it in a container using the following command:

				
					docker run -d -p 3000:3000 my-nestjs-app

				
			

This command runs the container in detached mode (-d) and maps port 3000 of the container to port 3000 on your machine (-p 3000:3000).

After running the command, you can access your NestJS application by visiting http://localhost:3000 in your browser.

To verify that the container is running, use:

				
					docker ps
				
			
Step 6 : Pushing the Image to Docker Hub

If you want to deploy your NestJS application to a remote server, you’ll need to push your Docker image to a container registry like Docker Hub. To do this, follow these steps:

  1. Log in to your Docker Hub account from the terminal :
    docker login
  2. Tag your Docker image with your Docker Hub username :
    docker tag my-nestjs-app <your-username>/my-nestjs-app
  3. Push the image to Docker Hub :
    docker push <your-username>/my-nestjs-app
Step 7 : Example of Deploying to ECS
  1. Create a Docker Image : Build your Docker image for the NestJS app.
    docker build -t my-nest-app .
  2. Create an ECS Cluster : Go to the AWS Management Console, navigate to ECS, and create a new cluster using the “EC2 Linux + Networking” template or the “Fargate” option if you want a serverless approach.
  3. Push Docker Image to Amazon ECR :
    • Create a repository on Amazon ECR (Elastic Container Registry).
    • Authenticate your Docker client with AWS:
      aws ecr get-login-password –region <your-region> | docker login –username AWS –password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com
    • Tag your Docker image:
      docker tag my-nest-app:latest <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/my-nest-app:latest
    • Push the image to ECR:
      docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/my-nest-app:latest
  4. Create a Task Definition : Define your ECS task, specifying the Docker image from ECR and necessary resource requirements.
    • Go to ECS > Task Definitions.
    • Click Create new Task Definition.
    • Choose Fargate or EC2 depending on your cluster type.
    • Add the container, setting the image to the ECR URL, and configure CPU and memory.
  5. Run a Service :
    • Go to ECS > Services and create a new service.
    • Select your cluster and task definition, and configure the desired number of tasks.
    • Choose the network settings (e.g., VPC, subnets, security groups).
  6. Expose your App : Set up a load balancer to expose your NestJS app to the internet.
    • Go to EC2 > Load Balancers and create an Application Load Balancer.
    • Add a listener for port 80 or 443.
    • Register your ECS service to the load balancer target group.
  7. Access the App : Once deployed, you can access your NestJS application via the load balancer’s DNS name.
Conclusion

Docker makes deploying NestJS applications efficient and scalable. With containerization, you can ensure that your application behaves consistently across different environments, whether it’s local development or a production server.

Explore Our Services

Discover how we can help your business thrive, whether you’re running a small startup, an SME, or a large enterprise. We’re here to understand your unique needs and goals, offering the expertise and resources to support your journey to success.

Stay informed about our services and updates by subscribing to our newsletter—just fill in the details below to subscribe.

Loading
Loading...

Related Blogs

October 18, 2024
Securing Your Node.js Apps With Helmet
Securing Your Node.js Apps With Helmet
October 18, 2024
Building Secure Payment Gateways With Node.js
Building Secure Payment Gateways With Node.js
October 18, 2024
Why Choose NestJS Over Plain Node.js for Enterprise-Level Applications
Why Choose NestJS Over Plain Node.js for Enterprise-Level Applications?
Loading...

Digital Solutions for Your Business with IGNEK

4.9

5.0

5.0

5.0

LOGO

Making the world a better place through constructing elegant hierarchies

COMPANY

  • About
  • Career
  • Case Study
  • Blogs

SERVICES

  • Enterprise Portal Development
  • Dedicated Development Team
  • Back-end Development
  • Front-end Development
  • Digital Transformation
  • SaaS Development

HIRE US

  • Liferay
  • Spring Boot
  • ReactJS
  • Healthcare
  • MERN Stack
  • AEM

CONTACT

  • info@ignek.com
  • info@ignek.com
SALES
  • sales@ignek.com
  • (+91) 635 157 6580
CAREER
  • hr@ignek.com
  • (+91) 932 849 5160
  • E 910-912, Ganesh Glory 11, Jagatpur Road, SG Highway, Ahmedabad, Gujarat - 382470

© 2018-2025 IGNEK, Inc. All rights reserved

Linkedin Facebook X-twitter Instagram