📦 Docker Tutorial

Learn Docker from Scratch

This tutorial covers every Docker concept — from the basics of operating systems to running multi-container applications in production. No prior experience needed.

⚙️ Prerequisites

1. Prerequisites

Before we dive into Docker, let's make sure you understand some fundamental computing concepts. Even if you're a complete beginner, this section will get you up to speed.

What is an Operating System (OS)?

Think of your computer as a house. The hardware (CPU, RAM, disk) is the house's structure. The Operating System is like the building management — it controls who can use the electricity, water (resources), and ensures everything runs smoothly.

An OS is software that sits between your hardware and your applications. Examples: Linux macOS Windows

Hardware (CPU · RAM · Disk · Network) Operating System Kernel App 1 Browser App 2 Node.js App 3 Python App 4 Database
Fig 1.1 — Operating System sits between Hardware and Applications

What is a Process?

When you open a program (like Chrome or a Python script), the OS creates a process — a running instance of that program. Each process gets its own allocated memory and CPU time. If you open two Chrome windows, those are two processes.

What is a Network Port?

Imagine your computer is a large apartment building. Its IP address is the building's street address. A port is the apartment number — it tells incoming data which specific application to reach.

There are 65,535 possible ports. Common examples:

Port 80
HTTP web traffic
Port 443
HTTPS (secure) web traffic
Port 5432
PostgreSQL database
Port 3000
Node.js development servers (by convention)

What is a Server?

A server is simply a program (or machine) that provides services to other programs (called clients). When you visit google.com, your browser (client) sends a request; Google's server responds with the webpage. A server is always listening on a port for incoming requests.

What are Environment Variables?

Environment variables are key-value pairs stored in the OS environment that applications can read. They let you configure applications without changing their code — for example, setting a database password or API key.

  Terminal
# Setting an environment variable (Linux/macOS)
export DATABASE_URL="postgres://localhost:5432/mydb"
export PORT=3000

# Reading it in a program (Python example)
import os
db = os.getenv("DATABASE_URL")   # reads the value
💡 Why This Matters for Docker

Docker uses environment variables heavily to configure containers at runtime without rebuilding images. Understanding them now will make later sections much easier.

🧠 Quiz — Section 1: Prerequisites (5 Questions)
1What is the primary role of an Operating System?
  • A) A type of programming language used to build apps
  • B) Software that manages computer hardware and provides services for programs
  • C) A network protocol for communicating over the internet
  • D) A type of database management system
Show Answer & Explanation
✅ Correct Answer: B
The Operating System (OS) is the software layer that manages hardware resources (CPU, RAM, disk, network) and provides a platform for applications to run on. Programming languages, databases, and network protocols are separate concepts built on top of the OS.
2What is a "process" in computing?
  • A) A step in a project management plan
  • B) A physical component inside a CPU
  • C) A running instance of a program in memory
  • D) A type of computer network connection
Show Answer & Explanation
✅ Correct Answer: C
A process is a program that has been loaded into memory and is currently executing. The OS manages multiple processes simultaneously, giving each one allocated CPU time and memory. For example, opening a browser creates one or more processes.
3A network port is best described as:
  • A) A physical connector (like USB or HDMI) on a computer
  • B) A logical endpoint identified by a number that directs traffic to a specific application
  • C) A type of network cable used in data centers
  • D) A wireless networking standard like Wi-Fi
Show Answer & Explanation
✅ Correct Answer: B
Ports are logical (not physical) endpoints numbered from 0 to 65,535. They allow a single computer (with one IP address) to run many services simultaneously. For example, port 80 routes traffic to your web server while port 22 routes to SSH.
4In a client-server model, which of the following best describes a "server"?
  • A) A person employed at a restaurant
  • B) Only the physical machine in a data center
  • C) A program or system that listens for and responds to requests from clients
  • D) The web browser you use to visit websites
Show Answer & Explanation
✅ Correct Answer: C
A "server" in software is any program that provides services. It doesn't have to be a physical machine — your laptop running a local development web server IS a server. The key characteristic is: it listens on a port and responds to client requests.
5What is the main advantage of using environment variables to configure applications?
  • A) They make the application run faster by caching data
  • B) They allow configuring applications for different environments without changing code
  • C) They store images and binary files efficiently
  • D) They replace the need for a database
Show Answer & Explanation
✅ Correct Answer: B
Environment variables separate configuration from code. The same application binary can connect to a dev database in development and a production database in production — simply by changing the DATABASE_URL environment variable. This is a core 12-factor app principle that Docker leverages extensively.
↑ Back to top
🐳 Introduction

2. What is Docker?

Docker solves one of software's oldest headaches — "it works on my machine." Let's understand the problem and how Docker elegantly fixes it.

The Problem Docker Solves

Imagine you build a web app on your laptop. It uses Python 3.11, a specific version of a database driver, and a particular Linux library. Everything works perfectly.

Then you hand it to a colleague. Their machine has Python 3.9, a different library version — and nothing works. You've encountered the classic "works on my machine" problem.

👨‍💻 Developer Machine Python 3.11 + lib v2.5 macOS 14, 16GB RAM ✅ Works! 🖥️ Production Server Python 3.9 + lib v1.8 Ubuntu 20, 8GB RAM ❌ Crashes!
Fig 2.1 — The "works on my machine" problem caused by environment differences

Docker's Solution: The Shipping Container Analogy

Before the 1960s, shipping goods internationally was chaotic. Different ships had different-sized cargo holds, and loading/unloading was manual and slow. Then the standardized shipping container was invented — a uniform box that fits on any ship, truck, or train, regardless of what's inside.

Docker does the same thing for software. A Docker container packages your application and all its dependencies into a single, portable unit that runs identically everywhere.

🐳 Docker Container Your App Code Python 3.11 + Libs Config · Env Variables · Dependencies macOS Linux Server Windows Build once — run anywhere
Fig 2.2 — A Docker container packages everything needed; runs identically on any platform

Key Facts About Docker

First released
March 2013, by dotCloud (later renamed Docker, Inc.)
Written in
Go (Golang)
Foundation technology
Linux namespaces (isolation) and cgroups (resource control)
License
Apache License 2.0 (open source)

What Can You Do With Docker?

  • Package an application with all its dependencies
  • Run the same app consistently across dev, test, and prod environments
  • Spin up a database with one command (no installation needed)
  • Run multiple isolated services on a single machine
  • Scale applications horizontally with ease
  • Enable microservices architecture
🐳 Fun Fact

The Docker logo is a whale (named Moby Dock) carrying containers on its back — a perfect visual metaphor for the shipping container concept!

🧠 Quiz — Section 2: What is Docker? (8 Questions)
6What is the primary problem that Docker was designed to solve?
  • A) Making computers run faster by optimizing the CPU
  • B) Inconsistent environments causing software to behave differently across machines
  • C) Creating new programming languages for cloud development
  • D) Managing database schemas across teams
Show Answer & Explanation
✅ Correct Answer: B
Docker's core value proposition is environment consistency. By packaging an application with all its dependencies into a container, the "works on my machine" problem is eliminated. The same container runs identically in development, CI/CD pipelines, and production.
7Docker containers are built on which underlying Linux kernel features?
  • A) Windows Registry and Active Directory
  • B) Linux namespaces and cgroups
  • C) macOS Spotlight and Keychain
  • D) BIOS/UEFI firmware settings
Show Answer & Explanation
✅ Correct Answer: B
Docker uses namespaces to isolate each container's view of the system (PID, network, filesystem, etc.) and cgroups (control groups) to limit and monitor resource usage (CPU, memory). These are Linux kernel features that Docker essentially wraps with a user-friendly interface.
8In what year was Docker first publicly released?
  • A) 2008
  • B) 2011
  • C) 2013
  • D) 2016
Show Answer & Explanation
✅ Correct Answer: C
Docker was released in March 2013 by Solomon Hykes at PyCon. It was originally a side project of the PaaS company dotCloud. Its debut was a landmark moment — the live demo went viral in the developer community almost overnight.
9Which real-world analogy best describes how Docker containers work?
  • A) Postal envelopes — different sizes for different contents
  • B) Standardized shipping containers — uniform box that fits on any transport
  • C) Filing cabinets — store documents in ordered drawers
  • D) Telephone lines — dedicated point-to-point connections
Show Answer & Explanation
✅ Correct Answer: B
Just like standardized shipping containers revolutionized global trade by providing a uniform unit that fits any ship, truck, or train, Docker containers provide a uniform unit for software that runs on any machine with Docker installed — regardless of the underlying OS or configuration.
10The phrase "works on my machine" refers to which software development problem?
  • A) A software license that only allows use on one machine
  • B) A situation where software runs correctly in development but fails in other environments
  • C) A cloud computing pricing model
  • D) A type of software bug that only appears once
Show Answer & Explanation
✅ Correct Answer: B
This classic problem occurs because different machines have different OS versions, library versions, environment configurations, etc. Code that works in a developer's environment may fail in staging or production due to these differences. Docker eliminates this by ensuring every environment uses the exact same container.
11Docker is primarily written in which programming language?
  • A) Python
  • B) Java
  • C) Go (Golang)
  • D) C++
Show Answer & Explanation
✅ Correct Answer: C
Docker is written in Go (Golang), a language developed at Google known for its speed, concurrency support, and ability to compile to single static binaries. This makes Docker efficient and easy to distribute. Many other container ecosystem tools (Kubernetes, Prometheus, Terraform) are also written in Go.
12Which company originally created Docker?
  • A) Google
  • B) Microsoft
  • C) dotCloud (now Docker, Inc.)
  • D) Amazon Web Services
Show Answer & Explanation
✅ Correct Answer: C
Docker was created by Solomon Hykes while working at dotCloud, a Platform-as-a-Service (PaaS) company. After Docker became wildly popular, the company pivoted entirely to focus on Docker and rebranded as "Docker, Inc."
13Docker containers share which resource with the host machine?
  • A) The RAM only — containers cannot use disk at all
  • B) Nothing — containers are fully isolated from the host
  • C) The operating system kernel
  • D) The CPU brand and model number
Show Answer & Explanation
✅ Correct Answer: C
Unlike VMs which run their own kernel, Docker containers share the host's OS kernel. This is what makes containers so lightweight and fast. The isolation (namespaces) makes each container think it has its own system, but they all talk to the same underlying kernel.
↑ Back to top
⚖️ Core Concept

3. Containers vs Virtual Machines

Before Docker, the standard way to isolate applications was with Virtual Machines. Understanding the difference helps you appreciate why containers are so revolutionary.

Virtual Machines (VMs) Explained

