AskHandle

AskHandle Blog

Installing Node.js in Docker: A Quick Guide

February 7, 2025Jessy Chan3 min read

Installing Node.js in Docker: A Quick Guide

Installing Node.js in a Docker container is a fantastic way to create a consistent development environment. Docker allows developers to package applications and their dependencies into a single unit, making it easier to deploy and run code. This article will guide you through the steps to install Node.js in Docker, allowing for a seamless setup process for your Node.js applications.

Prerequisites

Before starting, ensure that you have Docker installed on your system. You can download and install Docker Desktop if you are using Windows or macOS. For Linux users, Docker can be installed using package managers like apt, yum, or dnf, depending on your distribution.

Once Docker is installed, check if it's running by executing the command:

bash
1docker --version

You should see the Docker version information which confirms that it is properly installed.

Creating a Dockerfile for Node.js

A Dockerfile is a text document that contains all the commands to assemble an image. To set up your Node.js application in a Docker container, start by creating a new directory for your project.

bash
1mkdir my-node-app
2cd my-node-app

Inside this directory, create a file named Dockerfile. Open it in your favorite text editor and add the following content:

dockerfile
1# Use an official Node.js runtime as a parent image
2FROM node:14
3
4# Set the working directory in the container
5WORKDIR /usr/src/app
6
7# Copy package.json and package-lock.json to the working directory
8COPY package*.json ./
9
10# Install the app dependencies
11RUN npm install
12
13# Copy the rest of the application code
14COPY . .
15
16# Expose the application port
17EXPOSE 8080
18
19# Define the command to run the application
20CMD ["node", "app.js"]

Explanation of the Dockerfile

  1. Base Image: FROM node:14 specifies that the image will use Node.js version 14 as a base.

  2. Working Directory: WORKDIR /usr/src/app sets the working directory where all subsequent commands will run.

  3. Copying Files: The COPY package*.json ./ command copies your application's package.json and package-lock.json files which contain the dependency information.

  4. Installing Dependencies: The RUN npm install command installs the dependencies listed in your package.json.

  5. Copying Application Code: The COPY . . command copies the rest of your application files into the working directory.

  6. Exposing the Port: EXPOSE 8080 indicates the port on which your application listens for requests.

  7. Starting the Application: The command CMD ["node", "app.js"] defines how to run your application. Replace app.js with the entry point of your application.

Building the Docker Image

To build the Docker image, use the following command in the terminal:

bash
1docker build -t my-node-app .

This command builds the Docker image with the name my-node-app. The dot (.) indicates the current directory to use as the build context.

Running the Docker Container

Now that the image is built, you can run it with:

bash
1docker run -p 8080:8080 my-node-app

This command runs your application and maps port 8080 on your host machine to port 8080 on the container. You can access your Node.js application by visiting http://localhost:8080 in your web browser.

Common Practices

While developing with Docker, keep the following practices in mind:

  • Use .dockerignore to exclude files that should not be included in the Docker image, such as node_modules.
  • Regularly update your Node.js image to the latest stable version.
  • Use multi-stage builds for larger applications to keep your image sizes small.

Setting up Node.js in Docker can save time and effort in managing environments. With a Dockerfile, building and running your application becomes straightforward. This setup allows teams to work more efficiently, ensuring that everyone has the same development environment.