Introduction to the
This article introduces how to use Docker to build NestJS and MongoDB development environment.
Full project address: github.com/LeoooY/nest…
To successfully run the code and understand how each part works, you need to have at least a basic understanding of the following:
JavaScript
&TypeScript
Docker
Basic use ofMongoDB
Basic use of
precondition
- Install Docker and docker-compose
- Install NodeJS
Begin to build
Main steps:
- create
NestJS
project - The container is changed
NestJS
project - use
Docker-Compose
Arrangement of the container - in
NestJS
Connect in projectMongoDB
service
createNestJS
project
Introduction | NestJS
$ npm i -g @nestjs/cli
$ nest new project-name
Copy the code
After running the nest new command, go to the root directory of the project-name project and run start:dev.
Open the browser and visit http://localhost:3000. If you can access it normally, it means that the NestJS project has been created.
The container is changedNestJS
project
In this step, we need the NestJS project we just created to be packaged into a Docker container.
- First of all in
NestJS
Create a new one for the project root directoryDockerfile
file
$ touch Dockerfile
Copy the code
- Modify the
Dockerfile
The file content
# Docker multi-phase build ### DEV environment ### FROM node:14.17.3 AS development # Package *.json./ RUN NPM install glob rimraf RUN NPM install --only=development COPY.. RUN NPM RUN build ### PROD environment ### FROM node:14.17.3 as production ARG NODE_ENV=production ENV NODE_ENV=${NODE_ENV} WORKDIR /usr/ SRC /app COPY package*.json ./ RUN \ npm config set registry https://registry.npm.taobao.org \ && npm install --only=production COPY . . COPY --from=development /usr/src/app/dist ./dist CMD ["node", "dist/main"]Copy the code
So a NestJS project Docker image is customized.
Because there are also MongoDB related container services, we do not directly use the Docker command to build and run, but use docker-compose to orchestrate the container
useDocker-Compose
Arrangement of the container
The docker-compose docker-compose. Yml configuration file defines a set of associated application containers as a project, which makes it easy to manage NestJS and MongoDB services.
- in
NestJS
Create a new one for the project root directorydocker-compose.yml
file
$ touch docker-compose.yml
Copy the code
- Modify the
docker-compose.yml
The file content
Version: '3.9' services: dev: container_name: server-dev image: server-dev:1.0.0 Build: Context:.target: development dockerfile: ./Dockerfile command: npm run start:debug ports: - 3000:3000 - 9229:9229 networks: - server-network volumes: - .:/usr/src/app - /usr/src/app/node_modules restart: unless-stopped environment: MONGO_URL: Mongodb prod: container_name: server-prod image: server-prod:1.0.0 Build: Context:. Target: Production Dockerfile: ./Dockerfile command: npm run start:prod ports: - 3000:3000 - 9229:9229 networks: - server-network volumes: :/usr/src/app - /usr/src/app/node_modules restart: unless-stopped mongodb: image: mongo:5.0.0 Container_name: server-mongodb environment: - MONGO_INITDB_ROOT_USERNAME=root - MONGO_INITDB_ROOT_PASSWORD=pass12345 volumes: - mongodb-data:/data/db networks: - server-network ports: - 27017:27017 healthcheck: test: echo 'db.runCommand("ping").ok' | mongo localhost:27017/test --quiet interval: 30s timeout: 10s retries: 3 restart: unless-stopped mongo-express: image: mongo-express container_name: server-mongo-express environment: - ME_CONFIG_MONGODB_SERVER=mongodb - ME_CONFIG_MONGODB_ENABLE_ADMIN=true - ME_CONFIG_MONGODB_ADMINUSERNAME=root - ME_CONFIG_MONGODB_ADMINPASSWORD=pass12345 - ME_CONFIG_BASICAUTH_USERNAME=admin - ME_CONFIG_BASICAUTH_PASSWORD=admin123 volumes: - mongodb-data depends_on: - mongodb networks: - server-network ports: - 8081:8081 healthcheck: test: wget --quiet --tries=3 --spider http://admin:admin123@localhost:8081 || exit 1 interval: 30s timeout: 10s retries: 3 restart: unless-stopped volumes: mongodb-data: name: mongodb-data networks: server-network:Copy the code
At this point, we’ve completed all the containerization steps; all that’s left is to connect to the MongoDB service in NestJS.
inNestJS
Connect in projectMongoDB
service
We used the @nestjs/ Mongoose tool recommended by NestJS to connect to the MongoDB service.
- The installation
@nestjs/mongoose
$ npm install --save @nestjs/mongoose mongoose
# or yarn
$ yarn add -D @nestjs/mongoose mongoose
Copy the code
- The connection
MongoDB
service
app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '@nestjs/mongoose';
const url = process.env.MONGO_URL || 'localhost';
@Module({
controllers: [AppController],
providers: [AppService],
imports: [
MongooseModule.forRoot(
`mongodb://${url}: 27017? serverSelectionTimeoutMS=2000&authSource=admin`,)]})export class AppModule {}
Copy the code
MONGO_URL is the address of the mongodb service defined in docker-compose. Yml. See the basic function of a docker container from another container.
Start the project
Now that we have all the configuration work done, we can get the project off the ground.
Start the NestJS service, Mongo service, and Mongo-Express service.
$ docker-compose up -d dev mongodb mongo-express
Copy the code
Note: When you install a new package using NPM, use the -v parameter to recreate the node_modules anonymous data volume of the container.
$ docker-compose up -d -V dev
Copy the code
View the status of each container
$ docker ps
Copy the code
View the logs of the container
$ docker logs server-dev
$ docker logs server-dev -f -f is used to continuously output logs as a parameter
Copy the code
Enter the container shell
$ docker exec -it server-mongodb bash Enter the mongo container
Copy the code
The resources
- Setting up a NestJS project with Docker for Back-End development
- Containerized development with NestJS and Docker
- How To Run MongoDB as a Docker Container
- Containerize Nest.js+MongoDB application in 5 minutes
- Accessing a docker container from another container
- From inside of a Docker container, how do I connect to the localhost of the machine?
- Docker goes from inception to practice
- Eggjs Dockerfile
@nestjs/mongoose
- Docker-compose: node_modules not present in a volume after npm install succeeds
- Top 4 Tactics To Keep Node.js Rockin’ in Docker