
You have probably heard this one before. A developer writes code on their laptop, tests it, and it works perfectly. They push it to GitHub. Their teammate pulls it down, runs it — and it crashes instantly.
Why? Different operating systems. Different library versions. Different environment settings. The code was fine — the environment was not.
This problem caused countless hours of frustration for decades. Then Dockerarrived and changed everything.
Docker packages your entire application — code, libraries, dependencies, and settings — into a single, portable unit called a container. It runs the same way on your laptop, your teammate's machine, and any server in the world.
In this beginner-friendly guide, you will learn what Docker is, how it works, how to use it step by step, and get a complete Docker command cheat sheet you can come back to any time.
Docker is an open source platform that lets you build, ship, and run applications inside containers.
A container is a lightweight, isolated environment that contains everything your application needs to run — code, runtime, libraries, and configurations. It is completely independent of whatever machine it runs on.
Docker was released in 2013 and quickly became the standard for modern software deployment. Today, it powers everything from small side projects to production systems at Netflix, Spotify, PayPal, and thousands of other companies.
Before shipping containers were invented, moving goods around the world was chaotic. Every item had a different shape, size, and handling requirement. Ships, trains, and trucks all had to be custom-loaded.
Then someone invented the standard shipping container — a metal box with the same dimensions, no matter what is inside. Ships, cranes, and trucks all know how to handle it.
Docker containers work the same way. Instead of packing crates with different sizes, they provide a standard unit that runs anywhere. The application inside does not care whether it is on your laptop, on AWS, or on a server in another country. The container runs the same everywhere.
Docker containers are often compared to virtual machines (VMs). Both provide isolation, but they work very differently.
| Aspect | Docker Container | Virtual Machine |
|---|---|---|
| Size | Megabytes (MB) | Gigabytes (GB) |
| Startup Time | Seconds | Minutes |
| Uses Host OS Kernel | Yes | No — runs full OS |
| Resource Usage | Low | High |
| Isolation | Process-level | Full hardware-level |
Rule of thumb: Use Docker for applications. Use VMs when you need full hardware-level isolation or a completely different OS.
A read-only template that contains your application and its dependencies. Think of it as a recipe or blueprint. You build an image once and use it many times.
A running instance of an image. If an image is a class, a container is an object. If an image is a recipe, a container is the cooked meal.
A text file with instructions for building an image. It specifies the base image, files to copy, dependencies to install, and commands to run.
A persistent storage that lives outside a container. Containers are temporary — when they stop, their data is gone. Volumes keep data safe between restarts.
The official registry for Docker images. Similar to GitHub but for containers. You can pull pre-built images (like nginx, postgres, node) or push your own.
A tool for defining and running multi-container applications with a single YAML file. Instead of running five docker run commands, you run one docker compose up.
Docker runs on Windows, macOS, and Linux. The installation is simple.
Download Docker Desktop from docker.com/products/docker-desktop. It includes Docker Engine, CLI, and Docker Compose in one installer.
If you see a friendly "Hello from Docker!" message, you are ready to go.
Let's run a real container — an Nginx web server — with one command.
Let's break it down:
Now open http://localhost:8080 in your browser. You will see the default Nginx welcome page. You just ran a web server without installing anything.
Running existing images is useful, but the real power of Docker is packaging your own apps. Let's build a simple Node.js app.
Create a file called app.js:
Create a file called Dockerfile (no extension):
Here is what each line does:
The -t flag tags the image with a name. The . tells Docker to use the current directory as context.
Open http://localhost:3000. You will see "Hello from Docker!". You just built and ran your own container.
Real applications usually need more than one container — for example, a web app plus a database. Docker Compose lets you define them all in one file.
Create a file called docker-compose.yml:
Now run everything with one command:
Docker Compose starts your app and the database, connects them, and manages volumes automatically. This is how modern development teams run entire stacks with one command.
Containers are temporary. When you delete a container, everything inside it disappears. For databases, logs, and user uploads, that is a problem.
Volumes solve this. A volume is storage that lives outside the container and survives restarts.
Create a volume:
Mount it to a container:
Now even if the container is deleted, the data in my-data remains.
Here is your complete reference. Bookmark this section — you will come back to it often.
| Command | Description |
|---|---|
| docker images | List all local images |
| docker pull nginx | Download an image from Docker Hub |
| docker build -t name . | Build an image from a Dockerfile |
| docker rmi image_id | Delete an image |
| docker tag source target | Tag an image with a new name |
| docker push user/image | Upload an image to Docker Hub |
| docker history image_id | View the layers of an image |
| Command | Description |
|---|---|
| docker run image | Create and start a container |
| docker run -d image | Run in the background (detached) |
| docker run -it image bash | Run interactively with a shell |
| docker run -p 8080:80 image | Map host port to container port |
| docker ps | List running containers |
| docker ps -a | List all containers (including stopped) |
| docker start container_id | Start a stopped container |
| docker stop container_id | Stop a running container |
| docker restart container_id | Restart a container |
| docker rm container_id | Delete a container |
| docker logs container_id | View container logs |
| docker exec -it id bash | Open a shell inside a running container |
| docker inspect container_id | View detailed container info |
| docker stats | Live CPU, memory, and I/O usage |
| docker top container_id | See running processes inside a container |
| Command | Description |
|---|---|
| docker volume create name | Create a new volume |
| docker volume ls | List all volumes |
| docker volume inspect name | See volume details |
| docker volume rm name | Delete a volume |
| docker volume prune | Remove unused volumes |
| Command | Description |
|---|---|
| docker network ls | List all networks |
| docker network create name | Create a custom network |
| docker network inspect name | View network details |
| docker network rm name | Delete a network |
| Command | Description |
|---|---|
| docker compose up | Start all services |
| docker compose up -d | Start in the background |
| docker compose down | Stop and remove all services |
| docker compose ps | List services |
| docker compose logs | View logs from all services |
| docker compose logs -f web | Follow logs from a specific service |
| docker compose build | Rebuild images |
| docker compose exec web bash | Open a shell inside a service |
| docker compose restart | Restart all services |
| Command | Description |
|---|---|
| docker system df | Show disk usage |
| docker system prune | Remove unused containers, networks, images |
| docker system prune -a | Aggressive cleanup — removes all unused images |
| docker container prune | Remove all stopped containers |
| docker image prune | Remove unused images |
| Instruction | Purpose |
|---|---|
| FROM | Base image to start from |
| WORKDIR | Set working directory inside container |
| COPY | Copy files from host to container |
| ADD | Like COPY but also handles URLs and tarballs |
| RUN | Execute a command during build |
| CMD | Default command when container starts |
| ENTRYPOINT | Fixed command that cannot be overridden easily |
| ENV | Set environment variables |
| ARG | Build-time variables |
| EXPOSE | Document which port the app uses |
| VOLUME | Declare a mount point for external storage |
| USER | Switch to a non-root user |
| LABEL | Add metadata (author, version, description) |
alpine variants to reduce sizenode_modules, .git, and other junklatest in productiondocker scan or Trivy-d: Without it, your terminal gets stuck running the containerdocker run, docker build, docker ps, docker logs.Docker has become a fundamental skill for developers, DevOps engineers, and anyone deploying software. Once you understand images, containers, and volumes, the entire Docker ecosystem clicks. Start with one container, then move to Compose, then integrate Docker into your CI/CD pipeline. Bookmark the cheat sheet — you will use it almost daily.