Dockerize nestjs application with Postgres

WHAT TO KNOW - Sep 10 - - Dev Community

<!DOCTYPE html>



Dockerizing a NestJS Application with PostgreSQL

<br> body {<br> font-family: sans-serif;<br> line-height: 1.6;<br> margin: 0;<br> padding: 0;<br> }</p> <div class="highlight"><pre class="highlight plaintext"><code>h1, h2, h3 { margin-top: 1.5em; } code { background-color: #f0f0f0; padding: 2px 4px; border-radius: 3px; } pre { background-color: #f0f0f0; padding: 10px; border-radius: 5px; overflow-x: auto; } img { max-width: 100%; height: auto; } </code></pre></div> <p>



Dockerizing a NestJS Application with PostgreSQL



This article guides you through the process of Dockerizing a NestJS application using PostgreSQL as the database. Docker offers a robust environment for developing and deploying applications, ensuring consistency and portability across different environments. Combining NestJS's powerful framework with Docker's containerization capabilities simplifies development, deployment, and scalability.



Introduction



Let's break down why Docker and PostgreSQL are a powerful combination for your NestJS applications:



Docker for Development & Deployment



  • Consistent Environments:
    Docker eliminates environment discrepancies between development, testing, and production, ensuring your application behaves predictably across different stages.

  • Simplified Deployment:
    Docker containers make it easy to deploy your application to various platforms like cloud providers (AWS, GCP, Azure) or on-premise servers.

  • Isolation and Security:
    Containers isolate your application from the host system, offering improved security and preventing conflicts with other applications.

  • Resource Optimization:
    Docker allows efficient resource allocation by using lightweight containers that share the host operating system kernel. This leads to faster startup times and reduced resource consumption.


PostgreSQL for Data Storage



  • Robust and Reliable:
    PostgreSQL is a mature open-source database known for its reliability, ACID compliance, and extensive features.

  • Strong Data Integrity:
    PostgreSQL enforces data integrity through features like foreign key constraints, ensuring data consistency and reducing errors.

  • Scalability and Performance:
    PostgreSQL can handle large datasets and high traffic, making it suitable for applications with growing data needs.

  • Community Support:
    PostgreSQL benefits from a large and active community, providing ample resources, tutorials, and support.


By integrating Docker and PostgreSQL, you create a streamlined and robust platform for building and deploying your NestJS application.



Project Setup



Let's start by setting up the basic structure of our project. You can follow these steps to create a new NestJS project and initialize it with Docker:



  1. Create a New NestJS Project:

    npm init -y
    npm install @nestjs/cli -g
    nest new nestjs-postgres-docker
    cd nestjs-postgres-docker

  2. Install PostgreSQL and TypeORM Dependencies:

    npm install pg @nestjs/typeorm typeorm

  3. Initialize TypeORM:

    npx typeorm init

  4. Create a Dockerfile:
    Create a new file named
    Dockerfile
    in the root of your project.

  5. Create a docker-compose.yml file:
    Create a new file named
    docker-compose.yml
    in the root of your project.


Building the NestJS Application



We will now create a simple API with a single endpoint to demonstrate the integration of NestJS, PostgreSQL, and Docker.



Creating the Database Entity



Let's define a simple database entity named "Product" for our example. Open the

src/entities/product.entity.ts

file and add the following code:



import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Product {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ length: 255 })
name: string;

@Column()
price: number;
}



Creating the Service and Controller



Next, create the service and controller for managing product data.



// src/product/product.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Product } from '../entities/product.entity';

@Injectable()
export class ProductService {
constructor(
@InjectRepository(Product)
private productRepository: Repository,
) {}

async findAll(): Promise {
return await this.productRepository.find();
}

async create(createProductDto: CreateProductDto): Promise {
const product = this.productRepository.create(createProductDto);
return await this.productRepository.save(product);
}
}

// src/product/product.controller.ts
import { Controller, Get, Post, Body } from '@nestjs/common';
import { ProductService } from './product.service';
import { CreateProductDto } from './dto/create-product.dto';

@Controller('product')
export class ProductController {
constructor(private readonly productService: ProductService) {}

@Get()
async findAll() {
return await this.productService.findAll();
}

@post()
async create(@Body() createProductDto: CreateProductDto) {
return await this.productService.create(createProductDto);
}
}



Connecting to the Database



To connect to the PostgreSQL database, we need to configure the database credentials within the NestJS application. Open the

src/app.module.ts

file and modify it as follows:



import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProductModule } from './product/product.module';
import { Product } from './entities/product.entity';

@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: parseInt(process.env.POSTGRES_PORT, 10),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DATABASE,
entities: [Product],
synchronize: true, // This option is for development, don't use in production
}),
ProductModule,
],
controllers: [],
providers: [],
})
export class AppModule {}



The above code defines the database connection details using environment variables. These variables will be set later within the Docker Compose configuration.



Dockerizing the Application



Now, we will create the Docker configuration to run our NestJS application and the PostgreSQL database in Docker containers.



Dockerfile (for the NestJS Application)


Use the official Node.js image as the base

FROM node:18-alpine

Set the working directory

WORKDIR /app

Copy package.json and package-lock.json for installation

COPY package*.json ./

Install dependencies

RUN npm install

Copy the rest of the application code

COPY . .

Expose the application port

EXPOSE 3000

Start the application

CMD ["npm", "run", "start:dev"]



docker-compose.yml



version: '3.8'

services:
postgres:
image: postgres:14
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: nestjs_db
ports:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql/data

app:
build: .
ports:
- '3000:3000'
depends_on:
- postgres
environment:
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DATABASE: nestjs_db

volumes:
postgres_data:







Explanation:







  • postgres service:

    Defines the PostgreSQL database container. It uses the official PostgreSQL image (

    postgres:14

    ) and sets environment variables for the database username, password, and database name. It also exposes port 5432 and creates a named volume

    postgres_data

    to persist the database data.


  • app service:

    Defines the NestJS application container. It uses the

    Dockerfile

    built in the previous step, exposes port 3000, and depends on the

    postgres

    service. It also sets environment variables to connect to the PostgreSQL database.


  • volumes:

    The

    postgres_data

    volume ensures that the database data is persisted even if the container is stopped or restarted.





Running the Application





Now, with the Docker configuration in place, you can run your application using the following commands:





docker-compose up -d





This command will build the images, create the containers, and start them in the detached mode (



-d



). You can then access your application at



http://localhost:3000



.





To stop the containers and remove them, use:





docker-compose down






Conclusion





This comprehensive guide demonstrates how to effectively Dockerize a NestJS application with PostgreSQL. By leveraging Docker's containerization capabilities, you can streamline development, deployment, and scalability, while PostgreSQL provides a reliable and robust data storage solution. This approach enables you to build and deploy your application with consistency and confidence.





Key best practices to consider:





  • Environment Variables:

    Always use environment variables to manage sensitive information like database credentials, API keys, and configuration settings. This promotes separation of concerns and enhances security.


  • Multi-stage Builds:

    For production deployments, use multi-stage Docker builds to create smaller and more optimized images by separating the build process from the final production container.


  • Docker Hub:

    Push your container images to Docker Hub or a private registry for easier sharing and distribution.


  • Monitoring and Logging:

    Implement monitoring tools and logging strategies to track your application's performance and identify potential issues. This ensures visibility and helps with troubleshooting.


  • Security:

    Follow security best practices for Docker containers, including using secure base images, minimizing privileges, and implementing vulnerability scans.




By understanding these concepts and implementing best practices, you can effectively utilize Docker and PostgreSQL to build scalable and maintainable NestJS applications.




. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player