Assisted documents of this article:

  • Link 1
  • Link 2

CentOS7 changes the default storage locations of Docker containers and images

Usually, when you start using Docker, we don’t care much about the default directory docker uses to store images and containers. When you do more experiments with Docker and Docker starts taking up a lot of space, you have to press the panic button. So now is the time to put troubleshooting on how to change docker’s default storage location

There are some important things to know when trying to change the default Docker storage location.

Docker is currently using the default storage location docker is currently using the storage driver image and container to store the new storage space

The default Docker location is /var/lib/docker, where all current images and containers are stored. If you have any running containers, stop them and make sure none are running, then run the following command to determine which storage driver Docker is currently using.

1. View the current docker information

Note: Two pieces of information about docker need to be noted here:

  • Storage Driver: overlay2 Storage Driver used by the Docker
  • Docker Root Dir: /var/lib/docker Data directory used by the current Docker

docker info

[root@localhost ~]# docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 18.09.6 Storage Driver: Overlay2 Backing Filesystem: XFS Supports D_type: true Native Overlay Diff: false Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84 runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30 init version: Fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-514.el7.x86_64 Operating System: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-514.el7.x86_64 CentOS Linux 7 (Core) OSType: Linux Architecture: x86_64 CPUs: 1 Total Memory: 976.5MiB Name: localhost.localdomain ID: 3EGY:424H:ER2C:CBQ7:KUD5:SFRJ:TVDK:XV4Y:AFYJ:SOOL:7OM6:65K5 Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false Product License: Community EngineCopy the code

2. Stop docker service

systemctl stop docker
Copy the code

3. Modify the Docker service startup file

[root@localhost ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --graph=/mnt/docker
Copy the code

Note:

  • –graph=/ MNT /docker, / MNT /docker is the new storage location

  • Docker configuration can be modified using either of the following files:

/etc/systemd/system/multi-user.target.wants/docker.service
/usr/lib/systemd/system/docker.service
Copy the code

4. Copy all files and directories in the original directory to the new directory

[root@localhost mnt]# cp -rf /var/lib/docker/* /mnt/docker/

[root@localhost docker]# pwd
/mnt/docker
[root@localhost docker]# ls
builder  buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
Copy the code

5. Reload the configuration and start

[root@localhost docker]#  systemctl daemon-reload
[root@localhost docker]# systemctl start docker
Copy the code

6. Check docker information

[root@localhost docker]# docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 18.09.6 Storage Driver: Overlay2 Backing Filesystem: XFS Supports D_type: true Native Overlay Diff: false Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84 runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30 init version: Fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-514.el7.x86_64 Operating System: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-514.el7.x86_64 CentOS Linux 7 (Core) OSType: Linux Architecture: x86_64 CPUs: 1 Total Memory: 976.5MiB Name: localhost.localdomain ID: 3EGY:424H:ER2C:CBQ7:KUD5:SFRJ:TVDK:XV4Y:AFYJ:SOOL:7OM6:65K5 Docker Root Dir: /mnt/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false Product License: Community EngineCopy the code

Note:

Docker Root Dir: / MNT/Docker ==Docker Root Dir: / MNT/Docker ==