How to build NestJS environment with CLI and Docker
overview
I will explain how to build an environment for NestJS, a Node.js-based web framework.
In this article, we will introduce how to set up the environment using the following method.
- How to build an environment using NestJS CLI
- How to build an environment using Docker
Here, I will explain how to build the NestJS environment using the following versions.
NestJS v10.1.7
nodejs v19.7.0
In addition, all the code created this time is posted on GitHub.
*** NestJS runs on Node.js. If Node.js is not installed, please download it from the official website and install it. ** node.js
How to build an environment using NestJS CLI
This method uses the NestJS CLI tool. Please follow the steps below.
To build an environment with NestJS alone, the following prerequisites are required
Install NestJS CLI
Install the NestJS CLI globally. Please run the following command:
npm install -g @nestjs/cli
This will install the NestJS CLI on your system.
Creating a project
Run the following command to create a NestJS project:
nest new nestjs-project
The above command will create a project directory named nestjs-project
. You can change the directory name to anything you like.
Change to project directory
Go to your project directory. Please run the following command:
cd nestjs-project
After moving, move to the root directory of the project.
Installing dependencies
In your project root directory, install the dependencies: Please run the following command:
npm install
This will add NestJS and related packages to your project. The final directory structure will look like this:
nestjs-project
├── node_modules
│ └── (dependency package)
├── src
│ ├── app.controller.spec.ts
│ ├── app.controller.ts
│ ├── app.module.ts
│ ├── app.service.ts
│ └── main.ts
├── test
│ ├── app.e2e-spec.ts
│ └── jest-e2e.json
├── .gitignore
├── nest-cli.json
├── package.json
├── README.md
├── tsconfig.build.json
└── tsconfig.json
Start the server
Start the NestJS server. Please run the following command:
npm run start
When the server starts successfully, you should see “Nest application successfully started” in the console. Visit http://localhost:3000 in your browser to make sure your NestJS application is working properly.
The NestJS CLI makes setting up your NestJS project easy. Just install the NestJS CLI, create a project using the nest new
command, install dependencies, and you can quickly start developing NestJS applications.
How to build an environment using Docker
First of all, install Docker. Download the Docker installer for your operating system from the official Docker website and install it.
Create project directory
First, create a new directory to create your NestJS project. Run the below command in command line Clone the official project directory from Git Hub.
git clone https://github.com/nestjs/typescript-starter.git nestjs-docker-project
Change to project directory
Go to your project directory. Please run the following command:
cd nestjs-docker-project
After moving, move to the root directory of the project.
Install Required Packages
Install NestJS and the packages needed to create Docker images.
npm install
Creating a Dockerfile
To build the NestJS application as a Docker image, create a file named Dockerfile
in your project folder and add the following content:
FROM node:19.7.0-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY..
EXPOSE 3000
CMD ["npm", "run", "start"]
Creating an application
Create a NestJS application. Create a new file (e.g. main.ts
) inside your project directory and add the following code
Docker listens on 0.0.0.0 to allow connections from all hosts.
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000, '0.0.0.0');
}
bootstrap();
Building a Docker image
Build the Docker image using the command below.
docker build -t nestjs-docker-project .
Running a Docker container
Run a Docker container from the built Docker image.
docker run -p 3000:3000 -d nestjs-docker-project
This will run the NestJS application inside a Docker container and map it to port 3000 on your host. You can access your NestJS application by visiting http://localhost:3000 from your browser, API test tool, etc.
The above is the procedure for building a NestJS application environment using Docker. By using Docker, you can reduce the work of building the environment and make the development process smoother.
Note: Access to port 3000 may be restricted by the host’s firewall or network settings. Adjust firewall and network settings as needed.