NestJS is a powerful and versatile framework for building server-side applications in Node.js, offering a robust architecture with TypeScript at its core. One of the main challenges, however, is ensuring that your NestJS application can be deployed consistently across different environments. This is where Docker can make a significant impact.
Docker allows you to package your application along with all its dependencies into a single container, ensuring it runs seamlessly on any system that supports Docker. In this guide, we’ll walk through the steps necessary to containerize a NestJS application using Docker. By the end of this article, you’ll have a running Docker container of your NestJS application that you can deploy anywhere.
Step 1: Setting Up a Basic NestJS Application
First, ensure you have the NestJS CLI installed. If not, you can install it using npm:
npm install -g @nestjs/cli
Now, create a new NestJS application:
nest new my-nest-app
Navigate to the newly created project folder:
cd my-nest-app
You should see a basic NestJS application structure.
Step 2: Writing Your Dockerfile
A Dockerfile is a script that contains a sequence of instructions to create a Docker image. Create a new file named Dockerfile
in the root of your NestJS project and add the following content:
# Use the official Node.js 16 image as a base
FROM node:16
# Create a directory for the application
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install application dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Build the NestJS application
RUN npm run build
# Expose the application port
EXPOSE 3000
# Start the application
CMD ["npm", "run", "start:prod"]
This Dockerfile does the following:
- It starts by using the Node.js 16 image.
- It sets the working directory.
- It copies the
package.json
andpackage-lock.json
files and installs dependencies. - It copies the rest of the NestJS application code into the Docker container.
- It builds the NestJS application.
- It exposes port 3000, which is the default HTTP port used by NestJS.
- It starts the application in production mode.
Step 3: Creating a .dockerignore
File
To prevent unnecessary files from being included in the Docker image, create a .dockerignore
file in your project's root:
node_modules
dist
.git
.gitignore
Dockerfile
docker-compose.yml
npm-debug.log
This file ensures that Docker ignores specific files and directories when building the image, making the build faster and smaller.
Step 4: Building and Running the Docker Image
With the Dockerfile and .dockerignore
file in place, you can now build and run your Docker image.
Build the Docker Image
Run the following command to build your Docker image:
docker build -t my-nest-app .
Here, my-nest-app
is the name of the image. You can name it anything you like.
Run the Docker Container
Now that the image is built, you can run the Docker container:
docker run -p 3000:3000 my-nest-app
The -p 3000:3000
flag maps port 3000 on your host machine to port 3000 on the Docker container. Your application should now be accessible at http://localhost:3000
.
Step 5: Using Docker Compose (Optional)
Docker Compose can further simplify the management of your Docker containers, especially if you have multiple services. Create a docker-compose.yml
file in your project root:
version: '3.8'
services:
nest-app:
build: .
ports:
- "3000:3000"
environment:
NODE_ENV: production
command: npm run start:prod
With this setup, you can start your NestJS application with a single command:
docker-compose up
Conclusion
Containerizing your NestJS applications with Docker provides a consistent and reliable deployment process that can be easily shared across different environments. By following the steps outlined in this guide, you can ensure your application runs smoothly, from development to production, with the power and flexibility of Docker. Happy coding!