What is Docker and How It Works Step by Step Guide

Hello friends, welcome to shrash studio learning, in this article we will learn Docker from scratch — what it is, why every modern software team uses it, and how it is different from virtual machines. Whether you are a full-stack developer, backend engineer, or aspiring DevOps engineer, Docker has become a non-negotiable skill. We'll cover it with simple English, visual diagrams, real commands you can run today, and a clear mental model that will stick with you.

The "Works on My Machine" Problem

Imagine a team of five developers building the same application. One is on macOS, two on Windows, one on Ubuntu. The app needs Node.js v16, Redis v7, PostgreSQL v14, and half a dozen system libraries.

A TYPICAL ONBOARDING NIGHTMARE

Developer A (Mac)

Node 20 installed globally, app breaks on Node 16 features.

Developer B (Windows)

Redis install fails — needs WSL, new command syntax.

Developer C (Ubuntu)

Different libssl version crashes the postgres client.

Production Server

"It worked on staging!" — different kernel, missing packages.

The result is hours of Slack messages, mismatched versions, random bugs that appear only on one machine, and slow onboarding for every new hire. This is the famous "but it works on my machine" problem — and Docker was invented specifically to end it.

What Docker Really Is

Docker packages your application's code plus all its dependencies — the runtime, libraries, config files, environment variables — into a single portable unit called a container. That container runs identically on any machine that has Docker installed, no matter which host OS it is.

Portable: Ship the same container to Mac, Windows, Linux, AWS, Azure.
Lightweight: Starts in seconds, measured in megabytes, not gigabytes.
Isolated: Two containers on the same machine can't interfere with each other.
Reproducible: Every teammate runs exactly the same environment.

Images vs Containers — The Class/Object Analogy

This is the one concept every Docker beginner must nail down. Docker has two closely related ideas — Images and Containers.

THE CLASS / OBJECT ANALOGY

Docker Image

The blueprint. A read-only snapshot containing code, dependencies, and setup instructions.
Like a Class in OOP.

Docker Container

A running instance. A live, isolated process started from an image.
Like an Object in OOP.

One image can spawn many containers, exactly like one class can create many objects. When you want to share your project, you share the image — not the running container.

Visual Diagram — Image to Container Flow

From Dockerfile to Running Container
1. Dockerfile — a text file with build instructions
2. docker build → creates a reusable Image
3. Image pushed to Docker Hub (optional) for sharing
4. docker pull → teammate downloads the same image
5. docker run → image becomes a live Container

Docker Architecture — Client, Daemon, and Hub

When you type docker run ..., there's more happening than it seems. Docker has three cooperating pieces.

DOCKER'S THREE KEY PIECES

Docker Client

The CLI or Docker Desktop UI you interact with. Sends commands.

Docker Daemon (dockerd)

The background service that actually builds, runs, and manages containers.

Docker Hub / Registry

Cloud repository of public and private images. Like GitHub, but for Docker images.
What happens when you type docker run nginx
[You] type the command in the terminal
[Client] sends the request to the Daemon
[Daemon] checks locally — image found? If not, pull from Hub
[Hub] delivers the image layers
[Daemon] creates & starts the container
[You] see the output in the terminal

Installing Docker — Just Use Docker Desktop

The easiest way to start is Docker Desktop (Windows, macOS, Linux). It bundles the daemon, the CLI, and a friendly UI to inspect images and containers visually. Download from docker.com, install, and you're ready.

Quick test: Open a terminal and run docker --version. If it prints a version number, you're good to go.

Your First Docker Command — Hello World

The classic starter. The hello-world image is a tiny test that prints a confirmation message and exits.


# Pull the image from Docker Hub
docker pull hello-world

# Run a container from that image
docker run hello-world

# Expected output (shortened):
# "Hello from Docker!
#  This message shows that your installation appears to be working correctly."

If you see the message, Docker is successfully installed and communicating with the daemon.

Run Ubuntu Interactively — A Real Dev Environment in Seconds

Want a full Linux shell right now, without installing a VM? One command:


docker run -it ubuntu

# You are now inside a fresh Ubuntu container
# Try these inside it:
# ls
# apt update && apt install -y curl
# mkdir demo && cd demo
# exit

The -it flag opens an interactive terminal. You get a clean Ubuntu shell with nothing installed from your host. Install anything — it lives only in the container. Type exit and the container stops. Your host machine stays untouched.

Essential Docker Commands Cheatsheet


docker pull <image>                # download an image
docker images                       # list local images
docker run <image>                  # start a container
docker run -it <image>              # run interactively
docker run -d <image>               # run in background (detached)
docker run -p 8080:80 <image>       # map host port 8080 to container 80

docker ps                           # list running containers
docker ps -a                        # list all containers incl. stopped
docker stop <container-id>          # stop a container
docker start <container-id>         # start a stopped container
docker rm <container-id>            # delete a container
docker rmi <image-id>               # delete an image

