The core concepts, history, architecture, and basic commands.
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
Docker started as an internal project at dotCloud, a PaaS company, by Solomon Hykes and his team. It launched as an open-source project in 2013.
Dockerfile.Run multiple apps on the same machine without them fighting over dependencies (e.g., Python 3.8 vs 3.10).
Start databases (Mongo, Postgres, Redis) in seconds without installing them on your actual OS.
Mastering Docker is the first step to mastering Kubernetes and modern Container Orchestration.
"The Voice"
docker run ..."The Brain"
Docker Hub
The command line tool (docker) you use. It sends requests to the Docker Engine.
The background process (Daemon) that does the actual work. It builds, runs, and manages your containers. It creates the isolated environment.
Ideally works like GitHub but for Images instead of source code.
Source Control
git pushArtifact Storage
docker pushWhen you run this command, the Docker Engine first checks if you have the mongo image locally.
docker pull mongo first to just download it, and then docker run mongo later.The CLI talks to the Engine via a REST API. You can actually manage Docker using curl or Postman!
A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.
"Your codebase on GitHub"
A container is a running instance of an image. It encapsulates the application or service and its dependencies, running in an isolated environment.
"When you run node index.js on your machine"
Imagine joining a new Open Source project or team. You open the README.md and see:
With Docker, the entire environment (Node, Mongo, Postgres, Configs) is described in code.
You clone the repo and run one single command:
The Incident: A developer builds a feature. It runs perfectly on their MacBook. They push it to production.
🚨 Production CRASHES immediately.
The Real Magic: Docker containers are essentially "shipping the machine." The exact OS, libraries, and code that ran on your laptop are packaged up and sent to the server.
Cross-OS Consistency: docker run commands work exactly the same on Windows, Mac, and Linux. No more OS-specific installation guides!
By default, containers are isolated. If you run docker run mongo, the database starts on port 27017 inside the container, but it is completely inaccessible from your laptop (localhost).
The "Closed Door" Problem: The container listens on port 27017, but the "door" to your host machine is closed. You cannot connect to it.
-p FlagTo access the container, you must map a port on your machine (Host) to a port inside the container.
You can run multiple versions of the same app simultaneously without port conflicts!
Notice in the diagram above: Both containers are running standard Mongo on port 27017 internally. But to your machine, one is on 27017 and the other is on 27018. No conflict!
docker run hello-worldDownloads a test image and runs it in a container.docker psLists all correctly running containers.docker ps -aLists ALL containers (including stopped ones).docker logs <container_id>View the logs (stdout) of a container. Vital for debugging!docker exec -it <container_id> shGo INSIDE a running container (open a shell).docker imagesLists all images downloaded on your machine.docker build -t <name> .Builds a new image from exactly the Dockerfile in the current directory.docker start <container_id>Starts a stopped container. Differs from 'run' which creates a NEW container.docker stop <container_id>Stops a running container gracefully (SIGTERM).docker kill <container_id>Kills a container immediately (SIGKILL). Use only if stuck.docker rmi <image_id>Removes (deletes) an image from your disk to save space.docker rm <container_id>Removes (deletes) a stopped container.docker system pruneNuclear option: Cleans up ALL unused containers, networks, and dangling images.Often, running a container isn't enough. You need to see what's happening inside. Maybe you need to verify if a file was created or check database records directly.
docker exec -it <container_id> sh-i (Interactive): Keep STDIN open even if not attached.
-t (tty): Allocate a pseudo-TTY (makes it feel like a real terminal).
Why: You likely have another service (like a local Node app) running on port 3000.
Fix: Change the HOST port: -p 3001:3000
Why: You stopped a container but didn't remove it. It still exists in docker ps -a.
Fix: docker rm mongo or use --rm flag when running.
Do not use docker kill unless absolutely necessary.
If you build images often, you will see many named <none>. These take up space!
Clean them all up:
docker system prune