Building and Deploying to Docker, Heroku, and Railway in NestJS

In modern web development, containerization and cloud deployment have become essential practices for ensuring that applications are portable, scalable, and easy to manage. In this module, we will cover how to build and deploy a NestJS application using Docker, Heroku, and Railway, three of the most popular platforms for deploying web applications.

By the end of this module, you will know how to:

  1. Set up Docker for local development and deployment.
  2. Deploy your application to Heroku for cloud hosting.
  3. Use Railway for a simpler, streamlined deployment process.

Table of Contents

  1. Introduction to Docker, Heroku, and Railway
  2. Setting Up Docker for NestJS
  3. Deploying NestJS App to Docker
  4. Deploying NestJS App to Heroku
  5. Deploying NestJS App to Railway
  6. Best Practices for Docker, Heroku, and Railway Deployment
  7. Conclusion

Introduction to Docker, Heroku, and Railway

Before we dive into the specifics of building and deploying, let’s understand the tools we will use:

  1. Docker: Docker is a platform that allows you to package applications into containers. Containers provide a lightweight, portable, and consistent environment for your application, ensuring it runs the same on all machines (locally, in the cloud, etc.).
  2. Heroku: Heroku is a cloud platform that simplifies the deployment of web applications. It provides a platform-as-a-service (PaaS) that handles much of the infrastructure setup, allowing developers to focus on writing code rather than worrying about servers.
  3. Railway: Railway is a cloud platform that automates the process of building, deploying, and managing applications. Railway provides a simple setup process with automatic deployments, making it an excellent choice for rapid development and deployment.

Setting Up Docker for NestJS

Dockerizing your NestJS application allows it to be easily deployed across different environments, whether locally or on cloud platforms like Heroku and Railway.

Step 1: Install Docker

First, ensure you have Docker installed on your machine. You can download it from Docker’s official website.

Step 2: Create a Dockerfile

In the root directory of your NestJS project, create a Dockerfile. This file will define the environment needed to run your application inside a container.

Here is a basic Dockerfile for a NestJS application:

dockerfileCopyEdit# Use official Node.js image as base
FROM node:16-alpine

# Set working directory in container
WORKDIR /usr/src/app

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

# Copy the rest of the application code
COPY . .

# Build the application
RUN npm run build

# Expose the port the app will run on
EXPOSE 3000

# Start the application
CMD ["npm", "run", "start:prod"]

Step 3: Create a .dockerignore File

To avoid copying unnecessary files into your Docker container, create a .dockerignore file in the root directory. This is similar to a .gitignore file but for Docker.

textCopyEditnode_modules
dist
Dockerfile
.dockerignore
.git
.gitignore

Step 4: Build the Docker Image

To build the Docker image for your application, run the following command in your project directory:

bashCopyEditdocker build -t nestjs-app .

This command will create an image named nestjs-app.


Deploying NestJS App to Docker

Now that your NestJS app is Dockerized, let’s deploy it locally using Docker.

Step 1: Run the Docker Container

Once the image is built, you can run the application in a Docker container with the following command:

bashCopyEditdocker run -p 3000:3000 nestjs-app

This will map port 3000 on your local machine to port 3000 in the Docker container. You should now be able to access your app by visiting http://localhost:3000.


Deploying NestJS App to Heroku

Heroku is a cloud platform that provides easy deployment options. It supports Docker-based applications, making it a suitable platform for deploying NestJS.

Step 1: Install Heroku CLI

If you don’t already have the Heroku CLI installed, download and install it from Heroku’s website.

Step 2: Create a Heroku App

To create a new app on Heroku, run:

bashCopyEditheroku create your-app-name

This will generate a unique URL for your application, such as https://your-app-name.herokuapp.com.

Step 3: Log In to Heroku

Log in to your Heroku account using the CLI:

bashCopyEditheroku login

Step 4: Push Docker Image to Heroku

Once logged in, you can deploy your Dockerized NestJS application by pushing it to Heroku using the following commands:

  1. Login to Heroku Container Registry: bashCopyEditheroku container:login
  2. Tag the Docker Image: bashCopyEditdocker tag nestjs-app registry.heroku.com/your-app-name/web
  3. Push the Docker Image to Heroku: bashCopyEditdocker push registry.heroku.com/your-app-name/web
  4. Release the App: bashCopyEditheroku container:release web --app your-app-name

Now, your NestJS app should be deployed and accessible on Heroku at the provided URL.


Deploying NestJS App to Railway

Railway simplifies deployments with a streamlined platform for hosting applications, databases, and other services.

Step 1: Install Railway CLI

You can install the Railway CLI by running:

bashCopyEditnpm install -g railway

Step 2: Create a Railway Project

To create a new project, run:

bashCopyEditrailway init

Follow the prompts to create a new project, and link it to your existing NestJS repository.

Step 3: Set Up Docker for Railway

Once your project is initialized, Railway automatically detects your Dockerfile and builds your Dockerized app.

Step 4: Deploy the App to Railway

To deploy your app to Railway, simply run:

bashCopyEditrailway up

Railway will automatically build and deploy your app. You can monitor the deployment status in the Railway dashboard.

Once deployed, your application will be available on a public URL provided by Railway.


Best Practices for Docker, Heroku, and Railway Deployment

  1. Use Multi-Stage Builds in Docker: If your application requires different environments for building and running, consider using multi-stage Docker builds to optimize the image size and reduce unnecessary dependencies. Example of multi-stage Dockerfile: dockerfileCopyEdit# Build Stage FROM node:16-alpine as builder WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . RUN npm run build # Production Stage FROM node:16-alpine WORKDIR /usr/src/app COPY --from=builder /usr/src/app/dist ./dist COPY --from=builder /usr/src/app/package*.json ./ RUN npm install --production EXPOSE 3000 CMD ["npm", "run", "start:prod"]
  2. Use Environment Variables: Both Heroku and Railway allow you to manage environment variables easily. Use environment variables for secrets like database credentials, API keys, etc., rather than hardcoding them in your application code.
  3. Automate Deployment: Integrate CI/CD pipelines for automated testing and deployment. Services like GitHub Actions can automate the process of building, testing, and deploying to Docker, Heroku, or Railway.

Conclusion

In this module, we covered how to build and deploy a NestJS application using Docker, Heroku, and Railway. These tools provide flexible deployment solutions, allowing you to containerize your application, host it in the cloud, and ensure easy scalability.

By following this module, you should now be able to deploy your NestJS applications with ease, ensuring smooth transitions from development to production environments while maintaining portability and scalability.