Deploy Nest App

How to deploy a Nest App?

NOTE: Spheron Compute offers the flexibility to create custom configurations for your instance.

NestJS is a powerful, scalable, and extensible framework for building efficient and maintainable server-side applications in Node.js. It combines the robustness of TypeScript with the flexibility of JavaScript, providing a structured architectural approach inspired by Angular that encourages modular development and code reusability.

In this guide, you’ll configure a Nest app and deploy it to Spheron Compute using Docker.

Prerequisites

To successfully follow this guide, you will need the following:

Step 1: Create a Nest App

  1. Install NestJS CLI globally by running the following command in your terminal:

    npm install -g @nestjs/cli
  2. Create a new NestJS application by running the following command in your terminal:

    nest new nest-app
  3. Change into the project directory:

    cd nest-app
  4. Generate a controller using the NestJS CLI:

    nest generate controller cats
  5. Open cats.controller.ts and replace its content with the following code:

    import { Controller, Get, Post, Body } from '@nestjs/common';
     
     @Controller('cats')
     export class CatsController {
       private cats: string[] = [];
     
       @Get()
       getCats(): string[] {
         return this.cats;
       }
     
       @Post()
       createCat(@Body() cat: string): string[] {
         this.cats.push(cat);
         return this.cats;
       }
     }
  6. Generate a module using the NestJS CLI:

    nest generate module cats
  7. Open cats.module.ts generated after running the above command and replace its content with the following code:

    import { Module } from "@nestjs/common";
    import { CatsController } from "./cats.controller";
     
    @Module({
      controllers: [CatsController],
    })
    export class CatsModule {}
  8. Open app.module.ts and add CatsModule to the imports array:

    import { Module } from "@nestjs/common";
    import { CatsModule } from "./cats/cats.module";
     
    @Module({
      imports: [CatsModule],
    })
    export class AppModule {}
  9. Open main.ts and modify its content as follows:

    import { NestFactory } from "@nestjs/core";
    import { AppModule } from "./app.module";
     
    async function bootstrap() {
      const app = await NestFactory.create(AppModule);
      await app.listen(3000);
    }
    bootstrap();
  10. Save the changes and start the application by running the following command:

    npm run start
  11. Open your web browser and visit http://localhost:3000. To retrieve all cats, make a GET request to http://localhost:3000/cats. To create a new cat, make a POST request to http://localhost:3000/cats with a JSON payload containing the cat's name.

Check out the example here. (opens in a new tab)

Step 2: Create a Dockerfile

Here's what the dockerfile for this django app will look like:

# Use the official Node.js LTS (Long Term Support) image as the base image
FROM node:lts-alpine
 
# Set the working directory inside the container
WORKDIR /app
 
# Copy the package.json and package-lock.json files to the container
COPY package*.json ./
 
# Install the application dependencies
RUN npm install --production
 
# Copy the source code to the container
COPY . .
 
# Expose the port that the NestJS application will listen on
EXPOSE 3000
 
# Start the NestJS application
CMD [ "npm", "run", "start" ]

Step 3: Set default platform for docker build

Docker images built with Apple Silicon (or another ARM64 based architecture) can create issues when deploying the images to a Linux or Windows-based AMD64 environment. Before running the docker build command, run this command in your terminal:

export DOCKER_DEFAULT_PLATFORM=linux/amd64

Step 4: Build Docker image

To build the Docker image:

  1. Save the above Dockerfile in the root directory of your Nest app.
  2. Open a terminal and navigate to the root directory of your project where the Dockerfile is located.
  3. Run the following command to build the Docker image:
docker build -t nest_server .
  1. After the build process completes, you can run a container based on the image using the following command:
docker run -p 8000:8000 nest_server

Step 5: Push the app to DockerHub

To push an image, you first need to create a repository on Docker Hub.

Create a repo

To create a repository on Docker Hub:

  1. Sign up (opens in a new tab) or Sign in to Docker Hub (opens in a new tab).
  2. Select the Create Repository button.
  3. For the repo name, use nest_server. Make sure the Visibility is Public.
  4. Select the Create button.

Push the image

  1. Login to the Docker Hub using the command docker login -u YOUR-USER-NAME.
  2. Use the docker tag command to give the nest_server image a new name. Be sure to swap out YOUR-USER-NAME with your Docker ID.
docker tag nest_server YOUR-USER-NAME/nest_server
  1. Now try your push command again. If you’re copying the value from Docker Hub, you can drop the tagname portion, as you didn’t add a tag to the image name. If you don’t specify a tag, Docker will use a tag called latest.
docker push YOUR-USER-NAME/nest_server

Here's what a Docker Image will look like on Docker Hub:

Docker-Image

Check out this docker image here. (opens in a new tab)

Step 6: Run on Spheron Compute

To run your app on Spheron:

  1. Click "New Cluster" on the top right corner.
  2. Choose "Compute" to use CPU-based instances for running containers.
  3. Choose your desired Compute Type option under Compute Type.
  4. Select Import from Docker Hub.
  5. Enter the names for your cluster and docker image.
  6. Then, Add the tag and Click "Next."
  7. When selecting a region, we recommend starting by trying to deploy in a region closer to you. If you encounter any issues, you can consider switching to other regions. Choosing a region closer to you can improve performance and reduce latency. Click here to know more.
  8. Spheron will automatically select the recommended plan for the specific template. If you intend to move forward with the recommended plan, Create new Port Policy Mapping and just Click "Deploy" to initiate deployment.
  9. Select the instance plan that suits your needs. You can use the "Create Custom Plan" toggle to create custom plans for your CPU based instance.
  10. Configure Storage (SSD) plan for your instance. Use the "Add Persistent Storage" toggle to add persistent storage for your instance.
  11. Create new Port Policy Mapping. Add the container port, and Select the exposed port you want to map it to. Click here to know more.
  12. Add Environment Variable, if any.
  13. Add Secret Environment Variable if the value is a secret key. It will not be saved in the database. Click here to know more.
  14. You can add advanced configuration if required. Click here to know more.
  15. You can add health checkup if required. Click here to know more.
  16. Click "Deploy" to initiate deployment.

NOTE: Spheron supports only public docker images at the moment.

Verify Installation

The Nest App can be accessed only after the Compute Instance is provisioned. Thus, you need to wait for the installation to complete before you can start using the app. Your Apps can be verified for successful installation using the instructions below, while others may require different procedures.

Follow these instructions to verify the installation:

  • Attempt to access the app
    An App has an estimated deployment time of about 1-2 minutes. If you can successfully access it, the installation has been completed successfully. You can connect using the connection URL of the instance, which will also be provided after the instance is provisioned.
  • Check instance logs and events
    After successfully deploying your Nest App, it will produce logs and events, which you can check for any issues or errors.

Common Errors

Docker Fails When Building on Apple Silicon

🚫

ERROR: exec /usr/local/bin/docker-entrypoint.sh: exec format error

Docker images built with Apple Silicon (or another ARM64 based architecture) can create issues when deploying the images to a Linux or Windows-based AMD64 environment. Before running the docker build command, you must run this command in your terminal:

export DOCKER_DEFAULT_PLATFORM=linux/amd64
Deploy Express AppMarketplace App Guide