🐳 Day 4: CMD vs ENTRYPOINT, Docker Commands & Custom Dockerfile

Video Overview

This note outlines the 5 core concepts from the Day 4 Docker tutorial. It focuses heavily on the nuanced differences between CMD and ENTRYPOINT, managing custom Dockerfiles, and executing essential bulk CLI commands.

🔗 Source Video: Day 4: CMD vs ENTRYPOINT, Docker Commands & Custom Dockerfile | CKA Certification Course 2025


🏗️ Concept 1: Custom Dockerfile Names & The -f Flag

In real-world environments, you won’t always use the default Dockerfile name. You might have Dockerfile.dev or Dockerfile.prod.

  • To build from a custom name: You must use the -f (file) flag to explicitly tell the Docker daemon where the file is [00:03:51].
  • Example: docker build -f Dockerfile.dev -t my-app-image:v1 .

📦 Concept 2: Understanding the “Build Context” (.)

The period (.) at the end of your docker build command is highly misunderstood. It doesn’t just mean “build here”—it defines the Build Context [00:04:42].

  • What it does: It sends everything in that directory to the Docker daemon.
  • Best Practice Alert: Never run a docker build in a folder containing massive, unnecessary files (like a root directory or heavy .git folder). Keep your build context clean to speed up build times and reduce memory overhead [00:05:37].

⚔️ Concept 3: CMD vs. ENTRYPOINT (The Interview Classic)

Understanding the difference between these two instructions is a crucial CKA and Devops concept [00:14:40]:

  1. CMD (Default Command): Provides default arguments. If you supply a new command at the end of docker run, it completely overrides the CMD instruction [00:08:15].
    • Example: docker run <image> ls overrides CMD ["ping", "google.com"].
  2. ENTRYPOINT (Strict Executable): Treats the container like a dedicated application. Arguments passed at runtime do not replace it; they are appended to it [00:11:20].
    • Example: docker run <image> amazon.com appends to ENTRYPOINT ["ping", "-c", "4"].

🤝 Concept 4: The Ultimate Combo (CMD + ENTRYPOINT)

The best way to write a Dockerfile is to use both instructions simultaneously. You use ENTRYPOINT for the hardcoded application logic, and CMD for the flexible default parameters [00:16:39].

Perfect Combination Syntax

ENTRYPOINT ["ping", "-c", "4"]
CMD ["google.com"]
  • Run empty: docker run image_name → Pings google.com.
  • Run with arguments: docker run image_name 8.8.8.8 → Easily overrides the CMD and pings 8.8.8.8 instead!

🧹 Concept 5: Essential Lifecycle & Bulk Cleanup Commands

When managing Docker visually or through the CLI, you will quickly accumulate dead containers and dangling images. Use these commands to clean up [00:20:00]:

Inspection Commands

  • docker ps -a: Lists all containers (even stopped ones) [00:20:18].
  • docker inspect <container_id>: Returns detailed JSON metadata about the container [00:20:49].
  • docker top <container_name>: Shows active running processes inside the container [00:21:44].

Bulk Deletion Commands (Use with Caution!)

  • docker container prune: Safely removes all stopped containers (prompts for a y/n confirmation) [00:24:44].
  • docker rm -f $(docker ps -aq): The ultimate “nuke” command. It force-deletes every single container on your host system [00:25:49].
  • docker rmi -f $(docker images -q): Force-deletes all local images [00:26:46].