DEV Community

Arthur
Arthur

Posted on

How to Dockerize a Node.js Application Step by Step

Hello, I'm Arthur! I enjoy exploring programming, server technologies, and practical tools that help developers build and deploy applications more efficiently.

Have you ever built an application that works perfectly on your computer but fails when you run it on another machine?

This is a common development problem. Different operating systems, package versions, and environment configurations can cause unexpected issues.

Docker helps solve this problem by packaging an application and its dependencies into a portable container.

In this tutorial, we'll learn how to Dockerize a simple Node.js application.

1. What Is Docker?

Docker is a platform that allows developers to package applications into containers.

A container includes the application and the dependencies it needs to run.

Some advantages include:

  • Consistent development environments
  • Easier application deployment
  • Better portability
  • Simplified dependency management
  • Easier scaling and testing

2. Create a Node.js Project

First, create a project directory:

mkdir node-docker-app
cd node-docker-app
npm init -y
Enter fullscreen mode Exit fullscreen mode

Install Express:

npm install express
Enter fullscreen mode Exit fullscreen mode

Create a file named app.js:

const express = require("express");

const app = express();
const PORT = 3000;

app.get("/", (req, res) => {
    res.json({
        message: "Hello from Docker!",
        status: "Application is running"
    });
});

app.listen(PORT, "0.0.0.0", () => {
    console.log(`Server running on port ${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

The application listens on port 3000 and returns a JSON response.

3. Create a Dockerfile

Now create a file named Dockerfile in the project directory.

FROM node:22-alpine

WORKDIR /app

COPY package*.json ./

RUN npm ci --omit=dev

COPY . .

EXPOSE 3000

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

Let's understand these instructions:

  • FROM selects the Node.js base image.
  • WORKDIR sets the working directory.
  • COPY transfers project files into the container.
  • RUN installs production dependencies.
  • EXPOSE documents the application port.
  • CMD starts the application.

4. Create a .dockerignore File

You should avoid copying unnecessary files into your Docker image.

Create .dockerignore:

node_modules
npm-debug.log
.git
.env
Enter fullscreen mode Exit fullscreen mode

This helps keep your Docker build cleaner and prevents local dependencies and sensitive environment files from being copied into the image.

5. Build the Docker Image

Run this command from your project directory:

docker build -t node-docker-app .
Enter fullscreen mode Exit fullscreen mode

Docker reads the Dockerfile and creates an image containing your application and its required dependencies.

6. Run the Container

Start the application:

docker run -d -p 3000:3000 --name my-node-app node-docker-app
Enter fullscreen mode Exit fullscreen mode

Open your browser:

http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

You should see a JSON response:

{
    "message": "Hello from Docker!",
    "status": "Application is running"
}
Enter fullscreen mode Exit fullscreen mode

To check running containers:

docker ps
Enter fullscreen mode Exit fullscreen mode

To view application logs:

docker logs my-node-app
Enter fullscreen mode Exit fullscreen mode

7. Stop and Remove the Container

When you finish testing, stop the container:

docker stop my-node-app
Enter fullscreen mode Exit fullscreen mode

Remove it:

docker rm my-node-app
Enter fullscreen mode Exit fullscreen mode

You can now rebuild and run the application whenever you update your code.

Deploying Docker Applications on a Server

Running containers locally is a good starting point, but production deployment requires additional planning.

You should consider:

  • Available CPU and RAM
  • Storage capacity
  • Network connectivity
  • Container security
  • Application monitoring
  • Automated backups

For developers planning to run multiple containers or resource-intensive applications, Seimaxim dedicated server solutions are worth exploring when comparing infrastructure options.

Final Thoughts

Docker makes application deployment more consistent by packaging code and dependencies into containers.

With a simple Node.js project, you can learn the basics of Docker images, containers, Dockerfiles, and application deployment.

Once you understand these concepts, you can move toward more advanced tools such as Docker Compose, container orchestration, and CI/CD pipelines.

For projects that require dedicated infrastructure and predictable server resources, Seimaxim server hosting is another option to consider.

The best way to learn Docker is to containerize a real project and experiment with different configurations.

Top comments (0)