DEV Community

Cover image for How to Host Multiple Next.js Applications on a Single AWS ECR Instance Using Kamal 2.4
Derick Zihalirwa
Derick Zihalirwa

Posted on

How to Host Multiple Next.js Applications on a Single AWS ECR Instance Using Kamal 2.4

Introduction

Hey there! If you've already checked out my guide on deploying Next.js to AWS ECR with Kamal 2.4, you're in the right place for the next step. Haven't read it yet? No worries you might want to start there first.

Ready? Let's dive in!

Picture this scenario: your company manages several applications and services, each requiring staging and production environments.

Company Setup:

  • App 1: Marketing Site
    • Production Server
    • Staging Server
  • App 2: Service 1
    • Production Server
    • Staging Server
  • App 3: Service 2
    • Production Server
    • Staging Server

As you can see, maintaining multiple servers quickly adds up in cost. Let’s explore how to optimize this by consolidating your staging environments.

Why Are We Doing This?

Running separate servers for each staging app is like having a bunch of half-empty houses, it's expensive and wasteful. We could either put all our staging apps on one server (which is what we'll cover here) or use Kubernetes if we need something more complex. Let's keep it simple and focus on the single-server approach!


Why Consolidate Staging Environments?

Think of consolidating staging environments like having multiple apps share one house instead of giving each its own.

You'll save money and have an easier time managing everything in one place. Plus, you're making better use of your server resources instead of letting them sit partially empty.

Just keep in mind that apps will need to share computing power, and if the server has issues, all your staging apps will be affected. But with proper setup, it can be a smart way to streamline your infrastructure.


Key Considerations Before Deployment

Before moving your apps under one roof, do a quick health check. Make sure your server has enough space and memory to handle everyone comfortably, and try not to pack resource-hungry apps together, that's just asking for trouble!


Step-by-Step: Deploying Multiple Next.js Apps to One Server

We’ll use Docker and Kamal to containerize and deploy each app. Here’s how:

1. Dockerize Each Application

Ensure all applications have a Dockerfile in their root directory. This allows Kamal to build and deploy the containers.

2. Update Kamal Configurations

In each app’s repository, update the config/deploy.staging file to point to the same server IP, ensuring unique ports are assigned.

Example Configurations:

App 1: Marketing Site

service: app-1  
image: example/web-app  

servers:  
  web:  
    - 0.0.0.0  # Replace with your staging server IP  
proxy:  
  ssl: false  
  host: stagingmarketing.example.com  
  app_port: 3000  # Assign a unique port  
  healthcheck:  
    path: /  
    interval: 3  
    timeout: 30  
Enter fullscreen mode Exit fullscreen mode

App 2: Service 1

service: app-2  
image: example/web-app-2  

servers:  
  web:  
    - 0.0.0.0  # Same server IP  
proxy:  
  ssl: false  
  host: staging-service-1.example.com  
  app_port: 3001  # Increment the port  
  healthcheck:  
    path: /  
    interval: 3  
    timeout: 30  
Enter fullscreen mode Exit fullscreen mode

App 3: Service 2

service: app-3  
image: example/web-app-3  

servers:  
  web:  
    - 0.0.0.0  # Same server IP  
proxy:  
  ssl: false  
  host: staging-service-2.example.com  
  app_port: 3002  # Increment again  
  healthcheck:  
    path: /  
    interval: 3  
    timeout: 30  
Enter fullscreen mode Exit fullscreen mode

How It Works

Think of it like an apartment building: all your apps live at the same address (your server), but each gets its own unit number (port). So one app might be at port 3000, another at 3001, and so on. This makes it super easy to manage since everything's in one place, and you can still keep an eye on how each app is doing individually.


Wrapping Up

So there you have it! Putting all your Next.js staging apps on one AWS ECR server with Kamal 2.4 can save you money and make life easier. Just keep an eye on your server's capacity, and you should be good to go.

Top comments (0)