Understanding Containers and Docker Basics
Before you learn Kubernetes, you need to understand containers. Kubernetes manages containers — so knowing what a container is makes everything else click into place.
What Is a Container
A container is a lightweight package that holds your application and everything it needs to run — the code, the libraries, the settings. You ship that one package to any server, and the app runs exactly the same way every time.
Think of a container like a packed lunch box. The lunch box contains the food, the fork, and the napkin. Wherever you take it — office, park, train — you open it and eat the same meal. Your app inside a container works the same way on your laptop, on a test server, or in production.
┌─────────────────────────────┐
│ Container │
│ ┌────────┐ ┌───────────┐ │
│ │ App │ │ Libraries │ │
│ └────────┘ └───────────┘ │
│ ┌──────────────────────┐ │
│ │ Config / Settings │ │
│ └──────────────────────┘ │
└─────────────────────────────┘
Runs the same everywhere
Containers vs. Virtual Machines
A virtual machine (VM) is like renting an entire apartment just to work on one project. It contains a full operating system, takes several minutes to start, and uses a lot of memory. A container is like renting a single desk in a co-working space. It shares the building's infrastructure (the server's operating system) and starts in seconds.
| Feature | Virtual Machine | Container |
|---|---|---|
| Startup time | Minutes | Seconds |
| Size | Gigabytes | Megabytes |
| OS needed | Full OS per VM | Shares host OS |
| Isolation | Strong | Good (process-level) |
| Portability | Limited | High |
What Is Docker
Docker is the most popular tool for creating and running containers. It gives you a command-line tool to build a container image, run it, stop it, and share it. Think of Docker as the machine that builds and opens your lunch boxes.
A Docker image is the recipe — a read-only blueprint of your app. A Docker container is a running copy of that image — the actual lunch box you opened and are eating from.
Dockerfile → docker build → Image → docker run → Container (Recipe) (Baking) (Cake) (Serving) (Slice on plate)
Writing a Simple Dockerfile
A Dockerfile is a text file that tells Docker how to build your image. Each line is one instruction. Here is a basic example for a Node.js web application:
FROM node:18 WORKDIR /app COPY package.json . RUN npm install COPY . . EXPOSE 3000 CMD ["node", "server.js"]
Line by line, this file says: start from an official Node.js base, set the working folder, copy the dependency list, install dependencies, copy the rest of the code, open port 3000, and start the server. Docker reads these instructions and builds a self-contained image.
Docker Commands You Will Use Often
| Command | What It Does |
|---|---|
docker build -t myapp . | Builds an image named myapp from current folder |
docker run -p 3000:3000 myapp | Runs the image as a container on port 3000 |
docker ps | Lists all running containers |
docker stop <id> | Stops a running container |
docker images | Lists all locally stored images |
docker push myapp | Pushes image to a registry like Docker Hub |
Docker Hub and Container Registries
A container registry stores your images so any server can pull and run them. Docker Hub is the public registry — like GitHub but for images. Your company can also run a private registry using tools like AWS ECR, Google Artifact Registry, or Harbor.
Developer → docker push → Docker Hub → docker pull → Server (Build image) (Registry) (Run container)
How Docker Connects to Kubernetes
Kubernetes does not build images. Your team builds images with Docker and pushes them to a registry. Kubernetes then pulls those images and runs them as containers inside Pods on your cluster. Kubernetes handles the scheduling, scaling, and healing — Docker just builds the package.
Docker builds the image ──► Registry stores it ──► Kubernetes runs it
The Relationship in Simple Terms
- Docker = the factory that makes the boxes (images)
- Registry = the warehouse that stores the boxes
- Kubernetes = the logistics company that delivers and manages the boxes
Why Containers Changed Software Development
Before containers, developers would say "it works on my machine" and spend days figuring out why the same code behaved differently on the production server. Containers eliminate that problem entirely. The image you test locally is the exact same image that runs in production. There is no difference in environment, no missing library, no wrong version of a runtime.
Key Points
- A container packages your app and all its dependencies into one portable unit.
- Containers are faster and lighter than virtual machines.
- Docker is the tool that builds and runs containers.
- A Dockerfile defines how your image is built.
- Kubernetes does not build images — it runs images that Docker (or another builder) already created.
