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_appdirectory:- Use any
nodeimage as the base image. - Install the dependencies using
package.jsonfile. - Use
server.jsin theCMD. - Expose port
8084.
- Use any
The build image should be named as
nautilus/node-web-app.-
Now run a container named
nodeapp_nautilususing this image.- Map the container port
8084with the host port8093.
- Map the container port
. 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
Password for banner: BigGr33n
Inspect the Application Files
cd /node_app
ls -la
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
View the dependency manifest:
cat /node_app/package.json
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"
}
}
View the application entry point:
cat /node_app/server.js
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}`);
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
Add the following content:
FROM node:18-alpine
WORKDIR /node_app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 8084
CMD ["node", "server.js"]
Verify the file:
cat /node_app/Dockerfile
Output:
FROM node:18-alpine
WORKDIR /node_app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 8084
CMD ["node", "server.js"]
Build the Image
cd /node_app
docker build -t nautilus/node-web-app .
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
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
Output:
nautilus/node-web-app latest d413260175d7 3 seconds ago 135MB
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
Output:
c464eceb6897346d4afce4b5437afe0d21f178c172b5fcf580bc193a90d64457
The container nodeapp_nautilus started in detached mode.
Verify the Port Mapping
docker port nodeapp_nautilus
Output:
8084/tcp -> 0.0.0.0:8093
8084/tcp -> [::]:8093
Host port 8093 maps to container port 8084 on both IPv4 and IPv6.
Test the Application
curl http://localhost:8093
Output:
Welcome to xFusionCorp Industries!
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
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.jsonis copied andnpm installis run before copying the rest of the source for efficient layer caching -
server.jsis used as the entry point inCMD, and it listens on0.0.0.0:8084 - The container listens on port 8084 internally and is exposed on host port 8093
- The container name
nodeapp_nautilusand image namenautilus/node-web-appare 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_nautilusto diagnose missing dependencies or startup errors
Top comments (0)