Skip to content

Enggvault/Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Complete Docker Notes

Welcome to the Complete Docker Notes. This guide is designed to teach you Docker from a beginner to an advanced level using practical examples, diagrams, tables, commands, and real development workflows.

Whether you are running Linux, Windows, or macOS, this handbook covers the exact steps, syntax differences, and architectural variations you need to know.

Table of Contents

  1. What is Docker?
  2. Architecture Differences
  3. Docker Desktop vs Docker Engine
  4. Complete Installation Guide
  5. Running Docker Without Sudo
  6. Filesystem and Volume Differences
  7. Running Containers
  8. Writing Dockerfiles and Creating Images
  9. Apple Silicon and Multi-Platform Images
  10. Docker Compose and Multi-Container Apps
  11. Networking and Environment Variables
  12. Logs, Debugging, and Health Checks
  13. Security and Optimizing Images
  14. Cleaning Docker Resources Safely
  15. Troubleshooting

1. What is Docker?

Docker is a platform that allows you to package an application and all its dependencies into a standardized unit called a container. Containers are lightweight, isolated environments that run consistently across any machine.

  • Image: A read-only template with instructions for creating a Docker container (like a blueprint).
  • Container: A runnable instance of an image.
  • Volume: A persistent storage mechanism for container data.
  • Network: A communication layer for containers to talk to each other.

2. Architecture Differences

How Docker runs depends entirely on your operating system.

Linux

Docker runs directly on the Linux kernel. It uses namespaces and cgroups provided by the kernel to isolate processes. This is the most native and performant way to run Docker.

Windows

Windows uses Docker Desktop, which relies on WSL 2 (Windows Subsystem for Linux) or Hyper-V to run a lightweight Linux virtual machine. The containers run inside this WSL 2 backend, feeling seamless to the user.

macOS

macOS also uses Docker Desktop. Since macOS is Unix but not Linux, Docker runs a lightweight Linux VM in the background (using Hypervisor.framework) to execute Linux containers.

graph TD
    subgraph Linux OS
        kernel[Linux Kernel]
        C1[Container 1]
        C2[Container 2]
        kernel --> C1
        kernel --> C2
    end
    subgraph Windows / macOS
        host[Host OS]
        vm[Lightweight Linux VM / WSL2]
        WC1[Container 1]
        WC2[Container 2]
        host --> vm
        vm --> WC1
        vm --> WC2
    end
Loading

3. Docker Desktop vs Docker Engine

When installing Docker, you have two main choices:

Feature Docker Engine Docker Desktop
Interface CLI only GUI + CLI
Availability Linux Windows, macOS, Linux
Core Tech Direct kernel access Virtual Machine / WSL 2
Resource Limits Set via Linux cgroups Configured via GUI settings
Kubernetes Needs external tools (Minikube/K3s) Built-in (1-click install)
Extensions No Yes
Licensing Free / Open Source Free for personal/small business, Paid for large enterprises

4. Complete Installation Guide

Below are the workflows to install Docker across all major operating systems. Note: Always verify commands with official documentation, as repository URLs can update.

Linux

Workflow A1: Install Docker on Fedora

  • Starting situation: Fresh Fedora install.
  • Commands:
    sudo dnf -y install dnf-plugins-core
    sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
    sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    sudo systemctl enable --now docker
  • Expected result: Docker service is running and enabled on boot.

Workflow A2: Install Docker on Ubuntu

  • Starting situation: Fresh Ubuntu install.
  • Commands:
    # Add Docker's official GPG key:
    sudo apt-get update
    sudo apt-get install ca-certificates curl
    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc
    
    # Add the repository to Apt sources:
    echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
      $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
      sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    sudo systemctl enable --now docker

Workflow A3: Install Docker on Debian

  • Starting situation: Fresh Debian install.
  • Commands: Follow the exact same commands as Ubuntu, but the script automatically detects the Debian codename.

Workflow A4: Install Docker on Arch Linux

  • Starting situation: Fresh Arch install.
  • Commands:
    sudo pacman -S docker docker-compose docker-buildx
    sudo systemctl enable --now docker

Workflow A5: Install Docker on Manjaro

  • Starting situation: Fresh Manjaro install.
  • Commands: Identical to Arch Linux (sudo pacman -S docker docker-compose docker-buildx).

Workflow A6: Install Docker on openSUSE

  • Starting situation: Fresh openSUSE install.
  • Commands:
    sudo zypper install docker docker-compose python3-docker-compose
    sudo systemctl enable --now docker

Workflow A7: Install Docker on RHEL

  • Starting situation: Fresh RHEL system.
  • Commands:
    sudo yum install -y yum-utils
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    sudo systemctl enable --now docker

Workflow A8: Install Docker on Rocky Linux

  • Commands: Identical to RHEL setup.

Workflow A9: Install Docker on AlmaLinux

  • Commands: Identical to RHEL setup.

Workflow A10: Install Docker on CentOS Stream

  • Commands: Identical to RHEL setup.

