Docker - Setting Node.js
Docker - Setting up Node.js
Section titled “Docker - Setting up 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.
Running a Simple Node.js Script
Section titled “Running a Simple Node.js Script”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 Linuxdocker pull node:18-alpineStep 2: Create a simple Node.js script file named HelloWorld.js in your current directory.
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.jsExplanation 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/appdirectory inside the container. This makesHelloWorld.jsaccessible to the container.-w /usr/src/app: Sets the working directory inside the container to/usr/src/app. This is where thenodecommand 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 thenodeexecutable withHelloWorld.jsas 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 containerWORKDIR /usr/src/app
# Copy package.json and package-lock.json (or yarn.lock)# Copying these first leverages Docker cache if dependencies haven't changedCOPY package*.json ./
# Install app dependenciesRUN npm ci --only=production # Use 'npm install' if not using package-lock.json
# Bundle app source code inside the Docker imageCOPY . .
# Make port 8080 available to the world outside this containerEXPOSE 8080
# Define the command to run your app using CMD which defines your runtimeCMD [ "node", "server.js" ]Key steps in this Dockerfile:
- Start from a Node base image.
- Set a working directory.
- Copy
package.jsonandpackage-lock.json. - Install only production dependencies using
npm ci(faster and more reliable thannpm installifpackage-lock.jsonexists). - 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.