This article is assisted by the following document:
- Link 1
- Link 2
CentOS7 modifies the Docker container and mirror default storage location
Usually, when you start using Docker, we don’t care much about the default directory Docker uses to store images and containers. When doing more experiments with Docker, you have to press the emergency button when Docker starts to take up a lot of space. So now it’s time to put troubleshooting into how to change Docker’s default storage location
When trying to change Docker’s default storage location, we must know some important information.
The storage driver image that Docker is currently using and the new storage space that the container will hold
The default location for Docker is /var/lib/docker, where all current images and containers are stored. If you have any running containers, stop them and make sure no containers are running, then run the following command to determine which storage driver Docker is currently using.
1. View the current information of Docker
Note: Two pieces of information about Docker need to be noted here:
- Storage Driver: Overlay2 The Storage Driver currently used by Docker
- Docker Root Dir: /var/lib/docker The data directory currently used by 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: 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 Engine
2. Stop Docker service
systemctl stop docker
3. Modify 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
Note:
- Keep the original configuration the same and add –graph=/ MNT /docker where/MNT /docker is the new storage location
-
Docker configuration can be modified in either of the following two files:
/etc/systemd/system/multi-user.target.wants/docker.service /usr/lib/systemd/system/docker.service
4. Copy all the 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
5. Reload the configuration and start
[root@localhost docker]# systemctl daemon-reload
[root@localhost docker]# systemctl start docker
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: 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 Engine
Note:
/ MNT/Docker ==Docker Root Dir: / MNT/Docker ==