🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

The Blueprint Document

Learn how to author a Dockerfile to package custom applications into immutable Docker Images. Master the five essential instructions: FROM, WORKDIR, COPY, RUN, and CMD, and understand the critical difference between Build Time and Runtime.

Narrated Video Summary
data-composition-id="dockermasterclass-module2_1_dockerfilebasics"1280×720 @ 30fps6 clips3:00 total

The Blueprint Document

So far, you have only used pre-built Images from Docker Hub. But what happens when you write your own custom Node.js application? How do you package it into a Docker Image? You write a 'Dockerfile'. A Dockerfile is a simple, plain-text script that contains a sequential list of instructions. The Docker Engine reads this file from top to bottom, executing each command step-by-step to physically construct your immutable Image.

# 📄 Dockerfile

FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "server.js"]

FROM: The Foundation

Every single Dockerfile in the world MUST begin with the `FROM` instruction. Why? Because you rarely build an operating system from scratch. `FROM` defines the 'Base Image' you are starting with. If you type `FROM node:18`, you are telling Docker: 'Go to Docker Hub, download the official Node v18 image (which already includes Linux and Node.js), and use that as the foundation for my new custom image.'

# 🏗️ The Foundation

# 1. Start with an existing base
FROM node:18

# Or start with raw Ubuntu
# FROM ubuntu:22.04

COPY & WORKDIR

The Base Image doesn't have your code yet. You must move your source code from your laptop into the Image. First, use `WORKDIR /app` to create a dedicated directory inside the container and CD into it. Then, use the `COPY` instruction. The syntax is `COPY <host-path> <container-path>`. So, `COPY . .` means: 'Copy everything in my current laptop directory into the current container directory (/app).'

# 📁 Moving the Source Code

FROM node:18

# 1. Create and enter /app inside the image
WORKDIR /app

# 2. Copy laptop files (.) to /app (.)
COPY . .

RUN: Executing Commands

Your code is copied over, but it probably needs dependencies. In a Node.js project, you need to run `npm install`. The `RUN` instruction allows you to execute any standard terminal command *during the image building process*. If you write `RUN npm install`, Docker will execute that command inside the Image, download the packages, and permanently save the resulting `node_modules` folder directly into the Image.

# ⚙️ Installing Dependencies

FROM node:18
WORKDIR /app
COPY . .

# Execute during build time
RUN npm install

CMD: The Default Action

The Image is fully built. It has an OS, the source code, and dependencies. But what should it actually *do* when someone runs it? The `CMD` instruction defines the default command that executes when the Container starts. Unlike `RUN` (which happens during build time), `CMD` happens at runtime. If you write `CMD ["node", "server.js"]`, you are telling Docker: 'When someone spins up a container from this image, start the server.'

# 🚀 The Runtime Command

FROM node:18
WORKDIR /app
COPY . .
RUN npm install

# Executes ONLY when container starts
CMD ["node", "server.js"]

Syntax Mastered

You have decoded the syntax of the Dockerfile. You understand how `FROM` provides the foundation, `WORKDIR` and `COPY` handle the file system, `RUN` executes build-time configuration, and `CMD` dictates runtime behavior. However, simply writing these instructions in order is not enough. In the next lesson, we will uncover how Docker builds Images using 'Layers', and why the order of these instructions drastically affects performance.

/* Syntax Understood */
.curriculum { next: 'layer_optimization'; }
0:00 / 3:00
Scene 1 / 6 — The Blueprint Document
Total XP: 0|💻 dockermasterclass XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

The Blueprint Document

Production details.

Quick Quiz //

Why must every Dockerfile begin with a `FROM` instruction?


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Let's cut the fluff. Here is exactly what you need to know about this concept to survive in a real production environment.

1The Blueprint Document

Look, if you've ever dealt with this in production, you know exactly what the problem is. So far, you have only used pre-built Images from Docker Hub. But what happens when you write your own custom Node.js application? How do you package it into a Docker Image? You write a 'Dockerfile'. A Dockerfile is a simple, plain-text script that contains a sequential list of instructions. The Docker Engine reads this file from top to bottom, executing each command step-by-step to physically construct your immutable Image. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior engineers. When you deploy to a cluster, this is the mechanic that prevents catastrophic failure.

+
# 📄 Dockerfile

FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "server.js"]
localhost:3000
Terminal
$ Executing The Blueprint Document...
Status: OK
Success: Operation completed.

2FROM: The Foundation

Look, if you've ever dealt with this in production, you know exactly what the problem is. Every single Dockerfile in the world MUST begin with the FROM instruction. Why? Because you rarely build an operating system from scratch. FROM defines the 'Base Image' you are starting with. If you type FROM node:18, you are telling Docker: 'Go to Docker Hub, download the official Node v18 image (which already includes Linux and Node.js), and use that as the foundation for my new custom image.' This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior engineers. When you deploy to a cluster, this is the mechanic that prevents catastrophic failure.

+
# 🏗️ The Foundation

# 1. Start with an existing base
FROM node:18

# Or start with raw Ubuntu
# FROM ubuntu:22.04
localhost:3000
Terminal
$ Executing FROM: The Foundation...
Status: OK
Success: Operation completed.

3COPY & WORKDIR