Workflow A11: Install Docker on Linux Mint

  • Starting situation: Fresh Linux Mint install.
  • Commands: Linux Mint is based on Ubuntu. Use the Ubuntu installation workflow.

Workflow A12: Install Docker on Pop!_OS

  • Starting situation: Fresh Pop!_OS install.
  • Commands: Pop!_OS is based on Ubuntu. Use the Ubuntu installation workflow.

Workflow A13: Install Docker on Kali Linux

  • Starting situation: Fresh Kali Linux install.
  • Commands: Kali Linux is based on Debian. Use the Debian installation workflow.

Linux Distro Quick Reference

Task Fedora Ubuntu/Debian Arch/Manjaro openSUSE RHEL/Rocky/Alma/CentOS
Install Command sudo dnf install docker-ce... sudo apt-get install docker-ce... sudo pacman -S docker... sudo zypper install docker... sudo yum install docker-ce...
Start Service sudo systemctl start docker sudo systemctl start docker sudo systemctl start docker sudo systemctl start docker sudo systemctl start docker

Windows

Workflow A14: Install Docker on Windows 10

  • Starting situation: Windows 10 Pro, Enterprise, or Home (Build 19044+).
  • Commands / Steps:
    1. Install WSL 2 by opening PowerShell as Administrator and running: wsl --install
    2. Restart the computer.
    3. Download and install Docker Desktop for Windows from the official website.
    4. Ensure "Use WSL 2 instead of Hyper-V" is checked in Docker Desktop settings.
  • Expected result: Docker icon appears in system tray, and docker version works in PowerShell.

Workflow A15: Install Docker on Windows 11

  • Starting situation: Windows 11 system.
  • Commands: Same exact steps as Windows 10 using WSL 2.

Workflow A16: Install Docker on Windows Server

  • Notes: Docker Desktop is not supported on Windows Server. You must install the Docker EE (Enterprise Edition) or use the Mirantis Container Runtime. Alternatively, use WSL 2 natively on Server 2022 if configured for container development.

macOS

Workflow A17: Install Docker on macOS Intel

  • Starting situation: Mac with Intel processor.
  • Steps:
    1. Download Docker Desktop for Mac (Mac with Intel chip).
    2. Double-click the .dmg and drag Docker to Applications.
    3. Open Docker from Applications to complete setup.

Workflow A18: Install Docker on macOS Apple Silicon

  • Starting situation: Mac with M1/M2/M3/M4 chip.
  • Steps:
    1. Download Docker Desktop for Mac (Mac with Apple chip).
    2. Install Rosetta 2 if required for older x86 tools: /usr/sbin/softwareupdate --install-rosetta --agree-to-license
    3. Drag Docker to Applications and open it.
    4. Enable "Use Rosetta for x86/amd64 emulation" in Docker Desktop Settings -> General for better performance when running Intel images.

5. Running Docker Without Sudo

By default, the Docker daemon binds to a Unix socket owned by the root user.

Linux

To run Docker without typing sudo every time:

sudo usermod -aG docker $USER
newgrp docker
  • Explanation: Adds your current user to the docker group.

Windows & macOS

You do not need sudo or administrator privileges to run Docker commands on Windows and macOS. Docker Desktop manages the permissions and user access to the Docker daemon automatically.


6. Filesystem and Volume Differences

Bind mounts allow you to map a folder on your host machine to a folder inside the container. Because operating systems format paths differently, the commands change depending on your OS and terminal.

OS Example Path
Linux /home/user/project
Windows C:\Users\User\project
macOS /Users/user/project

Linux / macOS / WSL 2 (Bash)

docker run -v "$(pwd)":/app my-image

Windows (PowerShell)

docker run -v "${PWD}:/app" my-image

Windows (CMD)

docker run -v "%cd%:/app" my-image
  • Note: On Windows, Docker Desktop automatically translates Windows paths (C:\...) to Unix paths for the container.

7. Running Containers

To run a basic container:

docker run -d --name web -p 8080:80 nginx
  • -d: Detached mode (runs in background).
  • --name: Names the container.
  • -p 8080:80: Maps port 8080 on your host to port 80 inside the container.

Useful Commands

  • List running containers: docker ps
  • List all containers: docker ps -a
  • Stop a container: docker stop web
  • Remove a container: docker rm web
  • Execute a shell inside a container:
    docker exec -it web /bin/sh

8. Writing Dockerfiles and Creating Images

A Dockerfile is a text document that contains the commands to assemble an image.

Example: Node.js / Next.js / NestJS API

# Use an official runtime as a parent image
FROM node:20-alpine

# Set working directory
WORKDIR /app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the application
COPY . .

# Expose port and start app
EXPOSE 3000
CMD ["npm", "run", "start"]

Build the image:

docker build -t my-node-app .

9. Apple Silicon and Multi-Platform Images

Apple Silicon (M1/M2/M3/M4) uses the ARM64 architecture. Most traditional servers use AMD64 (x86_64).

