Steps
Create a file called Dockerfile
on root project.
--------------------------------------Dockerfile--------------------------------------
FROM node:21
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3001
CMD ["npm", "run", "start:dev"]
--------------------------------------Dockerfile--------------------------------------
Create a file called .dockerignore
on root project
--------------------------------------.dockerignore-----------------------------------
node_modules
test
.gitignore
.git
.prettierrc
Dockerfile
README.md
.angular
.vscode
.editorconfig
--------------------------------------.dockerignore-----------------------------------
Now you can execute command docker build -t [name image]
This command will create an image with the name provided in the command.
Now to raise a container from the image that we just created, we must execute the command:
docker run -p 3001:3001 [name image]
Production
A Dockerfile is now created for production called: Dockerfile.prod
--------------------------------------Dockerfile.prod---------------------------------
FROM node:21
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
RUN rm -rf ./src
EXPOSE 3002
CMD ["npm", "run", "start:prod"]
--------------------------------------Dockerfile.prod---------------------------------
You can now run a command for the production Dokerfile
docker build -t [name image for production] -f Dockerfile.prod .
Now you can raise a container of the production image of the app
docker run -p 3002:3002 [name image for production]
Now you can create a file called: docker-compose.yml
--------------------------------------docker-compose.yml---------------------------------
services:
nestjs_api:
build:
context: .
dockerfile: Dockerfile
ports:
- "3001:3001"
environment:
- PORT=3001
- NODE_ENV=development
volumes:
- ./src:/app/src
mysql_db:
image: mysql
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=root_password
- MYSQL_DATABASE=name_db
- MYSQL_USER=userdb
- MYSQL_PASSWORD=password_db
- MYSQL_TCP_PORT=3307
--------------------------------------docker-compose.yml---------------------------------
Now you can execute the command
docker-compose up --build
On file tsconfig.json
you most add the following lines
,
"watchOptions":{
//Use a dynamic polling instead of system's native events for file changes
"watchFile": "dynamicPriorityPolling",
"watchDirectory": "dynamicPriorityPolling",
"excludeDirectories": ["**/node_modules", "dist"]
}
Now you can to create a file called: docker-compose.prod.yml
--------------------------------------docker-compose.prod.yml---------------------------------
services:
nestjs_api:
build:
context: .
dockerfile: Dockerfile.prod
ports:
- "3002:3002"
environment:
- PORT=3002
- NODE_ENV=production
volumes:
- ./src:/app/src
mysql_db:
image: mysql
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=root_password_123
- MYSQL_DATABASE=name_db
- MYSQL_USER=user_db
- MYSQL_PASSWORD=password_user_db
- MYSQL_TCP_PORT=3307
--------------------------------------docker-compose.prod.yml---------------------------------
The only thing we changed were the environment variables.
In main.ts
we must modify it to know what environment we are in:
--------------------------------------main.ts--------------------------------------
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const PORT = process.env.PORT || 3000;
await app.listen(PORT, () => {
console.log(`Running API in MODE ${process.env.NODE_ENV} on Port: ${PORT}`);
});
}
bootstrap();
--------------------------------------main.ts--------------------------------------
Now we can to execute the: docker-compose.prod.yml
docker-compose -f docker-compose.prod.yml up --build
Now we can to install mysql
y typeorm
npm i typeorm @nestjs/typeorm mysql2
On app.module.ts
we most to import mysql
but we can to cerate a module for that
--------------------------------------app.module.ts--------------------------------------
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'mysql_db',
port: 3307,
database: 'name_db',
entities: [],
username: 'user_db',
password: 'password_user_db',
synchronize: true,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
--------------------------------------app.module.ts--------------------------------------
Now we can to execute the command:
docker-compose up --build -d