A Virtual Machine is a software emulation of a complete computer. It uses a program called a hypervisor (like VMware, VirtualBox, or Hyper-V) to simulate hardware. Inside the VM, a full OS boots up — just like on a physical computer.

Think of it like building an entire house inside another house. Each VM has its own walls, plumbing, electricity — completely separate from the host, but wasteful of space and resources.

Containers Explained

A container is much lighter. Instead of running a full OS, it shares the host's kernel and uses Linux namespaces to create an isolated environment. Think of it like renting a single room — you share the building's infrastructure (plumbing, electricity/kernel) but your room is private and isolated.

Virtual Machines App A App B App C Guest OS Guest OS Guest OS Hypervisor (VMware/KVM) Host Operating System Hardware Containers (Docker) App A App B App C Libs only Libs only Libs only Docker Engine Host Operating System (shared) Hardware Key Difference Each VM has its own full OS (GBs overhead) Containers share host kernel (MBs overhead)
Fig 3.1 — VM architecture vs Container architecture: VMs include full Guest OS, containers share host kernel

Side-by-Side Comparison

FeatureVirtual MachineDocker Container
Startup timeMinutes (boots full OS)Seconds (or milliseconds)
SizeGBs (includes OS)MBs (just app + libs)
Isolation levelStrong (hardware-level)Good (kernel-level)
Performance overheadHigh (hypervisor layer)Near-native
OS compatibilityCan run any OS (e.g., Windows on Linux)Must match host kernel type
Resource usageHigh (each VM reserves RAM/CPU)Low (shares host resources)
PortabilityModerateExcellent — runs identically anywhere
Use caseFull OS isolation, run Windows on LinuxMicroservices, CI/CD, dev environments

How Linux Namespaces Enable Isolation

Linux namespaces are the magic behind containers. They give each container its own isolated view of the system:

PID namespace
Each container has its own process tree. Process 1 inside a container is not PID 1 on the host.
Network namespace
Each container gets its own network interfaces, IP address, and routing tables.
Mount namespace
Each container has its own filesystem view — it can't see the host's files (by default).
UTS namespace
Each container can have its own hostname.
IPC namespace
Isolates inter-process communication (message queues, shared memory).
User namespace
Maps container users to different host users for additional security.

How cgroups Control Resources

cgroups (control groups) let Docker limit and monitor how much CPU, memory, disk I/O, and network a container can use. This prevents one container from consuming all system resources and starving others.

  Terminal
# Limit a container to 512MB RAM and 50% of 1 CPU core
docker run --memory="512m" --cpus="0.5" nginx

# This uses cgroups under the hood to enforce these limits
🤔 When to Choose VMs over Containers?

Choose VMs when: you need to run Windows containers on Linux, require maximum security isolation (e.g., untrusted code), or need to run a completely different kernel. For most web applications and microservices, containers are the right choice.

🧠 Quiz — Section 3: Containers vs VMs (8 Questions)
14What technology does a Virtual Machine use to simulate hardware and run multiple OS instances?
  • A) Docker Engine
  • B) A Hypervisor
  • C) A Load Balancer
  • D) A DNS Server
Show Answer & Explanation
✅ Correct Answer: B
A hypervisor (also called a Virtual Machine Monitor) is the software layer that enables virtualization. It sits between the hardware and the guest OS, translating hardware calls. Examples include VMware ESXi, Microsoft Hyper-V, KVM, and VirtualBox.
15Which of the following is a type-1 (bare-metal) hypervisor?
  • A) Docker Desktop
  • B) nginx web server
  • C) VMware ESXi
  • D) Apache Kafka
Show Answer & Explanation
✅ Correct Answer: C
VMware ESXi is a Type-1 (bare-metal) hypervisor that runs directly on hardware without a host OS. Type-2 hypervisors like VirtualBox run on top of an existing OS. Docker is not a hypervisor — it's a container runtime that uses Linux kernel features directly.
16Compared to Virtual Machines, Docker containers are generally:
  • A) Slower to start and use more resources
  • B) More isolated due to hardware virtualization
  • C) Lighter weight and faster to start
  • D) Require installing a full OS in each instance
Show Answer & Explanation
✅ Correct Answer: C
Because containers share the host kernel and don't include a full OS, they start in seconds (or less) vs minutes for VMs. A Docker image for a Node.js app might be 50MB, while an equivalent VM image could be 5GB+. This efficiency enables running many more containers per server.
17A typical Virtual Machine image file is approximately what size?
  • A) A few kilobytes
  • B) A few megabytes
  • C) Several gigabytes
  • D) Several terabytes
Show Answer & Explanation
✅ Correct Answer: C
A VM image includes the full OS installation plus applications, typically ranging from 2GB to 40GB+. An Ubuntu VM image alone is about 2.5GB. Compare this to the official Ubuntu Docker image which is only about 28MB because it doesn't include the kernel.
18Which provides stronger, hardware-level isolation?
  • A) Docker containers
  • B) Virtual Machines
  • C) Both provide exactly the same level of isolation
  • D) Neither provides any meaningful isolation
Show Answer & Explanation
✅ Correct Answer: B
VMs provide stronger isolation because they emulate separate hardware. A compromised VM kernel cannot affect the host or other VMs. Containers share the host kernel, so a kernel exploit in one container could theoretically affect others. For untrusted workloads (like running user-submitted code), VMs are still preferred.
19Containers achieve process and filesystem isolation using which Linux kernel features?
  • A) Hardware virtualization extensions (VT-x)
  • B) Linux namespaces and cgroups
  • C) Separate physical network cards
  • D) Different CPU cores assigned per app
Show Answer & Explanation
✅ Correct Answer: B
Namespaces provide isolation (each container has its own view of PIDs, networking, filesystems). Cgroups provide resource limits (CPU, memory). These are pure software features of the Linux kernel — no special hardware needed beyond what any Linux system has.
20Which of the following is NOT a typical advantage of containers over VMs?
  • A) Faster startup time
  • B) Smaller image sizes
  • C) Full hardware-level kernel isolation from the host
  • D) Highly portable across different environments
Show Answer & Explanation
✅ Correct Answer: C
Full hardware-level kernel isolation is NOT a feature of containers — that's the advantage of VMs. Containers share the host kernel (providing only OS-level isolation via namespaces). Faster startup, smaller sizes, and portability ARE genuine container advantages over VMs.
21What does "cgroups" stand for in the context of Linux?
  • A) Container groups — a Docker-specific term
  • B) Control groups — a Linux kernel feature for resource management
  • C) Cloud groups — a cloud provider term
  • D) CPU groups — only related to processor allocation
Show Answer & Explanation
✅ Correct Answer: B
Cgroups stands for Control Groups. It's a Linux kernel feature (not Docker-specific) that allows limiting, prioritizing, accounting, and controlling the resource usage of process groups. Docker uses cgroups to implement --memory, --cpus, and other resource limit flags on containers.
↑ Back to top
🛠️ Setup

4. Installing Docker

Let's get Docker running on your machine. The easiest way for most developers is Docker Desktop, which bundles everything you need.

Docker Desktop (Mac, Windows, Linux)

Docker Desktop is a GUI application that includes Docker Engine, Docker CLI, Docker Compose, and more. It's the recommended way to run Docker on a developer machine.

⚠️ Docker Desktop Licensing

Docker Desktop is free for personal use, education, and small businesses (<250 employees, <$10M revenue). Larger companies need a paid subscription.

Installation on macOS

  1. Go to docker.com/products/docker-desktop and download the macOS installer (choose Intel or Apple Silicon)
  2. Open the downloaded .dmg file and drag Docker to your Applications folder
  3. Launch Docker from Applications — a whale icon appears in the menu bar
  4. Wait for Docker to start (the whale stops animating)
  5. Open Terminal and verify: docker version

Installation on Linux (Ubuntu/Debian)

  Terminal — Ubuntu/Debian
# Step 1: Update packages and install prerequisites
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg

# Step 2: Add Docker's official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Step 3: Set up the repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
  https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Step 4: Install Docker Engine
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

# Step 5: Run Docker without sudo (optional but recommended)
sudo usermod -aG docker $USER
newgrp docker

Installation on Windows

  1. Enable WSL 2 (Windows Subsystem for Linux) — Docker Desktop uses it for Linux containers
  2. Download Docker Desktop installer from docker.com
  3. Run the installer and follow the prompts
  4. Restart your computer when prompted
  5. Launch Docker Desktop from the Start menu

Verifying the Installation

  Terminal
# Check Docker version (client and server info)
docker version
# Output shows Client version, Server (daemon) version, API version, etc.

# Check Docker system info (resources, containers, images count)
docker info
# Output shows: containers running/stopped, images, storage driver, OS, CPUs, memory

# The classic Hello World test
docker run hello-world
# Docker pulls a tiny test image, runs it, prints a success message, then exits

The docker run hello-world command does several things automatically:

  1. Checks if the hello-world image is locally available
  2. Doesn't find it, so pulls it from Docker Hub (the default registry)
  3. Creates a container from the image
  4. Runs the container — it prints a message and exits

Understanding: docker version vs docker info

CommandWhat it shows
docker versionClient version, Server (daemon) version, API version, Go version, OS/Arch
docker infoNumber of containers/images, storage driver, cgroup driver, OS, memory, CPUs, registry URL
🧠 Quiz — Section 4: Installing Docker (5 Questions)
22What is Docker Desktop?
  • A) A command-line only tool for managing containers
  • B) A GUI application bundling Docker Engine, CLI, and Compose for Mac/Windows/Linux
  • C) A cloud-hosted Docker service by Docker Inc.
  • D) A type of Docker container used for development
Show Answer & Explanation
✅ Correct Answer: B
Docker Desktop is an all-in-one application that includes Docker Engine (the container runtime), the Docker CLI, Docker Compose, Docker Scan, and a GUI dashboard. It also manages the Linux VM required to run Linux containers on macOS and Windows.
23Which command verifies that Docker is correctly installed and both client and daemon are running?
  • A) docker start
  • B) docker check
  • C) docker version
  • D) docker init
Show Answer & Explanation
✅ Correct Answer: C
docker version shows both the Client version and Server (daemon) version. If the daemon is not running, it will show "Cannot connect to the Docker daemon" — a clear sign something is wrong. docker info also works but provides much more verbose output.
24On Linux, how does the Docker Engine typically run?
  • A) As a browser extension in Chrome
  • B) As a system daemon (background service managed by systemd)
  • C) Only as a desktop GUI application
  • D) As a web server on port 80
Show Answer & Explanation
✅ Correct Answer: B
On Linux, Docker Engine runs as a daemon called dockerd, managed by systemd. You can check its status with sudo systemctl status docker and start it with sudo systemctl start docker. It automatically starts on boot after installation.
25What is the Docker Engine?
  • A) A game development framework that uses Docker containers
  • B) The core runtime component that creates, runs, and manages containers
  • C) A cloud hosting service offered by Docker Inc.
  • D) A Kubernetes plugin for managing Docker swarms