docker logs <container-id>          # view container logs
docker exec -it <container-id> bash # shell into running container

Docker vs Virtual Machines — The Biggest Question

This is where most beginners get confused. Docker containers and Virtual Machines both isolate environments, but they work in very different ways.

ARCHITECTURE COMPARISON — STACKED VIEW

Virtual Machine Stack

Guest App
Libraries / Dependencies
Guest Operating System (full OS)
Hypervisor
Host Operating System
Physical Hardware
Heavy — GBs per VM, slow boot

Docker Container Stack

Container App
Libraries / Dependencies
Docker Engine
Host Operating System (shared kernel)
Physical Hardware
Light — MBs per container, seconds to boot

The key difference — a Virtual Machine carries its own full operating system, including its own kernel. A Docker container shares the host's kernel and only packages the application + its dependencies. That one design choice is why Docker containers are so much lighter and faster.

Side-By-Side Comparison Table

Feature Docker Container Virtual Machine
What's virtualized Application only Full operating system
Size Tens to hundreds of MB Several GB
Startup time Seconds (often < 1s) Minutes
Resource usage Light — shares host kernel Heavy — own kernel and OS
Isolation level Process-level Full OS-level
Best for Microservices, dev environments, CI/CD Running a different OS entirely, legacy apps
Density per host Hundreds possible Typically 10s
On Windows and macOS Docker Desktop runs a tiny Linux VM behind the scenes because containers need a Linux kernel. But this VM is shared by all your containers — so you only pay the OS cost once, not per container like full VMs.

Where Docker Gives You Superpowers

REAL-WORLD DOCKER WINS

Same env for everyone

New joiner sets up the entire stack in 5 minutes with one command.

Version isolation

Run Node 16 and Node 20 side by side without conflicts.

Easy databases

Spin up Postgres, Redis, or MongoDB in one command, throw away after.

CI/CD pipelines

Same container in tests, staging, and production — no surprises.

Microservices

Each service in its own container, easy to scale independently.

Clean experiments

Try risky installs inside a container, delete it when done.

Common Beginner Mistakes

1. Confusing images with containers. An image is static. A container is a running instance. docker images lists images, docker ps lists containers.

2. Storing data inside containers. Containers are ephemeral — when deleted, their data is gone. For persistent data (databases, uploads), use volumes with -v.

3. Running everything as root inside the container. It's a security risk. Use USER in your Dockerfile to drop privileges.

4. Gigantic images. Using a full Ubuntu base for a tiny Node service can produce 1+ GB images. Start from lightweight bases like node:alpine or python:slim.

5. Ignoring .dockerignore. Without it, your entire node_modules or .git folder ends up in the image. Always add a .dockerignore.

6. Running containers without port mapping. If your app serves on port 80 inside the container but you didn't use -p 8080:80, you can't reach it from the host.

The Final Verdict

Docker is not optional anymore — it's the lingua franca of modern software.

Every cloud platform, CI/CD tool, microservice framework, and deployment system assumes you know Docker. Spend a weekend with the commands in this article, run a few containers, break them, delete them, and rebuild. That hands-on practice will teach you more than a hundred tutorials. Docker is easier than it looks once you internalise images = blueprints, containers = running instances, daemon = the engine doing the work.

Summary

Docker solves the "works on my machine" problem by packaging your application and all its dependencies into a single portable unit called a container. Containers are created from static images — think of images as classes and containers as objects. The Docker daemon does the heavy lifting while the Docker client sends commands, and Docker Hub is where images are shared.

Compared to Virtual Machines, containers share the host OS kernel, which makes them dramatically smaller (MBs vs GBs) and faster (seconds vs minutes) to start. That is why modern teams choose Docker for local development, CI/CD pipelines, microservices, and production deployments. Learn the five-or-six core commands — pull, run, ps, stop, rm, logs — and you are already 80% productive. The rest of Docker (Dockerfiles, Compose, networking, volumes) builds on that same foundation.

Concept Key Takeaway
Core problem "Works on my machine" environment inconsistency
Container Packaged app + dependencies, portable and isolated
Image Read-only blueprint used to create containers
Image : Container = Class : Object
Docker Client The CLI / UI that sends commands
Docker Daemon Background service doing the real work
Docker Hub Public / private registry of images (like GitHub)
Container vs VM Containers share host kernel; VMs have full OS
Container size MBs vs GBs for VMs
Startup time Seconds vs minutes for VMs
Must-know commands pull, run, ps, stop, rm, logs, exec
Biggest mistake Storing persistent data inside containers
Pro tip Use alpine/slim base images for small size
Why it matters Docker is table-stakes in modern software

Chakrapani U

Hi, I’m Chakrapani Upadhyaya, an IT professional with 15+ years of industry experience. Over the years, I have worked on web development, enterprise applications, database systems, and cloud-based solutions. Through this blog, I aim to simplify complex technical concepts and help learners grow from beginners to confident, industry-ready developers.

Previous Post Next Post

نموذج الاتصال