Moving The Docker Data Directory

Submitted by Tom Thorp on Wednesday, May 22, 2019 - 22:45
Modified on Tuesday, July 2, 2019 - 17:36
Docker

Scenario

After reinstalling my Linux Operating system recently, I needed to reinstall Docker. However once everything has been set in stone, I needed to move the contents of the default Docker data directory (/var/lib/docker) to a new partition and reconfigure accordingly. 
 

Solution

To do this, 
 
1) Stop the running service of Docker.
 
systemctl stop docker.service
2) Copy contents of Docker data to the new partition
 
mkdir -p /path/to/docker-data
rsync -aqxP /path/from/docker-data/ /path/to/docker-data
3) Configure the /etc/docker/daemon.json file to point to the new partition.
 
{
  "debug": true,
  "tls": true,
  "tlscert": "/etc/docker/server-cert.pem",
  "tlskey": "/etc/docker/server-key.pem",
  "hosts": ["unix:///var/run/docker.sock","tcp://192.168.0.120:2376"],
  "dns": ["192.168.0.60","8.8.8.8"],
  "selinux-enabled": true,
  "log-driver": "journald",
  "data-root": "/path/to/docker-data",   <<== Change this!!!
  "iptables": false,
  "storage-driver": "overlay2"
}
4) (If using RedHat / Fedora / CentOS with SELinux enabled) add the following commands 
 
sudo semanage fcontext -a -s system_u -t container_var_lib_t "/path/to/docker-data(/.*)?"
sudo semanage fcontext -a -s system_u -t container_share_t "/path/to/docker-data/.*/config\.env"
sudo semanage fcontext -a -s system_u -t container_log_t "/path/to/docker-data/containers/.*/.*\.log"
sudo semanage fcontext -a -s system_u -t container_share_t "/path/to/docker-data/containers/.*/hostname"
sudo semanage fcontext -a -s system_u -t container_share_t "/path/to/docker-data/containers/.*/hosts"
sudo semanage fcontext -a -s system_u -t container_share_t "/path/to/docker-data/init(/.*)?"
sudo semanage fcontext -a -s system_u -t container_share_t "/path/to/docker-data/overlay(/.*)?"
sudo semanage fcontext -a -s system_u -t container_share_t "/path/to/docker-data/overlay2(/.*)?"

sudo restorecon -Rv /path/to/docker-data
5) Restart the Docker service
 
systemctl start docker.service
Hopefully this will make administration of Docker a little bit easier. 
 
 

About the author

Tom Thorp
Tom Thorp is an IT Consultant living in Miami on Queensland's Gold Coast. With over 30+ years working in the IT industry, Tom's experience is a broad canvas. The IT services Tom provides to his clients, includes :
 
Website development and hosting
Database Administration
Server Administration (Windows, Linux, Apple)
PABX Hosting and Administration
Helpdesk Support (end-user & technical).
  If you like any of my content, consider a donation via Crypto by clicking on one of the payment methods :
 
Categories