Show Answer & Explanation
✅ Correct Answer: B
Docker Engine is the core technology that makes containers work. It consists of: the dockerd daemon, a REST API, and the CLI. When you run docker run, the CLI sends a request to the daemon via the API, and the daemon does the actual work of creating/running the container.
26After installing Docker, which command shows all currently running containers?
  • A) docker list
  • B) docker show
  • C) docker ps
  • D) docker containers --active
Show Answer & Explanation
✅ Correct Answer: C
docker ps (ps = process status) shows only running containers by default. To see all containers including stopped ones, use docker ps -a. The output includes Container ID, Image, Command, Created time, Status, Ports, and Name.
↑ Back to top
🏗️ Architecture

5. Docker Architecture

Understanding Docker's architecture helps you know what's happening behind the scenes every time you type a Docker command.

The Three Main Components

Docker uses a client-server architecture. The three main components are:

Docker Client (CLI)
The tool you interact with. When you type docker run, the client sends that request to the daemon.
Docker Daemon (dockerd)
The server component. Runs in the background, receives commands from the client, and manages all Docker objects (images, containers, networks, volumes).
Docker Registry
A storage and distribution system for Docker images. Docker Hub is the default public registry.
🖥️ Docker Client docker build docker pull docker run REST API UNIX socket ⚙️ Docker Daemon (dockerd) Manages Images Manages Containers Manages Networks Manages Volumes push/pull Registry Docker Hub Private Reg. ECR/GCR/ACR Client and daemon can run on the same machine or different machines
Fig 5.1 — Docker's client-server architecture: Client → Daemon (via API) → Registry

How a docker run Command Works Step by Step

  1. You type docker run nginx in the terminal
  2. The Docker client parses the command and sends a REST API request to the Docker daemon via the Unix socket (/var/run/docker.sock)
  3. The daemon checks if the nginx image exists locally
  4. Image not found → daemon sends a pull request to Docker Hub (registry)
  5. The image layers are downloaded and stored locally
  6. The daemon creates a new container from the image
  7. The container starts running — nginx is now serving requests

The Docker Socket

The Docker daemon listens on a Unix socket at /var/run/docker.sock. This is how the CLI communicates with it. When you mount this socket into a container, that container can control Docker on the host — a powerful but security-sensitive pattern.

  Terminal
# See all Docker objects and system info
docker info

# The daemon is a systemd service on Linux
sudo systemctl status docker

# You can also communicate via TCP (for remote Docker)
# DOCKER_HOST=tcp://192.168.1.10:2376 docker ps
🧠 Quiz — Section 5: Docker Architecture (8 Questions)
27What is the Docker daemon (dockerd)?
  • A) A type of Docker container used for background tasks
  • B) A background service that manages all Docker objects and responds to API requests
  • C) The Docker command-line interface tool
  • D) A Docker image for running system services
Show Answer & Explanation
✅ Correct Answer: B
The Docker daemon (dockerd) is the server component that runs in the background. It manages all Docker objects (images, containers, networks, volumes) and listens for Docker API requests from clients. The client sends commands; the daemon executes them.
28How does the Docker CLI communicate with the Docker daemon?
  • A) Via SSH (Secure Shell)
  • B) Via a REST API (typically over a Unix socket)
  • C) Via FTP (File Transfer Protocol)
  • D) Via Telnet
Show Answer & Explanation
✅ Correct Answer: B
The Docker CLI uses a REST API to communicate with the daemon. By default, this goes through the Unix socket at /var/run/docker.sock. For remote Docker, TCP (with TLS for security) is used. This clean API design means you can also control Docker programmatically from any language.
29What is Docker Hub?
  • A) A local development tool for building images faster
  • B) The default public registry for storing and sharing Docker images
  • C) A container runtime alternative to Docker Engine
  • D) A plugin for connecting Docker containers to networks
Show Answer & Explanation
✅ Correct Answer: B
Docker Hub (hub.docker.com) is the world's largest container image registry. It hosts official images for most popular software (nginx, postgres, node, python, etc.) and allows developers to publish their own images. When you run docker pull nginx, Docker Hub is the default source.
30When you run docker pull ubuntu, which component actually fetches the image?
  • A) The Docker CLI directly downloads it
  • B) The Docker daemon fetches it from the registry
  • C) Docker Hub pushes it to your machine
  • D) Docker Compose handles all image downloads
Show Answer & Explanation
✅ Correct Answer: B
The CLI just sends the pull command to the daemon via the API. The daemon is the one that actually communicates with the registry (Docker Hub), authenticates if needed, downloads the image layers, and stores them locally. The CLI is just a thin client.
31Where does the Docker daemon listen by default on Linux?
  • A) /var/run/nginx.sock
  • B) /var/run/docker.sock
  • C) /tmp/docker.sock
  • D) /etc/docker/daemon.sock
Show Answer & Explanation
✅ Correct Answer: B
/var/run/docker.sock is the Unix domain socket that the Docker daemon listens on by default. The Docker CLI connects to this socket to send API requests. Mounting this socket into a container (e.g., in CI/CD pipelines) allows that container to control the host Docker daemon.
32Which of the following is a Docker object managed by the daemon?
  • A) A JavaScript module file
  • B) A Python virtual environment
  • C) A Docker Image
  • D) An HTML webpage
Show Answer & Explanation
✅ Correct Answer: C
Docker objects are entities that Docker manages: Images, Containers, Networks, and Volumes. These are all created, tracked, and managed by the Docker daemon. JavaScript files, Python environments, and HTML pages are application-level files, not Docker objects.
33In Docker architecture, what is the Registry responsible for?
  • A) Running and monitoring containers in production
  • B) Building Docker images from source code
  • C) Storing and distributing Docker images
  • D) Managing container networking and DNS
Show Answer & Explanation
✅ Correct Answer: C
A registry is purely a storage and distribution system for Docker images. It doesn't run containers or build images. You push images to it and pull images from it. Docker Hub is the default public registry, but you can run private registries (e.g., Amazon ECR, Google GCR, self-hosted with registry:2 image).
34When you run docker run ubuntu and the image is not found locally, what does Docker automatically do?
  • A) Returns an error and exits without doing anything
  • B) Pulls the image from the default registry (Docker Hub) and then runs it
  • C) Creates an empty container without any OS
  • D) Asks you to manually download and install the image first
Show Answer & Explanation
✅ Correct Answer: B
Docker has a "pull before run" behavior. If the image isn't found locally, it automatically pulls it from the registry configured (Docker Hub by default) before running. You'll see "Unable to find image 'ubuntu:latest' locally" followed by the pull progress. This convenience is why Docker is so easy to get started with.
↑ Back to top
🖼️ Images

6. Docker Images

A Docker image is like a recipe or blueprint. It contains everything needed to run an application — code, runtime, libraries, config files — but it's not running yet.

What is a Docker Image?

Think of a Docker image as a snapshot of a filesystem. It's read-only — you can't modify it. When you want to run it, Docker creates a container from it (like making a cake from a recipe — the recipe doesn't change, but each cake is a new instance).

Images are described by a name and a tag: name:tag — e.g., nginx:1.25 or python:3.11-slim.

The Layer System

Docker images are composed of read-only layers stacked on top of each other. Each layer represents a change: adding files, installing software, modifying config. This system is very efficient — if two images share common base layers, those layers are stored only once on disk.

Layer 0: Scratch / Base OS (Ubuntu 22.04) Layer 1: apt-get install python3 (120 MB) Layer 2: pip install requirements.txt (45 MB) Layer 3: COPY app code (2 MB) Layer 4: ENV + CMD (config) Layers build on top of each other (read-only) Bottom → Top ✏️ Container Writable Layer (added when container starts)
Fig 6.1 — Docker image layers stacked from base OS to application config. Container adds a writable layer on top.

Essential Image Commands

Pulling an Image

  Terminal
# Pull the latest nginx image from Docker Hub
docker pull nginx
# Docker pulls it as: nginx:latest  (default tag)

# Pull a specific version (tag)
docker pull nginx:1.25-alpine
# alpine = a very small Linux distro, great for small images

# Pull an image from a specific registry (not Docker Hub)
docker pull gcr.io/google-containers/pause:3.9

How it works: Docker checks if you have the image locally. If not, it contacts the registry, downloads each layer individually (in parallel), and assembles the image locally. Downloaded layers are cached — if another image shares a layer, it won't be re-downloaded.

Listing Images

  Terminal
# List all locally available images
docker images
# or equivalently:
docker image ls

# Output columns:
# REPOSITORY   TAG       IMAGE ID      CREATED       SIZE
# nginx        latest    a6bd71f48f68  2 weeks ago   187MB
# ubuntu       22.04     174c8c134b2a  5 weeks ago   77.8MB

# Show all images including intermediate layers
docker images -a

# Filter images by name
docker images nginx

Inspecting an Image

  Terminal
# Get detailed JSON metadata about an image
docker inspect nginx

# This shows: layers, environment variables, exposed ports,
# architecture, OS, creation date, entrypoint, etc.

# Show image layer history
docker history nginx
# Output shows each layer, its size, and the command that created it

Removing Images

  Terminal
# Remove a specific image
docker rmi nginx
# or:
docker image rm nginx

# Force remove (even if containers use it)
docker rmi -f nginx

# Remove all unused images (not used by any container)
docker image prune

# Remove ALL images (careful!)
docker image prune -a

Image Tags Explained

Tags are labels attached to images to distinguish versions. Format: repository:tag

nginx:latest
The most recent stable release of nginx
python:3.11-slim
Python 3.11, slim variant (minimal OS packages)
python:3.11-alpine
Python 3.11 on Alpine Linux (~50MB vs ~1GB)
ubuntu:22.04
Ubuntu version 22.04 specifically
myapp:v1.2.3
Your custom app at version 1.2.3
⚠️ Avoid :latest in Production

The :latest tag is the default when no tag is specified, but it changes over time. In production, always pin to a specific version tag like nginx:1.25.3 to ensure reproducible deployments.

🧠 Quiz — Section 6: Docker Images (10 Questions)
35What is a Docker image?
  • A) A running process inside a container
  • B) A read-only template containing everything needed to create a container
  • C) A type of virtual machine disk file
  • D) A configuration file for the Docker daemon
Show Answer & Explanation
✅ Correct Answer: B
A Docker image is a read-only template that contains the application code, runtime, libraries, and configuration. You can't modify an image directly — it's immutable. When you run an image, Docker creates a container (a writable instance) from it.
36How are Docker images stored internally?
  • A) As a single large file (like a .zip archive)
  • B) As read-only layers stacked on top of each other
  • C) Only as compiled executable binaries
  • D) As XML configuration files