If you build an image on an M1 Mac, it is ARM64 by default. If you deploy it to an AWS EC2 Intel instance, it will fail.

Buildx

Use docker buildx to build for specific architectures.

Build for AMD64 (from an Apple Silicon Mac):

docker buildx build --platform linux/amd64 -t my-app-amd64 .

Build for ARM64:

docker buildx build --platform linux/arm64 -t my-app-arm64 .

Docker Desktop on Mac uses Rosetta 2 to emulate AMD64 images, but running native ARM64 images is always faster.


10. Docker Compose and Multi-Container Apps

Docker Compose is a tool for defining and running multi-container applications using a docker-compose.yml file.

Example Stack: Python FastAPI + PostgreSQL + Redis + MongoDB + Kafka

version: '3.8'

services:
  api:
    build: ./api
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql://user:pass@db:5432/appdb
      - REDIS_URL=redis://redis:6379/0
      - MONGO_URL=mongodb://root:example@mongodb:27017/
      - KAFKA_BROKER=kafka:9092
    depends_on:
      - db
      - redis
      - mongodb
      - kafka

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: appdb
    volumes:
      - pgdata:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine

  mongodb:
    image: mongo:6-jammy
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - mongodata:/data/db

  kafka:
    image: bitnami/kafka:latest
    environment:
      - KAFKA_CFG_NODE_ID=0
      - KAFKA_CFG_PROCESS_ROLES=controller,broker
      - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
      - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:9093
      - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER

volumes:
  pgdata:
  mongodata:

Commands:

  • Start stack: docker compose up -d
  • Stop stack: docker compose down

Note: On older installations, the command was docker-compose (with a hyphen). The modern standard is docker compose (with a space).


11. Networking and Environment Variables

Containers on the same Docker network can talk to each other using their service names as hostnames. In the YAML above, the api service connects to the database using the hostname db (postgresql://user:pass@db:5432/appdb).

Environment Variables

You can pass environment variables in several ways:

  1. Direct in Docker Compose (environment: block)
  2. Using an .env file and env_file: .env
  3. Command line: docker run -e DB_USER=admin nginx

12. Logs, Debugging, and Health Checks

Reading Logs

  • Container logs: docker logs <container_name>
  • Follow logs in real-time: docker logs -f <container_name>
  • Docker Compose logs: docker compose logs -f

Health Checks

Ensure a container is actually ready to receive traffic, not just "running".

  db:
    image: postgres:15
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user -d appdb"]
      interval: 10s
      timeout: 5s
      retries: 5

13. Security and Optimizing Images

  1. Use Alpine or Minimal Base Images: e.g., python:3.11-alpine or node:20-slim.
  2. Don't run as Root:
    RUN adduser -D myuser
    USER myuser
  3. Use Multi-Stage Builds: Compile in one stage, copy binary to a clean, small stage.
  4. .dockerignore: Always include a .dockerignore file (like .gitignore) to exclude node_modules, .git, and environment variables files from your image context.

14. Cleaning Docker Resources Safely

Docker can consume gigabytes of disk space over time.

  • Remove unused containers, networks, images (dangling):
    docker system prune
  • Remove EVERYTHING unused (including stopped containers and unused volumes):
    docker system prune -a --volumes

15. Troubleshooting

Problem: Docker daemon not running

  • Linux Fix: sudo systemctl restart docker
  • Windows Fix: Open Docker Desktop application. Ensure WSL 2 is installed (wsl --update).
  • macOS Fix: Open Docker Desktop application from Applications folder.

Problem: Permission Denied (Cannot connect to the Docker daemon)

  • Linux Fix: Run sudo usermod -aG docker $USER and log out/log back in.
  • Windows/macOS Fix: Ensure Docker Desktop is actually running.

Problem: Bind mount is empty or not syncing

  • Linux Fix: Check SELinux contexts (Fedora/RHEL). Add :Z to the end of the mount: -v $(pwd):/app:Z.
  • Windows Fix: Check paths. Use PowerShell ${PWD} instead of $(pwd). Ensure File Sharing is enabled in Docker Desktop.
  • macOS Fix: Ensure Docker Desktop has permission to access the folder (Settings -> Resources -> File sharing).

Problem: Image fails to run on EC2 / Server (Exec format error)

  • Fix: You built an ARM64 image on an Apple Silicon Mac, but deployed it to an AMD64 server. Rebuild with --platform linux/amd64.

Problem: WSL 2 / Hyper-V issues on Windows

  • Windows Fix: Open an elevated PowerShell and run:
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    wsl --set-default-version 2
    Ensure Virtualization is enabled in your BIOS/UEFI.

Problem: DNS Issues within Container

  • Linux Fix: Edit /etc/docker/daemon.json and add {"dns": ["8.8.8.8", "8.8.4.4"]}, then restart Docker.
  • Windows/macOS Fix: Change DNS settings in the host OS or configure within the Docker Desktop settings GUI under Network.

End of Guide

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors