Anatomy of Docker
Tags: docker, containers, tutorial, webdev
Published: 2024-01-15 13:00:00

In this section, we delve into the intricate components that form the backbone of Docker. Understanding the anatomy of Docker is essential for harnessing its true power in containerization.

### Container Components:

Docker operates on the fusion of Linux namespaces and control groups, providing a unique and isolated environment for applications. Let's simplify this complex structure:

- **Linux Namespace:**
  - A Linux kernel feature providing different "views" of your system to applications.
  - Offers a unique and isolated instance of system resources, allowing independent operation of processes or containers.

- **Linux Control Group (cgroup):**
  - Another Linux kernel feature restricting how much hardware each process can use.
  - Utilized for monitoring and restricting CPU, network, disk bandwidth, and memory consumption.
  - Assigning disk quotas (not supported by Docker).


![Anatomy of Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e06hmff96ykniuc26irx.png)
**Note:** While Docker is native to Linux and some newer versions of Windows, containers can run on any OS. Docker is compatible with Windows Server, and with the introduction of Windows Subsystem for Linux (WSL) from Windows 10 Anniversary Edition onwards, performance on Windows has significantly improved.

### Linux's Eight Kernels:

Let's briefly explore the eight namespaces in the Linux kernel that provide isolation for processes, networking, interprocess communication, and more:

1. **PID Namespace (pid):**
   - Isolates the process ID number space, ensuring processes in different namespaces have distinct PIDs.

2. **Network Namespace (net):**
   - Provides isolation in terms of network resources.
   - Enables each namespace to have its own network interfaces, routing tables, and firewall rules.

3. **Mount Namespace (mnt):**
   - Isolates the set of mount points seen by a group of processes.
   - Ensures each namespace has its own filesystem hierarchy.

4. **IPC Namespace (ipc):**
   - Isolates interprocess communication resources.
   - Each namespace has its own System V IPC, message queues, and semaphore arrays.

5. **UTS Namespace (uts):**
   - Isolates system identifiers like hostname and NIS domain name.
   - Enables each namespace to have its own hostname and domain name.

6. **User Namespace (user):**
   - Provides isolation for user and group IDs.
   - Allows different namespaces to have different user and group mappings.

7. **Cgroup Namespace (cgroup):**
   - Introduced in Linux kernel 5.10.
   - Enables isolation for control groups, allowing each namespace to have its own set of cgroups.

8. **Time Namespace (time):**
   - Introduced in Linux kernel 5.6.
   - Ensures each namespace has its own perception of time.

### Docker Advantages:

#### Dockerfiles:

Dockerfiles simplify configuring and packaging apps and their environments.

- Users instruct Docker on container configuration through Dockerfiles.
- Docker utilizes these Dockerfiles to package apps and their environments into images.

#### Example:

```dockerfile
# Dockerfile example (more details in the next blog)
FROM ubuntu:latest
RUN apt-get update && apt-get install -y nginx
CMD ["nginx", "-g", "daemon off;"]
```

#### Sharing Images:

- Docker Hub, a global repository, facilitates easy image sharing.
- Users can utilize alternative registries like AWS Elastic Container Registry (ECR), GCP Google Container Registry (GCR), Quay.io, Harbor, Azure Container Registry (ACR) etc., and share images effortlessly.

#### Docker CLI:

- Docker CLI makes starting apps in containers a breeze.
- No worries about UID mappings, network interfaces, or Docker configuration files.

#### Docker Limitations:

- Docker relies on Linux namespaces and control groups, making it technically native to Linux and some newer versions of Windows.
- Containers can run on any OS, but their images are bound to their parent OS. Workarounds exist for these limitations.

### Docker Alternatives:

#### Container Runtime Interface (CRI):

- Kubernetes introduced CRI, enabling developers to create their container runtimes.
- Examples: CRI-O, RUNC, Firecracker from AWS.
- Podman by RedHat for highly secure, rootless containers.

#### Docker Machine:

Docker Machine serves as a workaround for Mac and Windows users, allowing them to run Docker on their systems. However, if you are using Linux, there are specific distributions optimized to support container runtimes more efficiently.

##### Docker Machine for Mac/Windows:

Designed as a workaround for non-Linux environments.
Utilizes VirtualBox to create virtual machines running Docker.
Offers a solution for those operating systems lacking native Docker support.

##### Linux Distributions Optimized for Containers:

When using Docker on Linux, consider distributions optimized for container runtimes. Examples include:
- Ubuntu: A widely used and well-supported Linux distribution with good compatibility for Docker.
- Alpine Linux: Known for its lightweight nature, making it an excellent choice for containerized applications.
- Fedora CoreOS: A minimal OS designed explicitly for running containerized applications at scale.

##### Performance Considerations:

Docker Machine on Mac and Windows may exhibit slower performance due to the virtualization layer and dependency on VirtualBox.
On Linux, choosing a distribution optimized for containers can enhance Docker's performance and resource utilization.
When selecting a Linux distribution for Docker, it's essential to consider factors like compatibility, resource efficiency, and the level of support provided for container runtimes. Each distribution may have its strengths, so choose the one that aligns best with your specific requirements.

#### Docker Desktop:

- A more refined solution introduced in 2016.
- Smaller and faster VM running on Apple's native hypervisor or Hyper-V on Windows.
- Improved GUI for configuration, mounting volumes, and exposing network ports.
- Addresses the limitations of Docker Machine.

### Conclusion:

Understanding Docker's anatomy is crucial for harnessing its power. From Linux namespaces to Dockerfiles, we've peeled back the layers. Stay tuned for the next blog, where we'll dissect Dockerfiles and dive deeper into crafting containerized environments.

**References:**
- [Linux Documentation. Namespaces in operation, part 1: namespaces overview.](https://lwn.net/Articles/531114/#:~:text=The%20namespaces,instance%20of%20the%20global%20resource)
- [Docker Documentation. Dockerfile reference.](https://docs.docker.com/engine/reference/builder/)
- [Kubernetes Documentation. Container runtimes.](https://kubernetes.io/docs/setup/production-environment/container-runtimes/)
- [Podman Documentation. Podman is a daemonless container engine for developing, managing, and running OCI Containers on your Linux System.](https://podman.io/)
- [RedHat vs Kubernetes](https://www.redhat.com/en/technologies/cloud-computing/openshift/red-hat-openshift-kubernetes)