Look, if you've ever dealt with this in production, you know exactly what the problem is. The Base Image doesn't have your code yet. You must move your source code from your laptop into the Image. First, use WORKDIR /app to create a dedicated directory inside the container and CD into it. Then, use the COPY instruction. The syntax is COPY <host-path> <container-path>. So, COPY . . means: 'Copy everything in my current laptop directory into the current container directory (/app).' This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior engineers. When you deploy to a cluster, this is the mechanic that prevents catastrophic failure.

+
# 📁 Moving the Source Code

FROM node:18

# 1. Create and enter /app inside the image
WORKDIR /app

# 2. Copy laptop files (.) to /app (.)
COPY . .
localhost:3000
Terminal
$ Executing COPY & WORKDIR...
Status: OK
Success: Operation completed.

4Step-by-Step Breakdown

The Blueprint Document. So far, you have only used pre-built Images from Docker Hub. But what happens when you write your own custom Node.js application? How do you package it into a Docker Image? You write a 'Dockerfile'. A Dockerfile is a simple, plain-text script that contains a sequential list of instructions. The Docker Engine reads this file from top to bottom, executing each command step-by-step to physically construct your immutable Image.

FROM: The Foundation. Every single Dockerfile in the world MUST begin with the FROM instruction. Why? Because you rarely build an operating system from scratch. FROM defines the 'Base Image' you are starting with. If you type FROM node:18, you are telling Docker: 'Go to Docker Hub, download the official Node v18 image (which already includes Linux and Node.js), and use that as the foundation for my new custom image.'

Why must every Dockerfile begin with a FROM instruction?

  • Because it specifies the 'Base Image' (like a pre-configured Linux OS with Node installed) that your custom code will be built on top of.
  • Because it authenticates you to Docker Hub.

COPY & WORKDIR. The Base Image doesn't have your code yet. You must move your source code from your laptop into the Image. First, use WORKDIR /app to create a dedicated directory inside the container and CD into it. Then, use the COPY instruction. The syntax is COPY <host-path> <container-path>. So, COPY . . means: 'Copy everything in my current laptop directory into the current container directory (/app).'

RUN: Executing Commands. Your code is copied over, but it probably needs dependencies. In a Node.js project, you need to run npm install. The RUN instruction allows you to execute any standard terminal command *during the image building process*. If you write RUN npm install, Docker will execute that command inside the Image, download the packages, and permanently save the resulting node_modules folder directly into the Image.

CMD: The Default Action. The Image is fully built. It has an OS, the source code, and dependencies. But what should it actually *do* when someone runs it? The CMD instruction defines the default command that executes when the Container starts. Unlike RUN (which happens during build time), CMD happens at runtime. If you write CMD ["node", "server.js"], you are telling Docker: 'When someone spins up a container from this image, start the server.'

What is the critical difference between the RUN instruction and the CMD instruction in a Dockerfile?

  • RUN executes commands during the Image creation (Build Time). CMD defines the default command that executes when the Container actually starts (Runtime).
  • RUN is for Linux servers. CMD is for Windows servers.

Syntax Mastered. You have decoded the syntax of the Dockerfile. You understand how FROM provides the foundation, WORKDIR and COPY handle the file system, RUN executes build-time configuration, and CMD dictates runtime behavior. However, simply writing these instructions in order is not enough. In the next lesson, we will uncover how Docker builds Images using 'Layers', and why the order of these instructions drastically affects performance.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Semantic Usage

Using the proper structure for The Blueprint Document ensures that screen readers can correctly interpret the content hierarchy and purpose.

<!-- Apply semantic elements appropriately -->

SEO Implications

  • 1

    Contextual Relevance

    Proper implementation of The Blueprint Document provides search engine crawlers with better context, improving the indexing accuracy of your page.

Best Practices

Clean Code

Always validate your structure when using The Blueprint Document to prevent layout shifts and DOM inconsistencies.

Separation of Concerns

Keep styling and behavior separate from the structural markup of The Blueprint Document.

Frequent Bugs

THE BUG

Unexpected layout shifts or styling failures.

THE FIX

Ensure all implementations related to The Blueprint Document are properly structured according to strict specifications.

Real-World Examples

Production Usage

Here is how The Blueprint Document is typically implemented in a professional, robust application.

<!-- Best practice implementation of The Blueprint Document -->
<div class="production-ready">
  <!-- Content -->
</div>

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Not reading error messages carefully

Uncaught TypeError: Cannot read properties of undefined (reading 'length') // Solution: Ensure the variable you are calling .length on is initialized as a string or an array, not undefined.

The Solution //

Most of the time, the compiler or interpreter tells you exactly what line caused the crash and why. Read stack traces from the top down to identify the root cause.

The Error //

Hardcoding sensitive credentials

// Wrong const API_KEY = 'sk-123456789'; // Correct const API_KEY = process.env.API_KEY;

The Solution //

Never hardcode API keys, passwords, or secrets in your source code. Use environment variables (.env files) to keep them secure and out of version control.

Lesson Glossary

[01]Dockerfile

A text document that contains all the commands a user could call on the command line to assemble a Docker Image.

Code Preview
The Recipe

[02]FROM

The instruction that sets the Base Image for subsequent instructions. Every valid Dockerfile must start with FROM.

Code Preview
The Foundation

[03]WORKDIR

The instruction that sets the working directory for any subsequent COPY, RUN, or CMD instructions.

Code Preview
The Context

[04]RUN

An instruction that executes a terminal command during the image creation phase (Build Time).

Code Preview
The Factory Robot

[05]CMD

The instruction that specifies the default command to execute when a Container is started from the Image (Runtime).

Code Preview
The Ignition Key

Continue Learning