How to Dockerize a Node.js/Express Server & Run on Spheron Compute

Eshank TyagiEshank Tyagi
4 min read

In this blog post, we will explore how to Dockerize a Node.js/Express server and run it on Spheron Compute by Spheron Network. Dockerizing your application allows for easy deployment and scalability, while Spheron Compute provides a reliable and secure platform to run your Docker containers.

Prerequisites

Before we begin, make sure you have the following installed on your machine:

Dockerizing the Node.js/Express Server

  1. Create a new directory for your project and navigate into it:

     mkdir my-node-app
     cd my-node-app
    
  2. Initialize a new Node.js project:

     npm init -y
    
  3. Install Express and any other dependencies your server requires:

     npm install express
    
  4. Create a file named server.js and add the following code to set up a basic Express server:

     const express = require('express');
     const app = express();
    
     app.get('/', (req, res) => {
       res.send('Hello, world!');
     });
    
     app.listen(3000, () => {
       console.log('Server is running on port 3000');
     });
    
  5. Create a file named Dockerfile (without any file extension) in the root of your project and add the following code:

     # Use an official Node.js runtime as the base image
     FROM node:14
    
     # Set the working directory in the container
     WORKDIR /app
    
     # Copy package.json and package-lock.json to the working directory
     COPY package*.json ./
    
     # Install the server dependencies
     RUN npm install
    
     # Copy the rest of the application code
     COPY . .
    
     # Expose the server port
     EXPOSE 3000
    
     # Start the server
     CMD ["node", "server.js"]
    
  6. Build the Docker image by running the following command in the project's root directory:

     docker build -t my-node-app .
    
  7. Once the image is built, you can run the Docker container locally using the following command:

     docker run -p 3000:3000 my-node-app
    

    You should now be able to access your Node.js/Express server at http://localhost:3000.

Deploy and run on Spheron Compute

  1. Sign in to Spheron Compute by Spheron Network and create a new project.

  2. In the project dashboard, navigate to the "Deployments" section and click on "Create Deployment".

  3. Fill in the required details, such as the deployment name, container image (use the image name you specified during the Docker build step), and port mapping (e.g., 3000:3000).

  4. Click on "Create" to deploy your Node.js/Express server on Spheron Compute.

  5. Once the deployment is created, you can access your server using the provided endpoint.

Congratulations! You have successfully Dockerized your Node.js/Express server and deployed it on Spheron Compute.

(Optional) Running the Dockerized Node.js/Express Server on Spheron Compute using SSH
To run your Dockerized Node.js/Express server on Spheron Compute, follow these steps: <1> Sign up for an account on Spheron Network and create a new Project / Compute instance. <2> Connect to your Compute instance using SSH. <3> Build the Docker image on your Compute instance by cloning your project repository or copying the project files to the instance. <4> Once the files are on the Compute instance, navigate to the project's root directory and build the Docker image: docker build -t my-node-app . <5> Run the Docker container on the Compute instance: docker run -p 3000:3000 my-node-app <6> You should now be able to access your Node.js/Express server on Spheron Compute using the Compute instance's IP address or domain name.

That's it! Now you can enjoy the benefits of containerization and a reliable hosting platform for your application.

Example/Sample of the process

Let's Create a sample Dockerfile and Docker compose file that can be used to Dockerize a Node.js/Express server and deploy it on Spheron Compute.

Dockerfile:

# Use the official Node.js 14 image as the base image
FROM node:14

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the container
COPY . .

# Expose the port that the server will be listening on
EXPOSE 3000

# Start the server
CMD ["npm", "start"]

Docker Compose file (docker-compose.yml):

version: '3'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 3000:3000
    environment:
      - NODE_ENV=production
    restart: always

To deploy the Dockerized Node.js/Express server on Spheron Compute, follow these steps:

  1. Create a new directory for your project and navigate into it.

     mkdir my-node-app
     cd my-node-app
    
  2. Create a new file named Dockerfile and copy the above Dockerfile contents into it.

  3. Create a new file named docker-compose.yml and copy the above Docker Compose file contents into it.

  4. Create a new file named .env and add any environment variables your application requires. For example:

     NODE_ENV=production
    
  5. Place your Node.js/Express server code in the same directory as the Dockerfile and docker-compose.yml files.

  6. Open a terminal and navigate to the project directory.

  7. Build the Docker image and start the container using Docker Compose:

     docker-compose up -d
    

    This command will build the Docker image using the Dockerfile and start the container in detached mode.

  8. Your Node.js/Express server should now be running inside the Docker container. You can access it at http://localhost:3000 in your browser.

Note: Make sure you have Docker and Docker Compose installed on your machine before following these steps.

11
Subscribe to my newsletter

Read articles from Eshank Tyagi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Eshank Tyagi
Eshank Tyagi