0
0
Node.jsframework~30 mins

Docker containerization for Node.js in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker containerization for Node.js
📖 Scenario: You are building a simple Node.js app that says hello. You want to package it inside a Docker container so it can run anywhere easily.
🎯 Goal: Create a Docker container for a Node.js app that listens on port 3000 and responds with 'Hello from Docker!'.
📋 What You'll Learn
Create a basic Node.js app with Express that listens on port 3000
Add a configuration variable for the port number
Write a Dockerfile to containerize the app
Run the app inside the Docker container exposing port 3000
💡 Why This Matters
🌍 Real World
Docker lets developers package apps with all dependencies so they run the same on any computer or server.
💼 Career
Knowing Docker containerization is essential for modern backend and full-stack developers to deploy and manage applications efficiently.
Progress0 / 4 steps
1
Create the Node.js app
Create a file called app.js with these exact lines: import express, create an app, and add a route / that sends the text 'Hello from Docker!'. Then make the app listen on port 3000.
Node.js
Need a hint?

Use import express from 'express' and app.get for the route.

2
Add a port configuration variable
Add a constant called PORT and set it to 3000. Change the app.listen call to use this PORT variable.
Node.js
Need a hint?

Define const PORT = 3000 and use app.listen(PORT).

3
Write the Dockerfile
Create a file called Dockerfile with these exact lines: start from node:18-alpine image, set working directory to /app, copy package*.json and app.js into the container, run npm install, expose port 3000, and run the app with node app.js.
Node.js
Need a hint?

Use FROM node:18-alpine and CMD ["node", "app.js"].

4
Run the Docker container
Write the exact Docker command to build an image named node-hello from the current directory, then write the command to run a container from node-hello exposing port 3000 on the host.
Node.js
Need a hint?

Use docker build -t node-hello . and docker run -p 3000:3000 node-hello.