Show Answer & Explanation
✅ Correct Answer: B
Images use a layered filesystem. Each instruction in a Dockerfile creates a new layer. Layers are read-only and shared between images that have common bases. When you run a container, Docker adds a thin writable layer on top. This design makes images efficient in storage and transfer.
37Which command downloads a Docker image from Docker Hub to your local machine?
  • A) docker get nginx
  • B) docker fetch nginx
  • C) docker pull nginx
  • D) docker download nginx
Show Answer & Explanation
✅ Correct Answer: C
docker pull explicitly downloads an image from a registry. Note that docker run also pulls automatically if the image isn't local. docker pull is useful when you want to pre-download images or update to the latest version of a tagged image.
38What does docker images (or docker image ls) display?
  • A) Downloads and shows a preview of all images on Docker Hub
  • B) Lists all Docker images stored locally on your machine
  • C) Builds all images found in the current directory
  • D) Deletes all locally stored images
Show Answer & Explanation
✅ Correct Answer: B
docker images lists all images in your local Docker image cache, showing REPOSITORY, TAG, IMAGE ID, CREATED, and SIZE. The Image ID is a SHA256 hash (truncated). The same image can have multiple tags pointing to it.
39In Docker image naming, what is a "tag" used for?
  • A) To add metadata labels to a running container
  • B) To identify a specific version or variant of an image
  • C) To connect an image to a Docker network
  • D) To mount a volume inside an image
Show Answer & Explanation
✅ Correct Answer: B
Tags distinguish different versions or variants of an image. For example, python:3.11 and python:3.9 are different tags of the Python image. Tags like -slim or -alpine indicate size variants. Multiple tags can point to the same underlying image ID.
40What is the default tag applied when you run docker pull nginx without specifying a tag?
  • A) :v1
  • B) :stable
  • C) :latest
  • D) :current
Show Answer & Explanation
✅ Correct Answer: C
When no tag is specified, Docker defaults to :latest. So docker pull nginx is identical to docker pull nginx:latest. Be careful: :latest just means "the image tagged 'latest'" — it doesn't always mean the newest version. Image maintainers choose what latest points to.
41Which command removes a Docker image from your local machine?
  • A) docker delete image nginx
  • B) docker remove image nginx
  • C) docker rmi nginx
  • D) docker image kill nginx
Show Answer & Explanation
✅ Correct Answer: C
docker rmi (remove image) deletes a local image. The equivalent modern syntax is docker image rm. If a container (even stopped) is using the image, Docker will refuse to remove it — use -f to force. To remove all unused images, use docker image prune -a.
42What does the docker inspect nginx command output?
  • A) Opens an interactive terminal inside a running nginx container
  • B) Detailed metadata about the image in JSON format (layers, env vars, ports, etc.)
  • C) Network performance statistics for nginx traffic
  • D) Real-time CPU and memory usage of nginx
Show Answer & Explanation
✅ Correct Answer: B
docker inspect returns a JSON object with all metadata: the image's layers (RootFS), environment variables, exposed ports, entrypoint, labels, architecture, OS, creation timestamp, and more. It works on both images and containers.
43What is a "base image" in the context of Docker?
  • A) The largest image stored on Docker Hub
  • B) An image with no parent; the starting point for building other images
  • C) Any image stored on your local disk
  • D) A compressed backup of all your Docker images
Show Answer & Explanation
✅ Correct Answer: B
A base image is the foundation of a Docker image — it has no parent layer. Common base images include ubuntu, debian, alpine, and scratch. When you write FROM ubuntu:22.04 in a Dockerfile, ubuntu:22.04 is your base image.
44The scratch image in Docker is:
  • A) A debugging image with extra diagnostic tools
  • B) An empty image with absolutely nothing in it — the ultimate minimal base
  • C) A test image provided by Docker Inc. for tutorials
  • D) An image that only contains a minimal shell (like Alpine)
Show Answer & Explanation
✅ Correct Answer: B
scratch is a special reserved name in Docker — it's a completely empty image with no OS, no shell, no files. It's used for building ultra-minimal images, typically with statically compiled Go binaries that don't need any OS libraries. It results in the smallest possible images.
↑ Back to top
📦 Containers

7. Docker Containers

A container is a running instance of an image. This is where your application actually lives and executes. Let's master all the essential container operations.

Container Lifecycle

Created Running Paused Stopped start pause unpause stop/kill docker rm (removes from stopped state)
Fig 7.1 — Docker container lifecycle: Created → Running → Paused/Stopped → Removed

Running a Container: docker run

docker run is the most important Docker command. It creates and starts a container in one step.

  Terminal
# Basic: run nginx web server
docker run nginx
# Pulls nginx:latest if not local, creates & starts a container
# (runs in FOREGROUND — use Ctrl+C to stop)

# Run in detached (background) mode with -d
docker run -d nginx
# Returns the container ID immediately, runs in background

# Run with a custom name
docker run -d --name my-nginx nginx

# Map host port 8080 to container port 80
docker run -d -p 8080:80 --name web nginx
# Now visit http://localhost:8080 to see nginx

# Set environment variables
docker run -d -e MYSQL_ROOT_PASSWORD=secret --name db mysql:8

# Run an interactive terminal (e.g., explore Ubuntu)
docker run -it ubuntu bash
# -i = interactive (keep stdin open)
# -t = allocate a pseudo-TTY (terminal)
# bash = command to run inside the container

Anatomy of docker run -d -p 8080:80 --name web nginx

docker run
Create and start a new container
-d
Detached mode — runs in background, returns container ID
-p 8080:80
Port mapping: host_port:container_port. Traffic to host:8080 is forwarded to container:80
--name web
Assign the name "web" to this container (instead of a random name)
nginx
The image to use (pulls nginx:latest if not local)

Managing Running Containers

  Terminal
# List running containers
docker ps

# List ALL containers (including stopped)
docker ps -a

# Stop a running container (graceful - sends SIGTERM, waits 10s, then SIGKILL)
docker stop web

# Forcefully kill a container immediately (sends SIGKILL)
docker kill web

# Start a stopped container
docker start web

# Restart a container
docker restart web

# Remove a stopped container
docker rm web

# Remove a running container (force)
docker rm -f web

# Remove ALL stopped containers
docker container prune

Interacting with Running Containers

  Terminal
# Open a shell inside a running container
docker exec -it web bash
# -i = interactive, -t = terminal, bash = shell command

# Run a single command inside a container
docker exec web ls /etc/nginx/

# View container logs (stdout + stderr)
docker logs web

# Follow logs in real-time (like tail -f)
docker logs -f web

# Show last 50 lines
docker logs --tail 50 web

# View real-time resource usage (CPU, memory, network, disk)
docker stats

# View container processes
docker top web

# Copy files from container to host
docker cp web:/etc/nginx/nginx.conf ./nginx.conf

# Copy files from host to container
docker cp ./nginx.conf web:/etc/nginx/nginx.conf
💡 Containers are Ephemeral by Default

Any files written inside a container are lost when it's removed (docker rm). To persist data, use Volumes (Section 9). This is by design — containers are meant to be stateless and disposable.

🧠 Quiz — Section 7: Docker Containers (10 Questions)
45What is a Docker container?
  • A) A Docker image stored on disk in a compressed format
  • B) A running (or stopped) instance created from a Docker image
  • C) A Docker registry for storing images
  • D) A virtual machine that runs on Docker Engine
Show Answer & Explanation
✅ Correct Answer: B
A container is a running instance of an image — the image is the template, the container is the actual process. The analogy: image is a class (template), container is an object (instance). You can run many containers from the same image simultaneously.
46What does the -d flag do in docker run -d nginx?
  • A) Runs the container with debug logging enabled
  • B) Runs the container in detached (background) mode
  • C) Deletes the container automatically when it stops
  • D) Runs the container with root privileges
Show Answer & Explanation
✅ Correct Answer: B
The -d (detached) flag runs the container in the background. The command returns immediately with the container ID, and the container keeps running. Without -d, the container runs in the foreground and you see its output directly — pressing Ctrl+C stops it. To auto-delete on stop, use --rm.
47In docker run -p 8080:80 nginx, what does -p 8080:80 mean?
  • A) The container uses port 8080 internally and port 80 externally
  • B) Traffic arriving at host port 8080 is forwarded to container port 80
  • C) The image version is 8080, build 80
  • D) Both ports 8080 and 80 are blocked for security
Show Answer & Explanation
✅ Correct Answer: B
The format is host_port:container_port. So -p 8080:80 means: map your machine's port 8080 to the container's port 80. When you visit localhost:8080 in your browser, Docker forwards that traffic to nginx listening on port 80 inside the container.
48What is the difference between docker stop and docker kill?
  • A) They are identical — both do the same thing
  • B) stop sends SIGTERM (graceful shutdown), waits, then SIGKILL; kill sends SIGKILL immediately
  • C) stop pauses the container; kill stops it
  • D) kill also removes the container; stop only pauses it
Show Answer & Explanation
✅ Correct Answer: B
docker stop is graceful: it sends SIGTERM (please shut down cleanly), waits 10 seconds for the app to save state and exit, then sends SIGKILL. docker kill immediately sends SIGKILL (force quit) with no grace period. Use stop for production workloads.
49What does docker ps -a display?
  • A) Only containers that are currently running
  • B) All containers — both running and stopped
  • C) All locally stored Docker images
  • D) All processes running inside all containers
Show Answer & Explanation
✅ Correct Answer: B
docker ps by default only shows running containers. The -a (all) flag includes stopped, exited, and created containers. This is essential for debugging — if a container exited unexpectedly, you need -a to see it and then docker logs to see why it crashed.
50What does docker exec -it mycontainer bash do?
  • A) Starts a brand new container named mycontainer using bash
  • B) Opens an interactive bash shell inside the already-running container
  • C) Restarts the container and switches to bash shell mode
  • D) Kills the main bash process running inside the container
Show Answer & Explanation
✅ Correct Answer: B
docker exec runs a command in an EXISTING running container. The -it flags provide an interactive terminal. This is incredibly useful for debugging — you can explore the container's filesystem, check logs, test commands, etc., without stopping the container.
51What happens to data written inside a container when the container is deleted with docker rm?
  • A) Data is automatically saved to the current directory on the host
  • B) Data is permanently lost unless it was stored in a Docker volume
  • C) Data is pushed to Docker Hub for safekeeping
  • D) Data is compressed and archived in /tmp
