Creating a Node.js Application with Docker
What Will You Learn in This Guide?
In this guide, you will learn how to build a Node.js based application.
oaicite:0
You will learn how to turn it into a container.
The aim is to make the application portable and secure.
- Creating a Docker image for Node.js application
- Secure installation with multi-stage Dockerfile
- Sharing images via Docker Hub
Technical Summary
This guide is for a software running on Ubuntu.
oaicite:1
It explains how to move the (Express) application to Docker.
The problem is environment differences and manual installation confusion.
Steps followed:
- Preparing project files
- Writing Dockerfile
- Container creation
- Redeploy via Docker Hub
Prerequisites
-
Ubuntu 22.04+ a server
-
sudo authorized user
-
Docker installed
-
Node.js and npm installed -**
oaicite:2
**account
1. Step: Preparing the Project and Dependencies
Create the project directory:
mkdir node_project && cd node_project
- This command creates a workspace for the application.
Open the package.json file:
nano package.json
- This file defines project information and dependencies.
{
"name": "nodejs-image-demo",
"version": "1.0.0",
"description": "Node.js Docker demo",
"author": "GenixNode Dev Team <dev@ornek.com>",
"license": "MIT",
"main": "app.js",
"dependencies": {
"express": "^4.18.2"
}
}
- Install dependencies:
npm install
- This command downloads the Express library.
2. Step: Creating the Node.js Application
- Create the main application file:
nano app.js
- This file starts the Express server, which manages HTTP requests.
const express = require('express');
const app = express();
const port = 8080;
app.get('/', (req, res) => {
res.send('Docker üzerinde çalışan Node.js uygulaması');
});
app.listen(port, () => {
console.log(`Uygulama ${port} portunda çalışıyor`);
});
- Test the app:
node app.js
- Access from browser:
http://sunucu_ip:8080
3. Step: Writing Dockerfile (Multi-Stage)
- Create Dockerfile:
nano Dockerfile
- This file defines the container environment.
# Build aşaması
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
# Çalışma aşaması
FROM node:20-alpine
WORKDIR /app
COPY /app /app
EXPOSE 8080
CMD ["node", "app.js"]
- This structure:
1. Produces smaller image
2. Increases security
3. Suitable for production
4. Step: Creating .dockerignore File
- Exclude unnecessary files:
nano .dockerignore
node_modules
npm-debug.log
Dockerfile
.dockerignore
- This file shortens the build time.
5. Step: Creating and Running Docker Image
- Create the image:
docker build -t kullaniciadi/nodejs-demo .
- This command produces the Docker image.
Start container:
docker run -d -p 80:8080 --name nodejs-demo kullaniciadi/nodejs-demo
- Access the application:
http://sunucu_ip
6. Step: Redeploy via Docker Hub
- Log in to Docker Hub:
docker login
- Load the image:
docker push kullaniciadi/nodejs-demo
- Pull the image on another server:
docker pull kullaniciadi/nodejs-demo
- With this method you can quickly duplicate the application.
Frequently Asked Questions (FAQ)
1. Why is Docker preferred for Node.js? It eliminates environment differences and simplifies deployment.
2. Why is Alpine image recommended? It offers smaller size and lower security risk.
3. Is the code inside the container updated? No. The image must be recreated.
4. What does multi-stage build provide? Unnecessary files do not enter the production image.
Result
With this guide, you've made your Node.js application portable, secure and scalable. Thanks to Docker, you can run the same image on different servers without any problems.
🚀 You can go live this structure within minutes on the GenixNode infrastructure.

