Skip to content

Docker - Setting Node.js

Node.js is a popular JavaScript runtime environment for building server-side applications. Dockerizing Node.js applications allows for consistent development, testing, and deployment environments.

We’ll demonstrate how to run a simple Node.js script using an official Node.js Docker image.

Step 1: Pull an official Node.js image. It’s good practice to use a specific version tag, often an Alpine-based image for smaller size.

# Pull Node.js version 18 based on Alpine Linux
docker pull node:18-alpine

Step 2: Create a simple Node.js script file named HelloWorld.js in your current directory.

HelloWorld.js
console.log('Hello World from Node.js inside Docker!');

Step 3: Run the script inside a Node.js container using docker run.

docker run --rm -it \
--name node-hello \
-v "${PWD}":/usr/src/app \
-w /usr/src/app \
node:18-alpine node HelloWorld.js

Explanation of the command:

  • docker run: Starts a new container.
  • --rm: Automatically removes the container when it exits (useful for one-off tasks).
  • -it: Runs interactively with a TTY (though not strictly necessary for this simple script, it’s common practice).
  • --name node-hello: Assigns a name to the container.
  • -v "${PWD}":/usr/src/app: Mounts the current host directory (${PWD} or %CD% on Windows Cmd) into the /usr/src/app directory inside the container. This makes HelloWorld.js accessible to the container.
  • -w /usr/src/app: Sets the working directory inside the container to /usr/src/app. This is where the node command will look for the script.
  • node:18-alpine: Specifies the Docker image to use.
  • node HelloWorld.js: The command to execute inside the container (run the node executable with HelloWorld.js as the argument).

Output:

Hello World from Node.js inside Docker!

This demonstrates running a script by mounting the host directory. For actual application development, you would typically build a custom image using a Dockerfile.

Example Dockerfile for a Node.js Application

Section titled “Example Dockerfile for a Node.js Application”

A typical Dockerfile for a Node.js web application might look like this:

# Use an official Node runtime as a parent image (choose a suitable version)
FROM node:18-alpine
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json (or yarn.lock)
# Copying these first leverages Docker cache if dependencies haven't changed
COPY package*.json ./
# Install app dependencies
RUN npm ci --only=production # Use 'npm install' if not using package-lock.json
# Bundle app source code inside the Docker image
COPY . .
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Define the command to run your app using CMD which defines your runtime
CMD [ "node", "server.js" ]

Key steps in this Dockerfile:

  • Start from a Node base image.
  • Set a working directory.
  • Copy package.json and package-lock.json.
  • Install only production dependencies using npm ci (faster and more reliable than npm install if package-lock.json exists).
  • Copy the rest of the application code.
  • Expose the port the application listens on.
  • Set the default command to start the application (node server.js).

You would build this image (docker build -t my-node-app .) and then run it (docker run -p 8080:8080 my-node-app). Remember to also use a .dockerignore file to exclude node_modules, .git, etc., from the build context.