What is Docker?
Docker packages an application and its dependencies into a lightweight, isolated container that runs identically anywhere Linux runs.
What is this?
Docker is an open-source platform that automates the deployment of applications inside software containers. A container bundles your code, runtime, system tools, libraries and settings into one portable unit. Containers share the host operating system kernel, which makes them far lighter than virtual machines.
Why do you need it?
- Eliminates 'works on my machine' — the artifact you build is exactly what runs in production.
- Dramatically smaller and faster to start than VMs because containers share the host kernel instead of running a full guest OS.
- Reproducible builds and deploys: the same image produces the same behavior everywhere.
- Consistent isolation for microservices — each service runs in its own sandbox with its own filesystem and network.
Mental model
A shipping container
A Docker container
A shipping container standardizes cargo so any crane, ship or truck can move it without knowing the contents. Docker standardizes software so any server can run it without knowing the stack.
How it works
- You write a Dockerfile that describes how to assemble your image.
- docker build executes those instructions and produces an immutable image stored on the local registry cache.
- docker run creates a container from that image. The container gets its own filesystem layer, PID namespace, network namespace and cgroup limits.
- The Docker daemon (dockerd) manages containers, while the client (docker CLI) talks to the daemon over a socket or API.
- When the container's main process exits, the container stops. Data written to the writable layer is lost unless you use a volume.
How a Docker container isolates an app from the host.
Key commands
docker --versionCheck installed Docker Engine version.
docker infoShow system-wide information about the Docker installation.
docker run --rm -it ubuntu bashStart an interactive Ubuntu container and drop into a shell.
docker ps -aList all containers, including stopped ones.
docker imagesList locally cached images.
Examples
Run your first container
docker run --rm -d -p 8080:80 nginx# --rm remove the container when it stops# -d run detached (in background)# -p 8080:80 map host port 8080 to container port 80This pulls the official nginx image, starts a container in the background, and maps host port 8080 to the container's port 80. Open http://localhost:8080 to see nginx's default page.
In production
- Build once, run anywhere: CI produces an image that promotes unchanged through staging to production.
- Microservices ship as independently versioned containers managed by Kubernetes or Docker Compose.
- Ephemeral, horizontally scalable workloads: kill and recreate containers instead of patching live servers.
- Developer environments and feature previews spin up in seconds from the same image used in prod.
Best practices
- Pin image tags to specific versions or digests; never float on :latest in production.
- Keep containers ephemeral and stateless — write durable data to volumes.
- Run containers as non-root users with the least privileges needed.
- Prefer running one process per container (the main process defines the lifecycle).
Common mistakes
- Running containers as root inside the image (privilege escalation risk).
- Storing secrets in the image via ENV or baked into layers.
- Using :latest in production so deploys become unreproducible.
- Thinking containers persist data by default — the writable layer is wiped on recreation.
Common issues
Cannot connect to the Docker daemonClient cannot reach dockerd; common on Linux when the user lacks socket permission.
Verify the daemon is running (systemctl status docker) and add your user to the docker group, then re-login.
port is already allocatedHost port is already bound by another container or process.
Find the offender with docker ps and stop it, or map to a different host port.
No space left on deviceDisk image growth; dangling images and logs accumulate.
Run docker system prune -a --volumes (careful: removes unused images and volumes) and docker builder prune.
Debugging tips
- docker ps -a to see stopped containers and their exit codes.
- docker logs <container> to read stdout/stderr of the main process.
- docker exec -it <container> sh to inspect a running container.
- docker inspect <container> for low-level config including exit code and mounts.
Interview checkpoint
Hands-on lab
- Pull an image and run a container
- Inspect running containers
- Expose a container port to the host
- Clean up resources
Run Your First Container
20 min · beginner
Quick quiz
What is the main reason containers start faster than virtual machines?
A container's writable layer persists by default after the container is removed.
Which command lists containers that are currently stopped?
Cheat sheet
docker run IMAGECreate and start a containerdocker start|stop|restart IDManage a container's statedocker rm IDDelete a stopped containerdocker ps -aList all containersdocker logs IDView container outputdocker inspect IDLow-level container config