Skip to content
tezvyn:
Menu

Creating a Basic Node.js Dockerfile

Source: docs.docker.comEasyHow cards are made

A Dockerfile is a recipe for building a portable image of your app. Use it to ensure your Node.js app runs identically everywhere, from your laptop to production.

Why it exists

To solve the classic "it works on my machine" problem. Docker packages an application with all its dependencies into a standardized unit, a container, ensuring consistent behavior across development, testing, and production environments.

The mental model

A Dockerfile is a blueprint for an image. An image is a snapshot of a filesystem and a startup command. A container is a running instance of that image. The Dockerfile lists instructions: start with a base image (like one with Node.js pre-installed), set a working directory, copy your files, install dependencies, and define the command to run your app.

How it works

A basic Node.js Dockerfile executes a series of commands. First, FROM node:18-alpine specifies a lightweight base image with Node.js version 18. Second, WORKDIR /app sets the current directory inside the container. Third, COPY package*.json ./ copies your dependency manifests. Fourth, RUN npm install installs the dependencies inside the container. Fifth, COPY . . copies the rest of your application code. Sixth, EXPOSE 3000 documents the port your app listens on. Finally, CMD ["node", "server.js"] sets the default command to execute when the container starts.

When to use it

Use a Dockerfile for any Node.js project that needs to be deployed consistently across different environments, such as APIs, web servers, or background workers. It is the standard for building and shipping microservices and ensuring development parity among team members.

When not to use it

It's overkill for simple, local scripts that have no complex dependencies and are not intended for deployment. For a quick utility script on your own machine, running it directly with node script.js is simpler.

One canonical example

A well-structured Dockerfile optimizes for Docker's layer caching. By copying package.json and running npm install before copying the rest of your application code, you create a separate "dependency" layer. If you change your application code but not your dependencies, Docker reuses the existing dependency layer instead of re-running npm install on every build, making subsequent builds significantly faster. This separation is a critical best practice for efficient development workflows.

Interview question

What is the primary benefit of copying 'package.json' and running 'npm install' before copying the full application code in a Node.js Dockerfile?

  • a.It ensures that the application's source code is compiled before dependencies are installed.
  • b.It prevents conflicts between application files and node_modules during the build process.
  • c.This sequence guarantees that all development dependencies are removed from the final production image.
  • d.It allows Docker to reuse the dependency installation layer, speeding up subsequent builds if only application code changes.Correct
Why?

Copying package.json and running npm install first creates a separate Docker layer for dependencies. If only the application code changes, Docker can reuse the existing dependency layer, significantly speeding up subsequent build times. Option A is incorrect because Node.js applications are typically interpreted, not compiled, and the order is for caching, not compilation.

Just read this? Test yourself on what you have been reading.

Read the original → docs.docker.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on docker — each one lists the topics its interview covers.

See open roles