Learning Docker - Storage Driver
Tags: tutorial, docker, containers
Published: 2024-02-04 08:40:31

## 03. Selecting a Storage Driver

**Overview**

In this lesson, we delve into the concept of storage drivers within Docker, a pivotal component in managing the temporary internal storage of a container's writable layer. Docker's storage drivers facilitate a pluggable framework that allows for diverse management strategies of the container's internal storage, especially when the container's software writes data to disk without using mounted volumes or external storage.

**The Role of Storage Drivers**

_**Pluggable Framework:**_ Docker employs a pluggable framework for its storage drivers, offering flexibility in how container storage is managed. This adaptability is crucial for supporting multiple operating systems and environments, as certain storage methods are only viable in specific settings.

**_Selection Criteria:_** The choice of a storage driver depends on the operating system, environment, and specific use cases. Factors such as performance efficiency for read/write operations and compatibility with the underlying OS influence this decision.

**Key Storage Drivers**

**_Overlay2:_** Overlay2 is a file-based storage driver and is the default option for both Ubuntu and CentOS (version 8 and newer). It is efficient for most general use cases, particularly where reading operations predominate.

**_Devicemapper:_** In contrast, Devicemapper offers block storage and is the default for CentOS 7 and earlier versions. It tends to perform better in scenarios with heavy write operations to the container layers.

**Managing Storage Drivers**

**_Checking the Current Driver:_** The docker info command can be used to identify the current storage driver in use. For example, CentOS 7 defaults to Devicemapper.

**_Changing Storage Drivers:_** Users can override the default storage driver by either passing a flag to the Docker daemon or by specifying the desired driver in the daemon.json configuration file. The latter method is recommended for its standardization across different environments.

Run the below command to see the docker-storage option

`docker info`

```
$ docker info
Client:
 Version:    24.0.5
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.0.0+unknown
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 1
 Server Version: 24.0.5
Storage Driver: overlay2
  Backing Filesystem: xfs

```
In my case, I have Storage Driver as `overlay2`. 

**Practical Application**

I used the AWS Ec2 Linux 2023 server, showing how to check the current storage driver and switch to a different one if necessary. 
The process involves editing the Docker daemon's configuration file (daemon.json) to explicitly set the storage driver to `devicemapper` or `overlay2` highlighting the standardized approach for modifying Docker daemon settings.

```
$ vim /etc/docker/daemon.json
```

In `daemon.json` file you need to put below. 

```
{
  "storage-driver": "overlay2"
}
```

```
$ systemctl restart docker.service
```

**Conclusion**

Understanding and effectively managing Docker's storage drivers is essential for optimizing container performance and ensuring compatibility with various environments. This lesson provides a foundational knowledge of Docker storage drivers, setting the stage for more advanced container management and operation practices in subsequent lessons.