Docker for a Nest.js App with PostgreSQL and Nginx Reverse Proxy


Here, we will guide you through the process of setting up a Dockerfile​ for a Nest.js​ application that integrates PostgreSQL as its database and Nginx as a reverse proxy. Furthermore, we will demonstrate how to configure Nginx to use a personalized domain name, as

Let's kick things off by establishing our project structure. We'll assume that you have a Nest.js application already set up along with a docker-compose.yml​​ file to run our services.

Step 1: Set Up Project Structure:

Create a new directory for your Docker configuration files. Inside this directory, create a Dockerfile​ and a nginx.conf file​.

├── nestjs-app/
│   ├── src/
│   │   ├── main.ts
│   │   ├── app.module.ts
│   │   └── ... (other Nest.js application files)
│   └── ... (other Nest.js application directories)
├── docker-compose.yml
├── Dockerfile 
├── nginx.conf

Step 2: Dockerfile Configuration

Open the Dockerfile​ and add the below contents:

# Use Node.js base image
FROM node:18 AS builder

# Set working directory

# Install dependencies
COPY package*.json ./
RUN npm install

# Copy source code
COPY . .

# Build the application
RUN npm run build

# Production image
FROM node:18-alpine

# Set working directory
# Install production dependencies
COPY --from=builder /app/package*.json ./
RUN npm install --only=production

# Copy build files
COPY --from=builder /app/dist ./dist

# Expose port
# Start the application
CMD ["node", "dist/main"]

This Dockerfile​​ employs a multi-stage approach. It begins by installing dependencies and building the application in one stage, then copies over only the essential files to a smaller production image.

Step 3: Nginx Configuration

Next, let's configure Nginx to act as a reverse proxy. Open the nginx.conf​ file and add the following configuration:

server {
    listen 80;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

This Nginx configuration listens on port 80 for requests to and proxies them to our Nest.js application running on port 3000.

To configure SSL for your domain with nginx you can follow my SSL configuration blog here, or refer to my youtube video.

Step 4: Docker Compose Configuration

Finally, update your docker-compose.yml​ file to include both the Nest.js application and the Nginx service:


version: '3'
      context: .
      dockerfile: Dockerfile
      - '3000:3000'
      NODE_ENV: production
      - postgres
    image: nginx:latest
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      - '80:80'
      - app
    image: postgres:latest
      POSTGRES_DB: mydatabase
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword

Step 5: Build and Run the Docker Containers

Next, go to your project directory and execute the following command to build and launch the Docker containers:

docker-compose up --build

This script will compile your Nest.js application, generate Docker images for both the application and Nginx, and then launch the containers.

Hope you find this helpful!!!!
Docker for a Nest.js App with PostgreSQL and Nginx Reverse Proxy
Ram Krishna April 20, 2024
Share this post
Our blogs
Sign in to leave a comment
Unix commands for beginner IT professional.