avatarGABRIEL OKOM

Summarize

Dockerize a Node.Js App — with Job Scenario/DevOps ticket

Creating a Dockerized Node.js App and Deploying it on a Remote Server

Prerequisites:

  1. Access to a remote server (App Server 3 in this example) with Docker installed.
  2. A Node.js application with a package.json file and a server.js file. These should be placed in the /node_app directory on the remote server.
  3. Basic knowledge of Docker and Node.js.

Objectives:

  1. Dockerize a Node.js app.
  2. Deploy the Docker container on a remote server.
  3. Expose the Node.js app on a specific port.
  4. Test the deployed app using a curl command.

Job Scenario:

There is a requirement to Dockerize a Node app and to deploy the same on App Server 3. Under /node_app directory on App Server 3, we have already placed a package.json file that describes the app dependencies and server.js file that defines a web app framework.

Create a Dockerfile (name is case sensitive) under /node_app directory:

Use any node image as the base image.

Install the dependencies using package.json file.

Use server.js in the CMD.

Expose port 5000.

The build image should be named as nautilus/node-web-app.

Now run a container named nodeapp_nautilus using this image.

Map the container port 5000 with the host port 8091.

. Once deployed, you can test the app using a curl command on App Server 3:

curl http://localhost:8096

Step-by-Step Guide:

Step 1: SSH into the Remote Server:

NOTE: this is a fictional server

ssh banner@172.16.238.12

Step 2: Navigate to the App Directory

cd /node_app

Step 3: Create a Dockerfile Create a Dockerfile with the following content (case sensitive), named Dockerfile:

vi Dockerfile
# Use an official Node.Js runtime as the base image
FROM node:14
# Set the working directory in the container
WORKDIR /app
# Copy the package.json and any other directory to the container
COPY package*.json ./
# Install the app dependencies
RUN npm install
# Copy the rest of the application code to the container using the "."
COPY . .
# Expose port 5001
EXPOSE 5000
# Define the command to run your Node.js application
CMD ["node", "server.js"]

Step 4: Build the Docker Image Build the Docker image with the name nautilus/node-web-app:

docker build -t nautilus/node-web-app .

During the image build, the package-lock.json is built.

Step 5: Run the Docker Container Run a container named nodeapp_nautilus using the image, and map the container's port 5001 to the host's port 8096:

docker run -d -p 8091:5000 --name nodeapp_nautilus nautilus/node-web-app

To view the container, run:

docker ps
nodeapp_nautilus

Step 6: Test the Deployed App Test the app using a curl command

curl http://localhost:8091

You should receive a response from your Node.js application.

nodeapp_nautilus response

Security Tips:

  • Ensure that you have the necessary permissions to run Docker commands and access the necessary directories on the remote server.
  • Use strong, complex passwords for your server and Docker configurations.
  • Avoid exposing sensitive information in your Node.js app, such as API keys or passwords, in your Docker image.

Conclusively; By following these steps, you’ve successfully Dockerized your Node.js app and deployed it as a container on a remote server. You can now access your app by sending requests to the specified port (8096 in this case).

Remember to follow security best practices to ensure the safety and integrity of your system and application.

Ref:

Kodekloud

#docker #dockerize #devops #container #orchestration #dockerimages #secops #GabrielOkom

DevOps
Docker
Kubernetes
Orchestration
Nodejs
Recommended from ReadMedium