01. Introduction to Docker
Docker completely revolutionized how software is built, shipped, and run. By packaging applications into lightweight, portable containers, Docker ensures that an application runs identically on a developer’s laptop, a testing server, and in production.
1.1 What is Docker?
Section titled “1.1 What is Docker?”Docker is an open-source platform that enables developers to automate the deployment of applications inside lightweight, portable environments called containers.
Containerization
Section titled “Containerization”Containerization is a type of operating-system-level virtualization. Instead of virtualizing the hardware (like a VM does), containerization virtualizes the OS. Applications are bundled with their dependencies, libraries, and configuration files into a single immutable package.
Containers vs Virtual Machines (VMs)
Section titled “Containers vs Virtual Machines (VMs)”- Virtual Machines include the application, dependencies, and a full guest operating system. They require a hypervisor, consume heavy CPU/Memory overhead, and take minutes to boot.
- Containers share the host system’s kernel. They package only the application and its dependencies. They are extremely lightweight, consume minimal resources, and boot in milliseconds.
Benefits of Containers
Section titled “Benefits of Containers”- Consistency: “It works on my machine” is no longer an excuse. The environment is guaranteed to be identical everywhere.
- Speed: Containers start instantly because there is no OS boot process.
- Isolation: Applications run in isolation without interfering with each other.
- Resource Efficiency: You can pack far more containers onto a single server compared to VMs.
Docker Use Cases
Section titled “Docker Use Cases”- Microservices architectures
- Continuous Integration / Continuous Deployment (CI/CD) pipelines
- Standardized local development environments
- Cloud-native scalable applications
Docker in DevOps
Section titled “Docker in DevOps”In the DevOps lifecycle, Docker acts as the ultimate bridge between Development and Operations. Developers write the code and the Dockerfile (the environment), and Operations simply runs the resulting container without worrying about installing specific programming languages or libraries on the host server.
1.2 Docker Architecture
Section titled “1.2 Docker Architecture”Docker uses a client-server architecture.
Docker Client
Section titled “Docker Client”The primary interface you use to interact with Docker. When you run commands like docker run or docker build, the client sends these commands to the Docker Daemon via a REST API.
Docker Engine
Section titled “Docker Engine”The core software that runs and manages containers. It consists of the Docker Client, the REST API, and the Docker Daemon.
Docker Daemon (dockerd)
Section titled “Docker Daemon (dockerd)”The background service running on the host machine. It listens for API requests and actually does the heavy lifting of building images, running containers, and managing networks/volumes.
Docker Host
Section titled “Docker Host”The physical or virtual machine running the Docker Daemon.
Docker Registry
Section titled “Docker Registry”A scalable service that stores and distributes Docker images (like GitHub, but for compiled containers).
- Public Registries: Docker Hub, GHCR (GitHub).
- Private Registries: AWS ECR, GCP Artifact Registry.
Docker Hub
Section titled “Docker Hub”The default, massive public registry maintained by Docker. It hosts millions of official and community images (e.g., nginx, postgres, ubuntu).
Docker API
Section titled “Docker API”The REST API used by the Client to instruct the Daemon. You can also interact with this API programmatically using scripts or tools like Portainer.
1.3 Docker Components
Section titled “1.3 Docker Components”The fundamental building blocks you will interact with daily:
Images
Section titled “Images”Read-only templates used to create containers. An image contains the application code, libraries, and tools required to run the app. Images are built in layers to maximize reuse and reduce size.
Containers
Section titled “Containers”The runnable instance of an image. You can start, stop, move, or delete a container. It is an isolated sandbox running on the host machine.
Networks
Section titled “Networks”Networks allow containers to communicate with each other, the host machine, and the outside internet, all while maintaining isolation from containers on other networks.
Volumes
Section titled “Volumes”By default, data created inside a container is lost when the container is deleted. Volumes are used to persist data (like databases) outside the container’s lifecycle directly onto the host machine.
Plugins
Section titled “Plugins”Extensions that add capabilities to Docker Engine, primarily used for advanced networking (third-party network drivers) and specialized volume storage (like AWS EBS or NFS).
1.4 Docker Lifecycle
Section titled “1.4 Docker Lifecycle”The journey of an application from code to a running container follows a strict lifecycle:
The Flow
Section titled “The Flow”Dockerfile ↓ (Build) Image ↓ (Run)Container ↓ (Stop) Stopped Container ↓ (Remove) Deleted- Dockerfile: You write a text file containing the instructions to build your environment.
- Build: You execute
docker build, which reads the Dockerfile and compiles it into a read-only Image. - Run: You execute
docker run, which takes the Image and spins up a live, isolated Container. - Stop: You execute
docker stopto gracefully halt the container process. The container still exists on disk. - Remove: You execute
docker rmto permanently delete the container instance. (The image remains until explicitly removed).