DEV Community

Janak Shrestha
Janak Shrestha

Posted on Edited on

Docker Node App

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.

  1. 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 8084.
  2. The build image should be named as nautilus/node-web-app.

  3. Now run a container named nodeapp_nautilus using this image.

    • Map the container port 8084 with the host port 8093.

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

curl http://localhost:8093


Connect to App Server 3

ssh banner@stapp03
sudo -i
Enter fullscreen mode Exit fullscreen mode

Password for banner: BigGr33n


Inspect the Application Files

cd /node_app
ls -la
Enter fullscreen mode Exit fullscreen mode

Output:

total 16
drwxr-xr-x 2 root root 4096 Oct 29 09:03 .
dr-xr-xr-x 1 root root 4096 Oct 29 09:05 ..
-rw-r--r-- 1 root root  332 Oct 29 09:03 package.json
-rw-r--r-- 1 root root  296 Oct 29 09:03 server.js
Enter fullscreen mode Exit fullscreen mode

View the dependency manifest:

cat /node_app/package.json
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "Sample Test <sample.test@example.com>",
  "main": "server.js",
  "keywords": [
    "nodejs",
    "bootstrap",
    "express"
  ],
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}
Enter fullscreen mode Exit fullscreen mode

View the application entry point:

cat /node_app/server.js
Enter fullscreen mode Exit fullscreen mode

Output:

'use strict';

const express = require('express');

// Constants
const PORT = 8084;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Welcome to xFusionCorp Industries!');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Enter fullscreen mode Exit fullscreen mode

The app uses Express, listens on port 8084 at 0.0.0.0, and responds with a welcome message on the root route.


Create the Dockerfile

vi /node_app/Dockerfile
Enter fullscreen mode Exit fullscreen mode

Add the following content:

FROM node:18-alpine

WORKDIR /node_app

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 8084

CMD ["node", "server.js"]
Enter fullscreen mode Exit fullscreen mode

Verify the file:

cat /node_app/Dockerfile
Enter fullscreen mode Exit fullscreen mode

Output:

FROM node:18-alpine

WORKDIR /node_app

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 8084

CMD ["node", "server.js"]
Enter fullscreen mode Exit fullscreen mode

Build the Image

cd /node_app
docker build -t nautilus/node-web-app .
Enter fullscreen mode Exit fullscreen mode

Output:

[+] Building 15.0s (10/10) FINISHED                                                  docker:default
 => [internal] load build definition from Dockerfile                                           0.1s
 => => transferring dockerfile: 167B                                                           0.0s
 => [internal] load metadata for docker.io/library/node:18-alpine                              1.3s
 => [internal] load .dockerignore                                                              0.1s
 => => transferring context: 2B                                                                0.0s
 => [1/5] FROM docker.io/library/node:18-alpine@sha256:8d6421d663b4c28fd3ebc498332f249011d118  3.2s
 => [internal] load build context                                                              0.1s
 => => transferring context: 878B                                                              0.0s
 => [2/5] WORKDIR /node_app                                                                    0.1s
 => [3/5] COPY package.json ./                                                                 0.3s
 => [4/5] RUN npm install                                                                      8.6s
 => [5/5] COPY . .                                                                             0.2s
 => exporting to image                                                                         0.9s
 => => exporting layers                                                                        0.8s
 => => writing image sha256:d413260175d707b6ac3e7f5b3d94bcb0edc8dab95c5243c93395f03c7f257268   0.0s
 => => naming to docker.io/nautilus/node-web-app                                               0.0s
Enter fullscreen mode Exit fullscreen mode

The image built successfully in five steps: base image, working directory, dependency manifest copy, dependency install, and source copy.


Verify the Image

docker images | grep nautilus
Enter fullscreen mode Exit fullscreen mode

Output:

nautilus/node-web-app   latest    d413260175d7   3 seconds ago   135MB
Enter fullscreen mode Exit fullscreen mode

The image nautilus/node-web-app:latest is available locally.


Run the Container

docker run -d --name nodeapp_nautilus -p 8093:8084 nautilus/node-web-app
Enter fullscreen mode Exit fullscreen mode

Output:

c464eceb6897346d4afce4b5437afe0d21f178c172b5fcf580bc193a90d64457
Enter fullscreen mode Exit fullscreen mode

The container nodeapp_nautilus started in detached mode.


Verify the Port Mapping

docker port nodeapp_nautilus
Enter fullscreen mode Exit fullscreen mode

Output:

8084/tcp -> 0.0.0.0:8093
8084/tcp -> [::]:8093
Enter fullscreen mode Exit fullscreen mode

Host port 8093 maps to container port 8084 on both IPv4 and IPv6.


Test the Application

curl http://localhost:8093
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to xFusionCorp Industries!
Enter fullscreen mode Exit fullscreen mode

The application responds correctly, confirming the deployment is working.


Complete Command Sequence

ssh banner@stapp03
sudo -i

cd /node_app
ls -la
cat package.json
cat server.js

vi /node_app/Dockerfile
# Add the Dockerfile content

cat /node_app/Dockerfile

docker build -t nautilus/node-web-app .
docker images | grep nautilus

docker run -d --name nodeapp_nautilus -p 8093:8084 nautilus/node-web-app
docker port nodeapp_nautilus

curl http://localhost:8093
Enter fullscreen mode Exit fullscreen mode

Task Summary

Requirement Status
Dockerfile created at /node_app/Dockerfile Completed
Base image node:18-alpine used Completed
Dependencies installed from package.json Completed
server.js used in CMD Completed
Port 8084 exposed Completed
Image built as nautilus/node-web-app Completed
Container named nodeapp_nautilus running Completed
Host port 8093 mapped to container port 8084 Completed
App accessible via curl http://localhost:8093 Completed

Dockerfile Explanation

Line Description
FROM node:18-alpine Base image with Node.js 18 on Alpine Linux
WORKDIR /node_app Set working directory inside the image
COPY package.json ./ Copy dependency manifest first for layer caching
RUN npm install Install dependencies (express) from package.json
COPY . . Copy remaining application files, including server.js
EXPOSE 8084 Document the port the app listens on
CMD ["node", "server.js"] Start the application using server.js

Key Commands Reference

Command Purpose
docker build -t nautilus/node-web-app . Build the image from the Dockerfile
docker run -d --name nodeapp_nautilus -p 8093:8084 nautilus/node-web-app Run the container with port mapping
docker images List available images
docker port nodeapp_nautilus View port mapping
docker logs nodeapp_nautilus View container logs
curl http://localhost:8093 Test the Node app

Notes

  • The package.json is copied and npm install is run before copying the rest of the source for efficient layer caching
  • server.js is used as the entry point in CMD, and it listens on 0.0.0.0:8084
  • The container listens on port 8084 internally and is exposed on host port 8093
  • The container name nodeapp_nautilus and image name nautilus/node-web-app are used exactly as required
  • The application responds with Welcome to xFusionCorp Industries! on the root route
  • If the container exits immediately, check logs with docker logs nodeapp_nautilus to diagnose missing dependencies or startup errors

Top comments (0)