SoFunction
Updated on 2025-04-11

Steps to change the default data directory of docker

1. Check whether docker exists and stop the service

[root@hxy ~]# docker version
Client: Docker Engine - Community
 Version:           27.2.1
 API version:       1.47
 Go version:        go1.22.7
 Git commit:        9e34c9b
 Built:             Fri Sep  6 12:09:42 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.2.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.7
  Git commit:       8b539b8
  Built:            Fri Sep  6 12:07:57 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.21
  GitCommit:        472731909fa34bd7bc9c087e4c27943f9835f111
 runc:
  Version:          1.1.13
  GitCommit:        v1.1.13-0-g58aa920
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@hxy ~]# systemctl stop docker
Warning: Stopping , but it can still be activated by:
  

2. Mount the mirror and install rsync for easy backup

[root@hxy ~]# mount /dev/sr0 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
[root@hxy ~]# yum -y install rsync
Updated Subscription Management Software Repository。
Unable to read the client identity

This system has not been registered in the rights server。Available subscription-manager Register。

Last dimensional data expiration check:0:04:09 forward,Execute on 2024Year11moon18day Monday 14hour38point34Second。
Software Package rsync-3.2.3-19.el9.x86_64 Installed。
Dependency resolution。
No processing is required。
complete!
[root@hxy ~]# 

3. Unmount backup and migrate existing data

[root@hxy ~]# umount /mnt (cancel mount)[root@hxy ~]# mkdir -p /mnt/docker-data (create a new data directory)[root@hxy ~]# rsync -aP /var/lib/docker/ /mnt/docker-data/ (backup data)sending incremental file list
./
buildkit/
         32,768 100%    0.00kB/s    0:00:00 (xfr#1, ir-chk=1103/1116)
containers/0847c6969330b0e88bc78c4120221954dd6f321c334be357be83c23b673ec29c/
containers/0847c6969330b0e88bc78c4120221954dd6f321c334be357be83c23b673ec29c/config.
          2,401 100%    2.29MB/s    0:00:00 (xfr#2, ir-chk=1081/1116)
containers/0847c6969330b0e88bc78c4120221954dd6f321c334be357be83c23b673ec29c/
          1,461 100%  713.38kB/s    0:00:00 (xfr#3, ir-chk=1080/1116)
containers/0899aa482296158404fe7e475b8ca64f571ef05fccaedb5279ce34cb2a1aa598/
containers/0899aa482296158404fe7e475b8ca64f571ef05fccaedb5279ce34cb2a1aa598/config.
          2,393 100%    1.14MB/s    0:00:00 (xfr#4, ir-chk=1072/1116)
containers/0899aa482296158404fe7e475b8ca64f571ef05fccaedb5279ce34cb2a1aa598/
          1,461 100%  475.59kB/s    0:00:00 (xfr#5, ir-chk=1071/1116)
containers/217d978e1a191d798efdad2058b28a1cd21f938e0335c04038472b285c42e3ab/
containers/217d978e1a191d798efdad2058b28a1cd21f938e0335c04038472b285c42e3ab/config.
          2,396 100%  584.96kB/s    0:00:00 (xfr#6, ir-chk=1063/1116)
containers/217d978e1a191d798efdad2058b28a1cd21f938e0335c04038472b285c42e3ab/
          1,461 100%  285.35kB/s    0:00:00 (xfr#7, ir-chk=1062/1116)
overlay2/
runtimes/
tmp/
volumes/
volumes/
         32,768 100%    0.00kB/s    0:00:00 (xfr#29, to-chk=0/26382)

4. Modify the docker configuration file

[root@hxy ~]# vi /etc/docker/ (delete the original accelerator content)[root@hxy ~]# cat /etc/docker/ (View input)
{
  "data-root": "/mnt/docker-data"
}

5. Verify the usage of new data directory

[root@hxy ~]# systemctl start docker (activate docker)[root@hxy ~]# systemctl status docker (check the current status)●  - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/; enabled; prese>
     Active: active (running) since Mon 2024-11-18 14:49:31 CST; 20s ago
TriggeredBy: ● 
       Docs: 
   Main PID: 61352 (dockerd)
      Tasks: 17
     Memory: 53.2M
        CPU: 1.016s
     CGroup: //
             └─61352 /usr/bin/dockerd -H fd:// --containerd=/run/containerd>

11moon 18 14:49:30 hxy dockerd[61352]: time="2024-11-18T14:49:30.772302169+08>
11moon 18 14:49:31 hxy dockerd[61352]: time="2024-11-18T14:49:31.391786710+08>
11moon 18 14:49:31 hxy dockerd[61352]: time="2024-11-18T14:49:31.565001457+08>
11moon 18 14:49:31 hxy dockerd[61352]: time="2024-11-18T14:49:31.565120362+08>
11moon 18 14:49:31 hxy dockerd[61352]: time="2024-11-18T14:49:31.565160031+08>
11moon 18 14:49:31 hxy dockerd[61352]: time="2024-11-18T14:49:31.565929847+08>
11moon 18 14:49:31 hxy dockerd[61352]: time="2024-11-18T14:49:31.604773790+08>
11moon 18 14:49:31 hxy dockerd[61352]: time="2024-11-18T14:49:31.604909987+08>
11moon 18 14:49:31 hxy dockerd[61352]: time="2024-11-18T14:49:31.693026651+08>
11moon 18 14:49:31 hxy systemd[1]: Started Docker Application Container Engin>
lines 1-22/22 (END)
[root@hxy ~]# 

6. Just check the effect

[root@hxy ~]# docker info
Client: Docker Engine - Community
 Version:    27.2.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.16.2
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.29.2
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 13
  Running: 0
  Paused: 0
  Stopped: 13
 Images: 2
 Server Version: 27.2.1
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: .v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 472731909fa34bd7bc9c087e4c27943f9835f111
 runc version: v1.1.13-0-g58aa920
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 5.14.0-362.8.1.el9_3.x86_64
 Operating System: Red Hat Enterprise Linux 9.3 (Plow)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 1.879GiB
 Name: hxy
 ID: 5bfc3e44-6003-449e-bb96-93c320d6f2f2
 Docker Root Dir: /mnt/docker-data(The default directory change is successful)
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

This is the article about how to change the default data directory of docker. For more information about changing the default data directory of docker, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!