๐Ÿณ

Docker Basics

๐Ÿง‘โ€๐ŸŽ“ Apprentice

What problem does Docker solve?

Imagine this scenario (which happens ALL the time):

SituationWhat you hear
Developer"It works perfectly on my machine"
Production serverError 500, crash, missing dependencies
New developer"I've spent 2 days setting up the environment"
DevOps"What Node version did you use? And Python?"

Docker solves this by packaging your application WITH EVERYTHING it needs: code, runtime, libraries, configuration. If it works in Docker, it works anywhere.


๐Ÿ–ฅ๏ธ Virtual Machines vs Containers

    VIRTUAL MACHINE                    CONTAINER (DOCKER)
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”               โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚      App        โ”‚               โ”‚ App1 โ”‚ โ”‚ App2 โ”‚ โ”‚ App3 โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚  Libs/Binaries  โ”‚               โ”‚     Libs/Binaries      โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚   Guest OS      โ”‚  โ† Full OS    โ”‚     Docker Engine      โ”‚
    โ”‚  (Ubuntu 8GB)   โ”‚               โ”‚       (~100MB)         โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚   Hypervisor    โ”‚               โ”‚        Host OS         โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค               โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚    Host OS      โ”‚               โ”‚       Hardware         โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ”‚   Hardware      โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    ๐Ÿข Heavy: 10-20 GB per VM         ๐Ÿš€ Light: 100-500 MB
    โฑ๏ธ Boot: 1-2 minutes              โฑ๏ธ Boot: 1-2 seconds
    ๐Ÿ“ฆ Full isolation                 ๐Ÿ“ฆ Process-level isolation

๐Ÿ’ฐ Resource savings

MetricTraditional VMDocker Container
RAM per instance1-8 GB50-500 MB
Disk10-40 GB100 MB - 2 GB
Startup time30s - 2min1-5 seconds
Apps per server5-10 VMs50-100+ containers
CPU overhead15-20%1-5%

๐Ÿ’ก On a 32GB RAM server: you can run ~4 VMs or ~50 containers doing the same thing.


Why is it essential for the modern developer?

UseBenefit
Local developmentSame environment as production
OnboardingNew dev productive in minutes, not days
CI/CDTests in identical environment to production
MicroservicesEach service in its container
ExperimentingTry technologies without polluting your system
ScalabilityKubernetes orchestrates thousands of containers

๐Ÿ”ง Docker alternatives

Docker popularized containers, but it's not the only option:

TechnologyCreatorCharacteristics
DockerDocker IncMost popular, Docker Hub, Docker Desktop
PodmanRed HatDaemonless, rootless, Docker CLI compatible
containerdCNCFRuntime used by Docker and Kubernetes
LXC/LXDCanonicalSystem containers (more like VMs)
ColimaOpen SourceDocker on macOS/Linux without Docker Desktop

๐Ÿ’ก Tip: If you use Linux and want to avoid Docker Desktop, Podman is the most popular alternative. Commands are almost identical: podman run instead of docker run.


Key concepts

ConceptWhat it isAnalogy
ImageImmutable templateWritten recipe
ContainerInstance of an imagePrepared dish
DockerfileInstructions to create imageRecipe steps
RegistryImage repositoryRecipe library
VolumePersistent dataIngredients you save
NetworkCommunication between containersConnection between kitchens

Installation

SystemRecommended optionLightweight alternative
macOSbrew install --cask dockerColima + Docker CLI
WindowsDocker Desktop (WSL2)Podman Desktop
LinuxDocker EnginePodman (daemonless)
# Verify installation
docker --version
docker run hello-world

Container lifecycle

    Dockerfile     docker build      Image         docker run      Container
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถ  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถ  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    (recipe)                        (frozen                         (served)
                                    dish)

    docker push                     docker pull
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถ Docker Hub โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                 (registry)

Your first container

# Test that Docker works
docker run hello-world

# Run Ubuntu interactively
docker run -it ubuntu bash

# Inside the container:
cat /etc/os-release  # Ubuntu!
exit                 # Exit

Essential commands

# See running containers
docker ps

# See all containers
docker ps -a

# See downloaded images
docker images

# Run with interactive shell
docker run -it ubuntu bash

# Stop container
docker stop <container_id>

# Remove container
docker rm <container_id>

Practical example: Node.js

# Run Node.js 20
docker run -it node:20 node

# Now you can write JavaScript
> console.log("Hello from Docker!")

Your first Dockerfile

FROM node:20
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
# Build image
docker build -t my-app .

# Run
docker run -p 3000:3000 my-app

Practice

โ†’ Docker Hello World โ€” Your first container


Useful links