How to Dockerize a Node.js/Express Server & Run on Spheron Compute
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:
Docker: Install Docker
Node.js: Install Node.js
Dockerizing the Node.js/Express Server
Create a new directory for your project and navigate into it:
mkdir my-node-app cd my-node-app
Initialize a new Node.js project:
npm init -y
Install Express and any other dependencies your server requires:
npm install express
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'); });
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"]
Build the Docker image by running the following command in the project's root directory:
docker build -t my-node-app .
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
Sign in to Spheron Compute by Spheron Network and create a new project.
In the project dashboard, navigate to the "Deployments" section and click on "Create Deployment".
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
).Click on "Create" to deploy your Node.js/Express server on Spheron Compute.
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
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:
Create a new directory for your project and navigate into it.
mkdir my-node-app cd my-node-app
Create a new file named
Dockerfile
and copy the above Dockerfile contents into it.Create a new file named
docker-compose.yml
and copy the above Docker Compose file contents into it.Create a new file named
.env
and add any environment variables your application requires. For example:NODE_ENV=production
Place your Node.js/Express server code in the same directory as the Dockerfile and docker-compose.yml files.
Open a terminal and navigate to the project directory.
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.
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.
Subscribe to my newsletter
Read articles from Eshank Tyagi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by