Imagine you and your team have built an app. Now the testing team wants to test it.
Normally, they may need to install:
- Node.js
- npm packages
- dependencies
- environment setup
- configurations
Even after all that, errors can still happen because their system may be different from yours.
Docker solves this problem by packaging your app, runtime, dependencies, and configuration into one image. The tester only needs Docker installed. They can run your image directly and test the app.
Follow these steps to create your own Docker image:
Create the folder structure displayed in the image.
Create a server.js file and add the following code inside the file.
It is a demo code that displays the OS information when the user opens the URL in the browser.
const http = require("http");
const os = require("os");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(`
<html>
<body style="font-family:sans-serif;padding:40px;">
<h1>Running Inside Docker</h1>
<p><strong>Container Name:</strong> ${os.hostname()}</p>
<p><strong>OS:</strong> ${os.platform()}</p>
<p><strong>Architecture:</strong> ${os.arch()}</p>
<p><strong>Node Version:</strong> ${process.version}</p>
<p><strong>Current Time:</strong> ${new Date()}</p>
</body>
</html>
`);
});
server.listen(3000, () => {
console.log("Server running on port 3000");
Add the following information to the package.json file.
The package.json file contains information about the Node.js project, such as its name, version, main file, and dependencies.
{
"name": "docker-identity",
"version": "1.0.0",
"main": "server.js"
}
Add the following code to your dockerfile:
FROM defines the base image. Here, node:trixie already includes Linux, Node.js, npm, and required system libraries.
WORKDIR sets /app as the working directory inside the container.
COPY copies files from your local system into the container.
RUN executes a command while building the image. Here, it runs npm install.
CMD defines the command that runs when the container starts.
FROM node:trixie
COPY package.json /app/
COPY src /app/src
WORKDIR /app/
RUN npm install
CMD ["node", "src/server.js"]
Run the following command to build the Docker image.
- build: It is used to build the custom image.
- . : It is the current directory
- -t: It is the tag flag that is used to give name and version to the application
docker build -t node-app:1.0 .
After the build is successfully completed, run the following command to containerize your app.
- run: It is used to run the image as a container.
- -p: It is a flag that is used in port binding. Here, 3000 is the port of the container, whereas 8080 is the port of your system. So, we set 8080 as the port where you can publicly access the application.
- node-app: It is the name of the application.
docker run -p 8080:3000 node-app:1.0
After the container runs successfully, open localhost://8080 to see your app.
The following output is displayed:
Why This Is Useful
Now this image can run on any system that has Docker installed.
The tester does not need to install Node.js, npm, or project dependencies manually. Everything required to run the app is already packed inside the Docker image.
Even if the tester is using Windows, macOS, or Linux, the container will run the same way.


Top comments (0)