Show Answer & Explanation
✅ Correct Answer: B
Container filesystems are ephemeral by design. The writable layer added on top of the image is destroyed when the container is removed. To persist data (database files, uploaded files, etc.), you must use Docker Volumes or Bind Mounts, which store data on the host filesystem.
52The --name flag in docker run is used to:
  • A) Assign a name to a Docker image being built
  • B) Assign a custom name to the container for easy reference
  • C) Name a Docker network to connect to
  • D) Set the hostname of the host machine running Docker
Show Answer & Explanation
✅ Correct Answer: B
Without --name, Docker generates a random name (like "jovial_einstein" or "hardcore_tesla"). Using --name lets you use a meaningful name in subsequent commands: docker stop myapp, docker logs myapp instead of using the container ID.
53What does docker logs mycontainer show?
  • A) The system logs of the host machine where Docker is installed
  • B) The stdout and stderr output produced by the container's main process
  • C) The Docker daemon's own operational logs
  • D) All network traffic logs for the container
Show Answer & Explanation
✅ Correct Answer: B
Docker captures stdout and stderr from the container's main process (PID 1). docker logs retrieves this output. Add -f to stream in real-time (like tail -f). This is your primary debugging tool when a container misbehaves. Applications in containers should log to stdout/stderr for this reason.
54Which command removes all stopped containers in one go?
  • A) docker rm -f all
  • B) docker container prune
  • C) docker remove --all-stopped
  • D) docker clean containers
Show Answer & Explanation
✅ Correct Answer: B
docker container prune removes all stopped containers. It asks for confirmation first. To also clean up dangling images and unused networks/volumes, use docker system prune. Add -f to skip the confirmation prompt in scripts.
↑ Back to top
📝 Dockerfile

8. Dockerfile

A Dockerfile is a text script with instructions to build a custom Docker image. It's how you tell Docker exactly how to package your application.

What is a Dockerfile?

Think of a Dockerfile as a recipe for building a Docker image. Each instruction in the file creates a new layer in the image. Docker reads the file top-to-bottom and executes each instruction sequentially during the build.

📄 Dockerfile FROM node:18 WORKDIR /app COPY . . RUN npm install CMD ["node","app"] Build Context (local files) docker build Docker Image myapp:1.0 Ready to run!
Fig 8.1 — docker build reads the Dockerfile + build context to produce a Docker image

All Dockerfile Instructions Explained

FROM — Set Base Image

  Dockerfile
FROM node:18-alpine
# Every Dockerfile MUST start with FROM (except multi-stage)
# node:18-alpine means Node.js version 18 on Alpine Linux (small ~50MB)
# vs node:18 which is ~900MB on Debian

WORKDIR — Set Working Directory

WORKDIR /app
# Sets /app as the working directory for all subsequent instructions
# Creates the directory if it doesn't exist
# Better than: RUN mkdir /app && cd /app (which doesn't persist)

COPY — Copy Files from Host

COPY package*.json ./
# Copies package.json and package-lock.json from build context (host) to /app

COPY . .
# Copies everything from build context to /app
# (use .dockerignore to exclude node_modules, .git, etc.)

RUN — Execute Commands During Build

RUN npm install
# Executes during IMAGE BUILD, creates a new layer
# Good for: installing packages, compiling code, setting up config

