Skip to content

Container Compendium

Here is your high-density field manual for the core Docker terminology and Node.js containerization workflow we covered in this lesson.


SyntaxPurposeExample
FROMDefines the base image foundationFROM node:20-alpine
WORKDIRSets the internal execution directoryWORKDIR /app
COPYCopies files from the host to the imageCOPY package*.json ./
RUNExecutes a command during the buildRUN npm install
EXPOSEDocuments the expected internal portEXPOSE 3000
CMDDefines the default runtime commandCMD ["npm", "start"]
docker buildConstructs the image from the recipedocker build -t app-name .
docker runStarts a container from an imagedocker run -p 8080:3000 app-name
-p flagMaps host port to container port-p 8080:3000

The required baseline pieces to package a standard Node.js application:

  • Dockerfile: The exact recipe used at the root of the project to build the image.
  • .dockerignore: Prevents local files (especially node_modules and .env) from bleeding into the image.
  • package.json: Must include a valid start script for the CMD instruction to trigger.
  • Docker Engine: Must be running locally on your machine to build or execute containers.

ProblemLikely CauseQuick Fix
Build is incredibly slowIncorrect instruction order invalidating cacheMove COPY package*.json ./ and RUN npm install before COPY . .
Strange Node module errorsHost node_modules were copied into the Linux imageAdd node_modules to your .dockerignore file
Container exits instantlyThe default CMD process failed or finished immediatelyCheck package.json for a missing or crashing start script
”Cannot connect” in browserTraffic isn’t crossing from the host into the containerVerify you used -p hostPort:containerPort (e.g. -p 3000:3000)
Build fails immediatelyDocker can’t find the build contextEnsure you included the trailing . in the docker build -t name . command

  • Images vs Containers: You build static images. You run live containers. An image is the class; a container is the object.
  • Documentation only: EXPOSE does absolutely nothing operational by itself. It’s a sticky note for other developers. You must manually map the ports when running.
  • Cache invalidation: A change to line X in a Dockerfile invalidates the cache for line X and every single line below it.
  • Local Dev vs Containers: Don’t rely on your laptop’s invisible configuration. Push your requirements into the Dockerfile so the app is self-sufficient.
Pack it Right

Consistency is the goal. When you ship the environment alongside the code, you strip away an entire category of stressful deployment errors.

Immutability Rule

If you ever find yourself “logging into” a running container to manually fix a configuration file, you have failed the test. Fix the Dockerfile and rebuild the image.


Docker Fundamentals Study Guide

Complete Node Aboard Demo GitHub Repo