Moving Docker’s storage location

Oliver Mascarenhas
Code Uncomplicated
Published in
2 min readOct 6, 2023

The right way to move Docker’s default storage location on Linux

Move your docker storage, what could go wrong!

Docker has revolutionized the way we deploy and manage applications. However, as your Docker usage grows, you might find the need to move Docker’s default storage location. This could be due to various reasons such as running out of disk space, optimizing storage performance, or adhering to specific organizational requirements. In this post, we’ll explore the process of moving Docker’s default storage location and delve into the use cases where this move is most appropriate.

Docker’s Default Storage Location

By default, Docker stores its images, containers, and volumes in the /var/lib/docker directory on Linux systems. However, this location might not always be suitable, especially in scenarios where you have limited disk space on the system drive or need to distribute the storage across different partitions or drives.

Use Cases for Moving the Default Storage Location

Disk Space Optimization: When your server is running low on disk space, moving Docker’s storage location to a drive with more available space is crucial. This prevents your system from becoming unresponsive due to insufficient disk space.

Performance Enhancement: SSDs (Solid State Drives) are significantly faster than traditional HDDs (Hard Disk Drives). By moving Docker’s storage to an SSD, you can enhance the read/write speeds, leading to faster container operations and improved application performance.

Data Separation: In scenarios where you need to keep sensitive data separate from the operating system, moving Docker’s storage to an encrypted external drive or a specific partition ensures data security. This is particularly important for applications dealing with sensitive customer information or compliance data.

Backup and Disaster Recovery: When Docker’s storage location is on a separate drive, it becomes easier to manage backups and implement disaster recovery strategies. You can focus your backup efforts on the specific drive housing Docker’s data, ensuring quick recovery in case of system failures.

Steps

  1. Stop docker from running
> sudo systemctl stop docker.service

> sudo systemctl stop docker.socket

2. Edit the systemd file located at /lib/systemd/system/docker.service with your preffered editor, we’ll use vi here.

> sudo vi /lib/systemd/system/docker.service

3. Search for the line below.

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Add the --data-root flag to point to the new directory. In the example below, we’ve mounted an SSD drive.

ExecStart=/usr/bin/dockerd --data-root /mnt/ssddata/dockerdata -H fd:// --containerd=/run/containerd/containerd.sock

4. Next we’ll create the directory we intend to use in case it dosen’t already exist and use the rsync command to copy the contents of /var/lib/docker to the new location.

> sudo mkdir -p /mnt/ssddata/dockerdata

> sudo rsync -aqxP /var/lib/docker/ /mnt/ssddata/dockerdata

5. Finally we’ll reload the systemd configuration and restart the Docker service.

> sudo systemctl daemon-reload

> sudo systemctl start docker

--

--

Oliver Mascarenhas
Code Uncomplicated

Designing and developing scalable and fault tolerant data pipelines and platforms | https://olivermascarenhas.com/