RUN apt-get update && apt-get install -y curl vim && rm -rf /var/lib/apt/lists/*
# Combine multiple commands with && to create ONE layer (more efficient)
# rm -rf cleans up apt cache to reduce image size

ENV — Set Environment Variables

ENV NODE_ENV=production
ENV PORT=3000
# Available during build AND runtime
# Can be overridden at runtime: docker run -e NODE_ENV=development

EXPOSE — Document Port

EXPOSE 3000
# DOCUMENTS that the container listens on port 3000
# Does NOT actually publish the port (that's -p at runtime)
# It's informational and used by orchestration tools

CMD — Default Command

CMD ["node", "server.js"]
# The default command that runs when a container starts
# Can be OVERRIDDEN at runtime: docker run myapp npm test
# Use JSON array form (exec form) — preferred over shell form

ENTRYPOINT — Set Main Executable

ENTRYPOINT ["node"]
CMD ["server.js"]
# ENTRYPOINT: the fixed executable (harder to override)
# CMD: default arguments to ENTRYPOINT (easily overridden)
# Combined: runs "node server.js" by default
# Override: docker run myapp other-file.js  → runs "node other-file.js"

ADD vs COPY

COPY src/ /app/src/    # Preferred: simple, predictable

ADD archive.tar.gz /app/  # ADD can auto-extract tar archives
ADD http://example.com/file.txt /app/  # ADD can download URLs
# Rule of thumb: prefer COPY unless you need ADD's extra features

Complete Example: Dockerizing a Node.js App

  Dockerfile
# Stage 1: Use official Node.js Alpine image as base
FROM node:18-alpine

# Set working directory inside the container
WORKDIR /app

# Copy dependency files first (layer caching optimization!)
# If package.json hasn't changed, Docker reuses the cached layer
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy remaining application source code
COPY . .

# Declare the port our app listens on (documentation)
EXPOSE 3000

# Set NODE_ENV to production
ENV NODE_ENV=production

# Command to run when container starts
CMD ["node", "server.js"]

Building the Image

  Terminal
# Build image from Dockerfile in current directory
# -t = tag (name:version)  .  = build context (current directory)
docker build -t myapp:1.0 .

# Build with a different Dockerfile location
docker build -t myapp:1.0 -f docker/Dockerfile.prod .

# Build with build arguments (values passed at build time)
docker build -t myapp:1.0 --build-arg API_URL=https://api.prod.com .

# After building, run it
docker run -d -p 3000:3000 --name myapp myapp:1.0

The .dockerignore File

Just like .gitignore excludes files from git, .dockerignore excludes files from the build context. This speeds up builds and prevents accidentally copying secrets into images.

  .dockerignore
node_modules/
.git/
.env
*.log
.DS_Store
README.md
*.test.js

Multi-Stage Builds

Multi-stage builds let you use multiple FROM statements. Each stage can copy artifacts from previous stages. This is great for compiled languages — compile in one stage, copy only the binary to the final small image.

# Stage 1: Build (has all build tools)
FROM golang:1.21 AS builder
WORKDIR /app
COPY . .
RUN go build -o myserver .

# Stage 2: Run (only has the tiny binary)
FROM alpine:3.18
WORKDIR /app
COPY --from=builder /app/myserver .
EXPOSE 8080
CMD ["./myserver"]
# Result: ~15MB image instead of ~1GB (no Go compiler needed at runtime!)
💡 Layer Caching Tip

Docker caches each layer. If a layer hasn't changed, it reuses the cached version (much faster). Always copy dependency files (package.json, requirements.txt) and install dependencies BEFORE copying your application code. That way, a code change doesn't invalidate the expensive "install dependencies" layer.

🧠 Quiz — Section 8: Dockerfile (12 Questions)
55What is a Dockerfile?
  • A) A running script inside a container
  • B) A text file with step-by-step instructions to build a Docker image
  • C) A YAML configuration file for the Docker daemon
  • D) The same as a docker-compose.yml file
Show Answer & Explanation
✅ Correct Answer: B
A Dockerfile is a plain text file (named exactly "Dockerfile") containing sequential instructions that Docker reads to build an image. Each instruction creates a layer. It's the blueprint that makes builds reproducible and portable.
56Which Dockerfile instruction MUST appear first (or near the top in multi-stage builds) to set the base image?
  • A) BASE
  • B) START
  • C"> FROM
  • D) IMAGE
Show Answer & Explanation
✅ Correct Answer: C
FROM must be the first instruction in a Dockerfile (ARG can appear before FROM). It sets the base image that all subsequent instructions build on. Every image must start from something — even if it's FROM scratch (empty).
57What does the RUN instruction do in a Dockerfile?
  • A) Starts the final application when the container boots
  • B) Executes a command during the image build process, creating a new layer
  • C) Runs an existing Docker container
  • D) Sets environment variables for the build
Show Answer & Explanation
✅ Correct Answer: B
RUN executes commands during the BUILD phase (when you run docker build). It's used to install packages, compile code, create directories, etc. Each RUN creates a new cached layer. To start the app when a container runs, use CMD or ENTRYPOINT.
58What is the key difference between CMD and ENTRYPOINT in a Dockerfile?
  • A) There is no practical difference — they are aliases
  • B) CMD provides defaults easily overridden at runtime; ENTRYPOINT sets the main executable that's harder to override
  • C) CMD is for Linux containers; ENTRYPOINT is for Windows containers
  • D) ENTRYPOINT runs at build time; CMD runs at container start
Show Answer & Explanation
✅ Correct Answer: B
CMD sets default command/arguments that can be completely replaced: docker run myimage custom-command replaces CMD. ENTRYPOINT sets the executable — you'd need --entrypoint flag to override it. Combined: ENTRYPOINT is the executable, CMD is its default args.
59What does the COPY instruction do in a Dockerfile?
  • A) Downloads files from the internet into the image
  • B) Copies files from the build context (your local machine) into the image
  • C) Copies files between running containers
  • D) Copies images between Docker registries
Show Answer & Explanation
✅ Correct Answer: B
COPY <src> <dest> copies files from the build context (directory sent to Docker daemon during build) into the container filesystem. src is relative to the build context; dest is inside the container. Files in .dockerignore are excluded from the context.
60What does WORKDIR /app do in a Dockerfile?
  • A) Creates a new Docker volume named /app
  • B) Sets /app as the working directory; all subsequent instructions run relative to it
  • C) Mounts the /app directory from the host machine into the container
  • D) Permanently deletes the /app directory from the image
Show Answer & Explanation
✅ Correct Answer: B
WORKDIR sets the working directory for subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions. It creates the directory if it doesn't exist. It's equivalent to mkdir -p /app && cd /app but persists across subsequent instructions.
61What is the purpose of a .dockerignore file?
  • A) To suppress Docker warnings during builds
  • B) To exclude files and directories from the build context sent to Docker
  • C) To disable certain Dockerfile instructions
  • D) To ignore network errors during image pulling
Show Answer & Explanation
✅ Correct Answer: B
Like .gitignore, a .dockerignore file specifies files to exclude from the build context. This speeds up builds (less data transferred to daemon), avoids accidentally including secrets (.env), and prevents large directories like node_modules from being needlessly sent.
62Which command builds a Docker image from a Dockerfile?
  • A) docker create -t myapp .
  • B) docker make myapp .
  • C) docker build -t myapp:1.0 .
  • D) docker image new myapp .
Show Answer & Explanation
✅ Correct Answer: C
docker build is the command that reads a Dockerfile and builds an image. -t tags the image with name:version. The . at the end specifies the build context (current directory). Docker sends this context to the daemon, which uses it along with the Dockerfile.
63In docker build -t myapp:1.0 ., what does the trailing . represent?
  • A) The current directory as the build context (files available during build)
  • B) A version separator in the tag
  • C) All files currently on Docker Hub for this image
  • D) The location of the Dockerfile (always current directory)
Show Answer & Explanation
✅ Correct Answer: A
The . specifies the build context — the directory whose contents are sent to the Docker daemon. The daemon then uses files from this context when processing COPY instructions. The Dockerfile itself is expected to be in this directory, but you can override it with -f path/to/Dockerfile.
64What is the main benefit of a multi-stage build in Docker?
  • A) It builds the same image on multiple Docker registries simultaneously
  • B) It creates a smaller final image by discarding build tools and intermediate files, keeping only the runtime artifacts
  • C) It allows running multiple containers from a single Dockerfile
  • D) It lets you have multiple CMD instructions in one Dockerfile
Show Answer & Explanation
✅ Correct Answer: B
Multi-stage builds use multiple FROM statements. The first stage might have a full compiler toolchain to build your app; the final stage only copies the compiled binary. The result is a tiny production image without any build tools. A Go app might shrink from 800MB to 15MB this way.
65The ENV NODE_ENV=production instruction in a Dockerfile:
  • A) Sets an environment variable available ONLY during the build
  • B) Sets an environment variable available during both build time and at container runtime
  • C) Encrypts all environment data in the image
  • D) Defines which Node.js entrypoint script to execute
Show Answer & Explanation
✅ Correct Answer: B
ENV variables are persisted in the image and available at runtime. They can be overridden when running a container with docker run -e NODE_ENV=development. Be careful: never use ENV for secrets (passwords, API keys) as they're visible in docker inspect.
66What does EXPOSE 8080 in a Dockerfile actually do?
  • A) Opens port 8080 on the host machine automatically
  • B) Documents that the container listens on port 8080, but does not actually publish it
  • C) Forwards all traffic to port 8080 inside the container
  • D) Blocks all traffic except port 8080
Show Answer & Explanation
✅ Correct Answer: B
EXPOSE is documentation — it tells users and tools that the container listens on that port, but it doesn't actually publish it to the host. To make it accessible from outside, you still need -p 8080:8080 (or -P to auto-publish all exposed ports to random host ports).
↑ Back to top
💾 Volumes

9. Docker Volumes

Containers are ephemeral — data inside them disappears when they're removed. Volumes are the solution, providing persistent storage that outlives any container.

Why Volumes?

Imagine running a database inside a container. The database stores its files in the container's filesystem. If you remove the container (to update the database version, for example), all your data is gone. That's a disaster.

Docker Volumes solve this by storing data on the host filesystem (or network storage) outside the container. The container can be deleted and recreated, but the volume — and your data — persists.

Three Types of Docker Storage

Docker Container /var/lib/mysql → volume Named Volume Docker managed /var/lib/docker/volumes/ -v mydata:/var/lib/mysql Bind Mount Any host path /home/user/data:/data tmpfs Mount Host RAM only Not persisted to disk
Fig 9.1 — Three storage types: Named Volumes (Docker-managed), Bind Mounts (host path), tmpfs (memory only)

Named Volumes — The Recommended Way

Named volumes are managed entirely by Docker. Docker decides where on the host to store them (usually /var/lib/docker/volumes/). They're portable — they can be backed up, migrated, and shared between containers.

  Terminal
# Create a named volume
docker volume create mydata

# Use it when running a container
# -v volume_name:container_path
docker run -d \
  --name postgres-db \
  -e POSTGRES_PASSWORD=secret \
  -v mydata:/var/lib/postgresql/data \
  postgres:15

# The database files are stored in the 'mydata' volume
# Delete and recreate the container — data persists!
docker rm -f postgres-db
docker run -d --name postgres-db -v mydata:/var/lib/postgresql/data postgres:15
# ✅ Data is still there!

# List all volumes
docker volume ls

# Inspect a volume (shows where it's stored on host)
docker volume inspect mydata

# Remove a volume (DATA IS DELETED!)
docker volume rm mydata

# Remove all unused volumes
docker volume prune

Bind Mounts — Mount a Host Directory

Bind mounts link a specific directory on your host to a path inside the container. Changes on either side are immediately reflected on the other side. Perfect for development — edit code on your host, see changes immediately in the container.

  Terminal
# Mount current host directory to /app in container
# $(pwd) = print working directory (current directory)
docker run -d \
  -p 3000:3000 \
  -v $(pwd):/app \
  node:18-alpine \
  node server.js
# Edit code on your host → changes instantly visible in container

# Read-only bind mount (container can't modify the host files)
docker run -d -v $(pwd)/config:/app/config:ro nginx

tmpfs Mounts — In-Memory Storage

tmpfs mounts store data in the host's RAM. The data is never written to disk — it disappears when the container stops. Useful for sensitive temporary data (like secrets or session tokens) that you don't want written to disk.

  Terminal
# Mount an in-memory filesystem at /tmp inside the container
docker run -d --tmpfs /tmp nginx

# Or using the --mount syntax (more verbose but explicit)
docker run -d \
  --mount type=tmpfs,destination=/tmp,tmpfs-size=100m \
  nginx

Comparison: Volumes vs Bind Mounts

FeatureNamed VolumeBind Mount
Storage locationDocker-managed (/var/lib/docker/volumes)Any host path you specify
Best forProduction data (databases, persistent files)Development (live code reload)
PortabilityHigh — Docker manages itLow — depends on host path
PerformanceOptimized by DockerGood on Linux, slower on Mac/Windows
BackupEasy via Docker commandsManual host backup
🧠 Quiz — Section 9: Docker Volumes (8 Questions)
67Why are Docker volumes needed?
  • A) To make containers start faster by pre-loading data
  • B) Because container filesystems are ephemeral — volumes persist data beyond container deletion
  • C) To connect containers to the internet
  • D) To reduce the size of Docker images
Show Answer & Explanation
✅ Correct Answer: B
When a container is removed, its writable layer is deleted. Any data written to the container's filesystem is gone. Volumes solve this by storing data outside the container lifecycle. Critical for databases, file uploads, logs — anything that must survive container restarts/deletions.
68What are the three types of Docker storage mounts?
  • A) Local, remote, and cloud mounts
  • B) Volumes, bind mounts, and tmpfs mounts
  • C) Read, write, and execute mounts
  • D) Input, output, and error mounts
Show Answer & Explanation
✅ Correct Answer: B
Docker supports three mount types: Volumes (Docker-managed storage), Bind mounts (mount a host directory), and tmpfs mounts (host RAM, not persisted). Each serves a different use case.
69Which command creates a new named Docker volume?
  • A) docker storage create mydata
  • B) docker volume create mydata
  • C) docker mount create mydata
  • D) docker data new mydata
Show Answer & Explanation
✅ Correct Answer: B
docker volume create <name> creates a named volume. You can also let Docker create it automatically by using it in docker run -v mydata:/path — if "mydata" doesn't exist, Docker creates it. Volumes can be shared between multiple containers simultaneously.
70What does a bind mount in Docker do?
  • A) Mounts a Docker-managed named volume into the container
  • B) Mounts a specific directory from the host machine into the container
  • C) Creates a temporary in-memory filesystem inside the container
  • D) Connects two running containers' filesystems together
Show Answer & Explanation
✅ Correct Answer: B
A bind mount maps a specific directory from your host (e.g., /home/user/myapp) to a path inside the container. Both see the same files — changes on either side are instantly visible. This is essential for development workflows where you want live code reload.
71What is a tmpfs mount in Docker?
  • A) A permanent storage location managed by Docker on disk
  • B) A temporary filesystem stored in the host's RAM — data is not persisted to disk
  • C) A network-attached storage volume
  • D) A compressed disk volume for large databases
Show Answer & Explanation
✅ Correct Answer: B
tmpfs (temporary file system) mounts are stored in host RAM, never touching disk. They're faster than disk storage and more secure for sensitive data (passwords, session tokens) since data disappears when the container stops. Used for sensitive scratch space that shouldn't be written to disk.
72Which docker run flag is used to mount a volume or bind mount?
  • A) --storage
  • B) --attach
  • C) -v or --mount
  • D) --disk
Show Answer & Explanation
✅ Correct Answer: C
Both -v and --mount attach storage to containers. -v volume:/path is the short form; --mount type=volume,source=vol,target=/path is the explicit long form. --mount is recommended for new code as it's more readable and supports all options.
73On Linux, where does Docker store named volumes by default?
  • A) /home/docker/volumes/
  • B) /var/lib/docker/volumes/
  • C) /tmp/docker/volumes/
  • D) /etc/docker/volumes/
Show Answer & Explanation
✅ Correct Answer: B
Docker stores all its data under /var/lib/docker/ on Linux — including images, containers, and volumes. Named volumes are in /var/lib/docker/volumes/<volume-name>/_data/. You can verify this with docker volume inspect <name> which shows the "Mountpoint".
74Which command lists all Docker volumes on your system?
  • A) docker storage ls
  • B) docker volume ls
  • C"> docker mount list
  • D) docker disk show
Show Answer & Explanation
✅ Correct Answer: B
docker volume ls lists all volumes (both named and anonymous). The output shows DRIVER and VOLUME NAME. To remove unused volumes, use docker volume prune. To see details about a specific volume (like its host path), use docker volume inspect <name>.
↑ Back to top
🌐 Networking

10. Docker Networks

Docker networking allows containers to communicate with each other and with the outside world. Understanding networks is essential for building multi-service applications.

Why Docker Networking?

When you run multiple containers (e.g., a web app + database + cache), they need to talk to each other. Docker networking provides isolated virtual networks where containers can communicate securely.

Network Drivers

Host Machine bridge (default) App 172.17.0.2 DB 172.17.0.3 user-defined bridge (containers talk by name, not IP) web api db host network Container uses host's network directly No isolation — port 80 IS host port 80 none No network access overlay Multi-host (Swarm)
Fig 10.1 — Docker network drivers: bridge (default), user-defined bridge (DNS), host, none, overlay

Network Drivers Explained

bridge (default)
Containers on the same bridge can communicate via IP. The default bridge uses IP addresses — containers don't automatically know each other's names.
user-defined bridge
When you create your own bridge network, Docker provides automatic DNS — containers can reach each other by container name (e.g., ping db works). Always prefer this over the default bridge.
host
The container shares the host's network stack. localhost inside the container IS the host's localhost. No port mapping needed, but no network isolation.
none
Container has no network connectivity at all. Useful for batch jobs that shouldn't have internet access.
overlay
For Docker Swarm (multi-host) — connects containers running on different host machines across a virtual overlay network.

Working with Networks

  Terminal
# List all networks
docker network ls
# Shows: NETWORK ID, NAME, DRIVER, SCOPE
# Default networks: bridge, host, none

# Create a custom bridge network
docker network create mynetwork

# Create with custom subnet
docker network create --subnet=192.168.10.0/24 mynetwork

# Run containers on the same network
docker run -d --name api --network mynetwork myapi-image
docker run -d --name db --network mynetwork postgres:15

# 'api' can reach 'db' by name: postgres://db:5432/mydb
# This works because Docker's embedded DNS resolves container names

# Connect an existing container to a network
docker network connect mynetwork existing-container

# Disconnect a container from a network
docker network disconnect mynetwork existing-container

# Inspect a network (see connected containers and their IPs)
docker network inspect mynetwork

# Remove a network (all containers must be disconnected first)
docker network rm mynetwork

Container-to-Container Communication

On a user-defined bridge network, containers can communicate using their names:

  Example
# Create a network
docker network create appnet

# Start a database container
docker run -d --name postgres \
  --network appnet \
  -e POSTGRES_DB=mydb \
  -e POSTGRES_PASSWORD=secret \
  postgres:15

# Start your app — connect to 'postgres' by name
docker run -d --name myapp \
  --network appnet \
  -p 3000:3000 \
  -e DATABASE_URL="postgres://postgres:secret@postgres:5432/mydb" \
  myapp:latest
# 'postgres' in the DATABASE_URL resolves to the postgres container's IP
💡 Always Use Named Networks

The default bridge network doesn't support DNS resolution by container name. Always create a named network for your applications. Docker Compose does this automatically — it creates a network per project and adds all services to it.

🧠 Quiz — Section 10: Docker Networks (8 Questions)
75What is the default network driver used when you run a container without specifying a network?
  • A) host
  • B) overlay
  • C) bridge
  • D) macvlan
Show Answer & Explanation
✅ Correct Answer: C
The bridge driver is Docker's default. When you run a container without specifying --network, it attaches to the default bridge network called "bridge". Containers on the default bridge can communicate by IP but not by name. For name-based communication, create a user-defined bridge network.
76On a user-defined bridge network, how can containers find each other?
  • A) Only via the host machine's IP address
  • B) Directly by container name, using Docker's built-in DNS
  • C) Only through Docker Hub's registry service
  • D) They cannot communicate on a user-defined bridge
Show Answer & Explanation
✅ Correct Answer: B
User-defined bridge networks include Docker's embedded DNS server. When a container is named "db", any other container on the same network can resolve "db" to its IP address. This makes service discovery easy: your app can connect to db:5432 without knowing the actual IP.
77What does the host network mode do?
  • A) Completely isolates the container from all network traffic
  • B) Removes the network namespace — the container shares the host's full network stack
  • C) Creates a virtual network between containers on different machines
  • D) Connects the container to Docker Hub's network directly
Show Answer & Explanation
✅ Correct Answer: B
With --network host, the container has no network namespace of its own — it uses the host's network directly. Port 80 inside the container IS port 80 on the host. No -p mapping needed. Benefit: best performance. Drawback: no network isolation, port conflicts possible.
78What is the none network driver used for?
  • A) It is the default networking for all new containers
  • B) Multi-host networking for Docker Swarm
  • C) Completely disabling all networking for a container
  • D) Connecting a container to your Wi-Fi network
Show Answer & Explanation
✅ Correct Answer: C
The none driver removes the container from all networks — it has no network interface (except loopback). Useful for batch processing jobs that shouldn't have internet access, or for maximum security isolation. docker run --network none myimage
79The overlay network driver is primarily used for:
  • A) Single-host container-to-container communication
  • B) Multi-host container communication in Docker Swarm clusters
  • C) Connecting containers to external databases
  • D) Monitoring network traffic between containers
Show Answer & Explanation
✅ Correct Answer: B
The overlay driver creates a distributed virtual network that spans multiple Docker hosts in a Swarm cluster. Containers on different machines can communicate as if on the same local network. Kubernetes uses similar networking concepts with its CNI (Container Network Interface) plugins.
80How do containers on a user-defined bridge network discover each other by name?
  • A) By IP address only (you must hardcode IPs)
  • B) Via Docker's embedded DNS server that resolves container names to IPs
  • C) Via a load balancer configured by the user
  • D) Via Docker Hub's naming service
Show Answer & Explanation
✅ Correct Answer: B
Docker runs an embedded DNS server (at 127.0.0.11) inside each container on user-defined networks. When a container does a DNS lookup for "db", Docker's DNS server returns the IP of the container named "db" on the same network. This is automatic — no configuration needed.
81Which command creates a custom user-defined Docker network?
  • A) docker net create mynetwork
  • B"> docker network new mynetwork
  • C) docker network create mynetwork
  • D) docker create network mynetwork
Show Answer & Explanation
✅ Correct Answer: C
docker network create <name> creates a new bridge network by default. You can specify the driver with --driver (e.g., --driver overlay). Docker Compose automatically creates a network for each compose project, but for standalone containers, you create it manually.
82What does docker network inspect mynetwork show?
  • A) Tests the network speed between containers
  • B) Detailed JSON info about the network including connected containers and their IPs
  • C) Connects a new container to the network
  • D) Creates a new subnet configuration for the network
Show Answer & Explanation
✅ Correct Answer: B
docker network inspect returns detailed JSON metadata: the network's subnet, gateway, connected containers with their IPv4/IPv6 addresses and MAC addresses, and labels. It's the main debugging tool for container networking issues.
↑ Back to top
🎼 Compose

11. Docker Compose

Managing multiple containers with individual docker run commands gets tedious fast. Docker Compose lets you define a multi-container application in a single YAML file and manage everything with one command.

What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. You describe all your services (web app, database, cache, etc.) in a docker-compose.yml file, and then start everything with docker compose up.

docker-compose.yml services: web: build: . ports: ["3000:3000"] db: image: postgres:15 redis: image: redis:7 volumes: dbdata: docker compose up web Node.js :3000 db PostgreSQL :5432 redis Redis :6379 Auto-created Network myproject_default web → db (by name) web → redis (by name) DNS: db, redis, web
Fig 11.1 — Docker Compose reads docker-compose.yml and creates all services + a shared network automatically

Anatomy of a docker-compose.yml

  docker-compose.yml
services:
  # Service 1: Our Node.js web app
  web:
    build: .                          # Build from Dockerfile in current dir
    ports:
      - "3000:3000"                   # host:container port mapping
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgres://db:5432/mydb
    depends_on:
      - db                            # Wait for 'db' to start before 'web'
    networks:
      - appnet
    volumes:
      - ./logs:/app/logs              # Bind mount for logs

  # Service 2: PostgreSQL database
  db:
    image: postgres:15
    environment:
      POSTGRES_DB: mydb
      POSTGRES_PASSWORD: secret
    volumes:
      - dbdata:/var/lib/postgresql/data  # Named volume for persistence
    networks:
      - appnet

  # Service 3: Redis cache
  redis:
    image: redis:7-alpine
    networks:
      - appnet

# Named volumes (persistent data)
volumes:
  dbdata:

# Custom network
networks:
  appnet:
    driver: bridge

Essential Docker Compose Commands

  Terminal
# Start all services (in foreground — shows all logs)
docker compose up

# Start in detached (background) mode
docker compose up -d

# Build images AND start services (force rebuild)
docker compose up --build -d

# Stop all services (keeps containers, volumes, networks)
docker compose stop

# Stop AND remove containers + networks (keeps volumes)
docker compose down

# Stop AND remove EVERYTHING including volumes (⚠️ DATA LOSS!)
docker compose down -v

# View logs for all services
docker compose logs

# Follow logs in real-time
docker compose logs -f

# Follow logs for a specific service
docker compose logs -f web

# List all running services
docker compose ps

# Run a one-off command in a service
docker compose exec web bash
docker compose exec db psql -U postgres

# Scale a service (run multiple instances)
docker compose up --scale web=3 -d

# Rebuild only one service
docker compose up --build web

Environment Variables in Compose

  .env file (alongside docker-compose.yml)
POSTGRES_PASSWORD=supersecret
NODE_ENV=production
API_KEY=abc123
  docker-compose.yml (using .env variables)
services:
  db:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}  # Read from .env
💡 docker compose vs docker-compose

The newer docker compose (with a space, as a Docker plugin) replaced the older docker-compose (with a hyphen, a standalone binary). Both work similarly, but the plugin version is now bundled with Docker and is preferred. Most modern Docker installations include the plugin.

🧠 Quiz — Section 11: Docker Compose (10 Questions)
83What is Docker Compose primarily used for?
  • A) Compressing Docker images before pushing to registries
  • B) Defining and running multi-container Docker applications from a single YAML file
  • C) Managing Docker daemon configuration on production servers
  • D) Monitoring resource usage of running containers
Show Answer & Explanation
✅ Correct Answer: B
Docker Compose allows you to define an entire application stack (web server, database, cache, etc.) in a docker-compose.yml file and manage it with simple commands. Instead of running 5 separate docker run commands with many flags, you run docker compose up.
84What is the default filename Docker Compose looks for?
  • A) docker.yaml
  • B) compose.json
  • C) docker-compose.yml
  • D) Dockerfile.compose
Show Answer & Explanation
✅ Correct Answer: C
Docker Compose looks for docker-compose.yml (or docker-compose.yaml) by default. Newer versions also recognize compose.yml / compose.yaml. You can specify a different file with -f: docker compose -f prod.yml up.
85Which command starts all services defined in docker-compose.yml?
  • A) docker compose start
  • B) docker compose run --all
  • C) docker compose up
  • D) docker compose launch
Show Answer & Explanation
✅ Correct Answer: C
docker compose up creates and starts all services. If images need to be built, it builds them first. It also creates the required networks and volumes. Add -d for background mode. docker compose start exists but only starts existing stopped services (doesn't create new ones).
86What does docker compose down do?
  • A) Pauses all running services temporarily
  • B) Stops and removes the containers and networks created by up, preserving volumes
  • C) Deletes all volumes and images associated with the project
  • D) Stops only the first defined service in the YAML file
Show Answer & Explanation
✅ Correct Answer: B
docker compose down stops containers, removes them, and removes the networks created by Compose. Volumes are preserved by default (to protect data). To also delete volumes, add -v. To also remove images, add --rmi all.
87In a docker-compose.yml, the depends_on field is used to:
  • A) Define resource limits like memory and CPU for a service
  • B) Control startup order — the service won't start until its dependencies are running
  • C) Set environment variables from another service
  • D) Specify the Docker image version to use
Show Answer & Explanation
✅ Correct Answer: B
depends_on controls startup order. If web depends_on db, Compose starts db first, then web. Note: it only waits for the container to START, not for the service inside to be READY. For readiness checks, use healthcheck combined with depends_on: condition: service_healthy.
88How do you view real-time logs for all services in Docker Compose?
  • A) docker compose debug --all
  • B) docker compose logs -f
  • C) docker compose output --stream
  • D) docker logs --compose all
Show Answer & Explanation
✅ Correct Answer: B
docker compose logs shows logs from all services. Adding -f follows/streams the output in real-time (like tail -f). Logs from each service are color-coded and prefixed with the service name. To see logs for one service: docker compose logs -f web.
89What does the build: . key in a docker-compose.yml service definition mean?
  • A) Pull the image from Docker Hub automatically
  • B) Build the image from the Dockerfile in the current directory instead of pulling
  • C) Build the network configuration for the service
  • D) Create a compressed version of the service's filesystem
Show Answer & Explanation
✅ Correct Answer: B
When you specify build: ., Compose builds a Docker image using the Dockerfile in that directory. If you specify both image: and build:, Compose builds and tags the image with the name from image:. Use --build flag to force rebuild: docker compose up --build.
90Which flag starts all Docker Compose services in background mode?
  • A) docker compose up --background
  • B) docker compose up -d
  • C) docker compose up --silent
  • D) docker compose up --detach-all
Show Answer & Explanation
✅ Correct Answer: B
-d stands for "detached" in both docker run and docker compose up. Services run in the background and the terminal is free. Use docker compose logs -f to watch logs afterward. Production deployments typically always use -d.
91In docker-compose.yml, what does ports: - "3000:3000" mean for a service?
  • A) The service needs 3000 MB of memory to run
  • B) Maps host port 3000 to container port 3000 (same as -p 3000:3000)
  • C) The service exposes 3000 internal ports
  • D) Exposes port 3000 only within the internal Docker network
Show Answer & Explanation
✅ Correct Answer: B
ports: - "HOST:CONTAINER" in docker-compose.yml is equivalent to -p HOST:CONTAINER in docker run. It publishes the container port to the host. To expose a port only within the Docker network (not to the host), use expose: instead of ports:.
92In docker-compose.yml, a volumes: section at the top level (not inside a service) defines:
  • A"> Environment variables shared between services
  • B) Named volumes that can be shared between services and persist beyond container restarts
  • C) The number of volume replicas to maintain
  • D) Disk quotas for each service
Show Answer & Explanation
✅ Correct Answer: B
Top-level volumes: in docker-compose.yml declares named volumes. These are created by Docker and persist even when containers are removed. Multiple services can mount the same volume. Example: volumes: dbdata: creates a volume called projectname_dbdata. Removed with docker compose down -v.
↑ Back to top
🗄️ Registry

12. Docker Registry & Docker Hub

Registries are the distribution layer of the Docker ecosystem — they store and serve Docker images. Understanding how to push and pull images is essential for collaboration and deployment.

What is a Docker Registry?

A Docker Registry is a storage system for Docker images. Think of it like GitHub for code — but for container images. You push images to share them, and others pull them to run containers.

Docker Hub (hub.docker.com)
The default public registry. Free for public images. Hosts official images for nginx, postgres, node, python, and thousands more.
Amazon ECR
Elastic Container Registry — AWS's private registry, integrated with ECS and Kubernetes.
Google GCR / Artifact Registry
Google Cloud's registry service.
GitHub Container Registry (ghcr.io)
Store images alongside your GitHub repos.
Self-hosted
Run your own registry using the official registry:2 Docker image.

Image Naming Convention

The full name of a Docker image is: registry/username/repository:tag

  Image name examples
# Official images on Docker Hub (no username prefix)
nginx:latest           # registry: Docker Hub, official image
postgres:15

# User/org images on Docker Hub
myusername/myapp:1.0   # hub.docker.com/r/myusername/myapp

# Images on other registries
gcr.io/myproject/myapp:v2.3      # Google Container Registry
123456789.dkr.ecr.us-east-1.amazonaws.com/myapp:prod  # AWS ECR
ghcr.io/myorg/myapp:latest       # GitHub Container Registry

Working with Docker Hub

  Terminal
# Step 1: Log into Docker Hub
docker login
# Enter your Docker Hub username and password
# Credentials stored in ~/.docker/config.json

# Step 2: Tag your image with your Docker Hub username
docker tag myapp:1.0 yourusername/myapp:1.0
# Now the image is tagged as: yourusername/myapp:1.0

# Step 3: Push to Docker Hub
docker push yourusername/myapp:1.0
# Uploads all layers (only new layers are uploaded)

# Step 4: Anyone can now pull your image
docker pull yourusername/myapp:1.0

# Log out of Docker Hub
docker logout

Running a Private Local Registry

  Terminal
# Start a local registry on port 5000
docker run -d -p 5000:5000 --name registry registry:2

# Tag an image for your local registry
docker tag myapp:1.0 localhost:5000/myapp:1.0

# Push to local registry
docker push localhost:5000/myapp:1.0

# Pull from local registry
docker pull localhost:5000/myapp:1.0

Image Tags & Versioning Best Practices

💡 Tagging Strategy

Use semantic versioning: myapp:1.2.3, myapp:1.2, myapp:1, myapp:latest — all pointing to the same image. This lets users pin to exact versions or floating tags based on their needs.

  Terminal
# Tag the same image with multiple tags
docker tag myapp:1.2.3 myusername/myapp:1.2.3
docker tag myapp:1.2.3 myusername/myapp:1.2
docker tag myapp:1.2.3 myusername/myapp:latest

# Push all tags
docker push myusername/myapp:1.2.3
docker push myusername/myapp:1.2
docker push myusername/myapp:latest

# Or push all tags at once
docker push --all-tags myusername/myapp

Docker Content Trust (DCT)

Docker Content Trust is a security feature that uses digital signatures to verify image authenticity. When enabled, you can only pull images that have been signed — protecting against tampered or malicious images.

  Terminal
# Enable Docker Content Trust (for signing and verification)
export DOCKER_CONTENT_TRUST=1

# Now all push/pull operations require signed images
docker push myusername/myapp:1.0   # Signs the image
docker pull myusername/myapp:1.0   # Verifies the signature
🧠 Quiz — Section 12: Registry & Docker Hub (8 Questions)
93What is Docker Hub?
  • A) A local tool for storing Docker images on your laptop
  • B) The default public cloud registry for storing and sharing Docker images
  • C) A Docker networking management tool
  • D) A container performance monitoring service
Show Answer & Explanation
✅ Correct Answer: B
Docker Hub (hub.docker.com) is the world's largest container registry. It hosts official images (nginx, postgres, python, etc.) and community images. Free tier allows unlimited public repos and limited private repos. When you docker pull nginx, it comes from Docker Hub by default.
94Which command logs you into Docker Hub from the CLI?
  • A) docker auth login hub.docker.com
  • B) docker registry signin
  • C) docker login
  • D) docker connect hub
Show Answer & Explanation
✅ Correct Answer: C
docker login authenticates with Docker Hub (or another registry with docker login registry.url). Credentials are stored in ~/.docker/config.json (as base64 or via a credential store). Required before pushing private images or to avoid Docker Hub pull rate limits.
95What is the correct image name format for pushing to your Docker Hub account?
  • A) image:tag (no username needed)
  • B) hub/image:tag
  • C) yourusername/repository:tag
  • D) registry.image.tag
Show Answer & Explanation
✅ Correct Answer: C
For Docker Hub, the format is username/repository:tag. Only official images (maintained by Docker Inc.) can omit the username prefix (e.g., nginx, postgres). You must tag your local image appropriately before pushing: docker tag myapp yourusername/myapp:1.0.
96Which command uploads a local image to Docker Hub?
  • A) docker send
  • B) docker upload
  • C) docker push
  • D) docker publish
Show Answer & Explanation
✅ Correct Answer: C
docker push username/image:tag uploads the image to the registry. Docker is smart about it — it only uploads layers that don't already exist in the registry. If you change one small file and rebuild, only that new layer is pushed, not the entire image. You must be logged in with docker login first.
97What is a private Docker registry?
  • A) A secret registry only accessible to Docker Inc. employees
  • B) A registry (self-hosted or cloud-managed) where images are not publicly accessible
  • C) A registry that is in the process of being deleted
  • D) A registry stored as a repository on GitHub
Show Answer & Explanation
✅ Correct Answer: B
A private registry stores images that require authentication to access. Options include Docker Hub private repos (paid plan), Amazon ECR, Google Artifact Registry, GitHub Container Registry, Azure ACR, or running the registry:2 Docker image yourself. Private registries are essential for proprietary application code.
98What does the tag node:18-alpine tell you about the image?
  • A) It tags a running container named node with Alpine Linux settings
  • B) Node.js version 18 built on top of Alpine Linux (a minimal, lightweight OS)
  • C) It sets a 18-second timeout for the node container
  • D) It defines resource limits for the container
Show Answer & Explanation
✅ Correct Answer: B
The tag encodes both the software version (18 = Node.js v18) and the OS variant (alpine = Alpine Linux). Alpine is a minimal Linux distribution (~5MB) versus the default Debian/Ubuntu base (~900MB). node:18-alpine is ~120MB vs node:18 at ~900MB — a massive size difference for production images.
99What is Docker Content Trust (DCT)?
  • A) A subscription service for premium Docker Hub storage
  • B) A security feature that uses digital signatures to verify image authenticity before pulling
  • C) A monitoring tool for tracking container performance
  • D) A service that automatically backs up registry images
Show Answer & Explanation
✅ Correct Answer: B
Docker Content Trust (DCT) uses The Update Framework (TUF) to sign and verify images using cryptographic signatures. When enabled (DOCKER_CONTENT_TRUST=1), Docker refuses to pull unsigned images. This protects against man-in-the-middle attacks and ensures you run exactly what the publisher intended.
100What does docker logout do?
  • A) Stops all currently running containers immediately
  • B) Removes stored login credentials from the Docker CLI configuration
  • C) Deletes all locally cached Docker images
  • D) Disconnects the Docker daemon from the internet
Show Answer & Explanation
✅ Correct Answer: B
docker logout removes the stored authentication credentials for the registry (by default Docker Hub) from ~/.docker/config.json. After logout, docker push to private repos will fail until you docker login again. Important for security when working on shared machines.
↑ Back to top
🎉

Congratulations! You've Completed the Docker Tutorial

You've covered all 12 sections and 100 practice questions. You now understand everything from what Docker is to building production-ready containerized applications.

📖 Official Docker Docs 🐳 Docker Hub 🧪 Play With Docker