DEV Community

Bobby Orphé HOUESSINON
Bobby Orphé HOUESSINON

Posted on

3

Deploying a NestJS and Next.js Application on Dokku: Full-Stack Architecture

Introduction

Deploying a full-stack application that consists of a NestJS backend and a Next.js frontend on a Dokku server can be a powerful yet cost-effective solution. This article will guide you through the process of orchestrating both applications on a single Dokku instance.

Prerequisites

Before proceeding, ensure you have the following:

Step 1: Setting Up the Dokku Applications

On your server, create two Dokku applications:

Create applications

dokku apps:create my-nestjs-app
dokku apps:create my-nextjs-app
Enter fullscreen mode Exit fullscreen mode

Set up database and environment variables

# Example for database
dokku plugin:install https://github.com/dokku/dokku-postgres.git

dokku postgres:create mydatabase

dokku postgres:link mydatabase my-nestjs-app

# Example for environment variables
dokku config:set my-nestjs-app \
    APP_PORT=5000 \
    NGINX_ROOT=dist \
    JWT_SECRET=xxxxxxxxxxxxxxxxxx
Enter fullscreen mode Exit fullscreen mode

Step 2: Configuring NestJS for Deployment

Ensure your NestJS project has a Procfile on the root folder:

web: npm run prod
Enter fullscreen mode Exit fullscreen mode

Then, deploy your NestJS backend:

git remote add dokku dokku@your-server-ip:my-nestjs-app
git push dokku main
Enter fullscreen mode Exit fullscreen mode

Step 3: Configuring Next.js for Deployment

Next.js can be deployed similarly, but with custom environment variables:

dokku config:set my-nextjs-app \
    NEXT_PUBLIC_API_URL="https://api.yourdomain.com" \
    NGINX_ROOT=.next
Enter fullscreen mode Exit fullscreen mode

Ensure your Next.js project has a Procfile:

web: npm run prod
Enter fullscreen mode Exit fullscreen mode

Deploy with:

git remote add dokku dokku@your-server-ip:my-nextjs-app
git push dokku main
Enter fullscreen mode Exit fullscreen mode

Step 4: Configuring Nginx and Domains

If using domains, set them up with:

dokku domains:set my-nestjs-app api.yourdomain.com
dokku domains:set my-nextjs-app yourdomain.com
Enter fullscreen mode Exit fullscreen mode

Enable SSL with Let's Encrypt:

dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git
dokku letsencrypt my-nestjs-app
dokku letsencrypt my-nextjs-app
Enter fullscreen mode Exit fullscreen mode

Step 5: Testing the Deployment

Verify both applications are running:

dokku logs my-nestjs-app
dokku logs my-nextjs-app
Enter fullscreen mode Exit fullscreen mode

Visit https://yourdomain.com for the frontend and https://api.yourdomain.com for the backend.

Conclusion

By following this guide, you've successfully deployed a full-stack NestJS + Next.js application on a Dokku server. This setup allows for scalability, easy updates, and a structured separation between the frontend and backend.

Further Reading

Let me know in the comments if you have any questions or need clarifications!

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Image of DataStax

Langflow: Simplify AI Agent Building

Langflow is the easiest way to build and deploy AI-powered agents. Try it out for yourself and see why.

Get started for free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay