--- title: Docker collection: docker-k8s-fundamentals author: Akash Kadlag updated: 2026-11-18 source: https://docs.chaicode.com/docker-k8s-fundamentals/docker --- # Docker ## **Docker** for Dummies **Docker** for Dummies This chapter is about Docker: a way to package an application together with its dependencies into a container. The core idea: build the container once, run it unchanged on a laptop, a test machine or in production. We will look at why this matters and how containers are built and run. ## One standard box for software One standard box for software Your app plus everything it needs like runtime, libraries, config packed into one image that runs the same everywhere. Docker packages your app plus everything it needs — the runtime, the libraries, the config — into one unit called a container image. That image runs the same way on your laptop, in CI and in the cloud. Before standard shipping containers, cargo came in every shape and was loaded by hand at every port. After, one standard box could go on any ship, crane or truck. Docker is that standard box for software. ## VMs vs Containers VMs vs Containers A VM is a whole house. A container is a flat in a building: your own space, **shared** foundation. Contains a full guest OS + app → Just the app + its libraries Size: GBs → Size: MBs Starts in minutes → Starts in seconds Strong isolation (separate kernel) → Process-level (shared kernel) Tens per server → Hundreds per server A VM contains a full guest operating system plus the app; a container is just the app and its libraries, sharing the host's kernel through the Docker Engine. That's why VMs are gigabytes and start in minutes, while containers are megabytes and start in seconds — tens per server versus hundreds. Common question: on Mac and Windows, Docker Desktop runs a small Linux VM behind the scenes, because containers need a Linux kernel. Containers didn't replace VMs. In the cloud, containers usually run on VMs. ## Dockerfile → Image → Container Dockerfile → Image → Container Image : Container = Class : Object. One image can run as **many** containers. docker build turns the Dockerfile recipe into an image docker run starts a container — a running instance of that image docker push / pull shares the image through a registry The Dockerfile is the recipe. docker build turns it into an image, a packaged snapshot. docker run turns the image into a container, a running instance. Image to container is like class to object: one image can run as many containers as you like. Images are shared through a registry — Docker Hub, GitHub Container Registry, Amazon ECR — with docker push and docker pull. For students: a recipe, then a packed meal kit, then a meal being cooked. FROM picks the base image. WORKDIR sets the folder inside the container. COPY brings files in from your machine. RUN executes a command at build time. EXPOSE only documents the app's port — it does not publish it; the -p flag does that at run time. CMD is what runs when the container starts. Say: look at what this file really is. It's the 2 AM runbook from earlier, the one people followed by hand, written as code that a machine runs the same way every time. Pro tip: copy package.json first and the rest of the code later. Docker caches each layer, so if only your code changes, npm ci doesn't re-run. ## Play with Docker Play with Docker Run commands one by one Hard limit six minutes. Narrate one sentence per command, then run it. If running late, skip docker logs and docker exec. Safety: pre-pull node:24-alpine and postgres:17. Have a .dockerignore with node_modules and .git so COPY . . is fast. Build once beforehand so layers are cached and the live build takes seconds. Increase the terminal font size; clear the terminal before starting. Keep a screen recording as a backup. If anything fails twice, switch to the recording and keep talking. Closing line: that was five minutes. Docker done properly, hands-on, needs hours — that's what I'm recording next (channel mention 3). ## We never installed specific Node version on our machine for this app. The image **brought** it. We never installed specific Node version on our machine for this app. The image **brought** it. This is the one sentence the demo exists for. The runtime came inside the image, not from the machine. That is the whole promise: the environment travels with the app, so the host only needs Docker. ## Volumes and networks Volumes and networks Volumes: A container's files vanish when it's removed. Volumes keep data outside: docker run -v mydata:/data my-app Networks: Containers on the same network reach each other by name: docker network create appnet Volumes: a container's files vanish when it's removed. Volumes store data outside the container — docker run -v mydata:/data my-app. Networks: containers on the same network reach each other by name — docker network create appnet. Key line: inside a container, localhost means the container itself. If you watched the networking video, this is the same ports and DNS, just inside containers (channel mention 2). ## Real apps need many containers Real apps need many containers A typical app is web plus API plus database plus a cache like Redis. Starting each one with a long docker run command, in the right order, with the right network, is painful. The pattern again: the fix was one container; the new pain is four containers, started by hand. ## Docker Compose Docker Compose Onboarding becomes: clone, docker compose up, start coding. web reaches the database at db:5432, because the service name is the hostname. The old version: "3" line is no longer needed. If on time, run docker compose up -d and docker compose ps live (30 seconds). If not, just show the file. The password is in the file because this is local development. In real projects it comes from an .env file or a secret manager. Likely question: depends_on controls start order only. It doesn't wait for Postgres to be ready; a healthcheck does that. ## The problems revisited The problems revisited 1. Environment drift → The same image runs in dev, CI and production 2. Dependency conflicts → Each app carries its own runtime, side by side 3. Slow onboarding → Clone, docker compose up, start coding 4. Snowflake servers → A server only needs Docker; everything else lives in the image 5. Painful rollbacks → Run the previous image tag 6. Manual scaling → Still manual Environment drift: the same image runs in dev, CI and production. Dependency conflicts: each app carries its own runtime, side by side. Slow onboarding becomes clone, docker compose up, start coding. Snowflake servers: a server only needs Docker; everything else lives in the image. Rollbacks: run the previous image tag. Say: this is why Docker won. Not because it was new, but because it closed five of the six problems on our list in one move. And look at number six. It's still standing. That one problem is the whole reason the next tool exists. ## The new challenge: too many containers The new challenge: too many containers Apps grew from one monolith into many microservices: Users, Orders, Payments, Search, Notifications. Apps grew from one monolith into many microservices — Users, Orders, Payments, Search, Notifications. Ten services times three copies each is thirty containers, spread across ten servers. Docker made containers so easy that we created too many of them. ## Who looks after them? Who looks after them? Which server has room for this container? A container crashed at 3 AM. Who **restarts** it? Traffic doubled. Who starts more copies? New version: how do we update with zero downtime? Containers keep moving. How do services find each other? Which server has room? Who restarts a crashed container at 3 AM? Who starts more copies when traffic doubles? How do we roll out a new version with zero downtime? When containers keep moving, how do services find each other? Compose runs many containers on one machine. None of these questions are answered on a single machine. ## Container **Orchestration** Container **Orchestration** Compose runs many containers on one machine; we need something to manage them across many machines. Docker made containers so easy that we created too many of them. Compose runs many containers on one machine. We need something to manage them across many machines. That's Kubernetes — the subject of the next chapter.