Storage directory migration of docker (valid for pro test)

The default storage directory of docker is / var/lib/docker. The directory structure is as follows:

    [root@ztscshop docker]# cd /var/lib/docker
    [root@ztscshop docker]# ll
    total 64
    drwx------  2 root root  4096 Jan 24  2019 builder
    drwx------  4 root root  4096 Jan 24  2019 buildkit
    drwx--x--x  3 root root  4096 Jan 24  2019 containerd
    drwx------  4 root root  4096 Oct 24 15:05 containers
    drwx------  3 root root  4096 Jan 24  2019 image
    drwxr-x---  3 root root  4096 Jan 24  2019 network
    drwx------ 80 root root 16384 Oct 24 15:25 overlay2
    drwx------  4 root root  4096 Jan 24  2019 plugins
    drwx------  2 root root  4096 Oct 22 11:57 runtimes
    drwx------  2 root root  4096 Jan 24  2019 swarm
    drwx------  2 root root  4096 Oct 24 15:13 tmp
    drwx------  2 root root  4096 Jan 24  2019 trust
    drwx------ 15 root root  4096 Feb  7  2019 volumes
    [root@ztscshop docker]# 

All of docker's own things will exist under these folders, including image files and containers, so it will occupy more and more disk space.

If the disk space where docker is located is relatively small, and the disk space is full, when there is no space, it will obviously lead to the failure of the docker container. At this time, it is necessary to migrate the docker storage directory to a larger disk.

1. Check disk usage: df -h

    [root@ecs-8f49 ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    devtmpfs        7.8G     0  7.8G   0% /dev
    tmpfs           7.8G     0  7.8G   0% /dev/shm
    tmpfs           7.8G   73M  7.7G   1% /run
    tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
    /dev/vda1        40G   13G   25G  33% /
    tmpfs           1.6G     0  1.6G   0% /run/user/0
    overlay          40G   13G   25G  33% /var/lib/docker/overlay2/53eb033fae8fadc98df74b118e743092fcd0ee68b579c2b771288c3c023b0b8b/merged
    shm              64M     0   64M   0% /var/lib/docker/containers/8ecb90f024c08468c5f32e7c8b8b0d1ff87616536e6c2155cfb58447bded3071/shm

2. Check the memory occupation of docker: docker system df

    [root@ecs-8f49 ~]# docker system df
    TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
    Images              16                  12                  2.893 GB            1.535 GB (53%)
    Containers          12                  1                   155.6 MB            155.6 MB (100%)
    Local Volumes       1                   1                   4.212 GB            0 B (0%)

3. The docker system prune command can be used to clean up disks, delete closed containers, useless data volumes and networks, and dangling images (i.e. images without tag s).

    [root@ecs-8f49 ~]# docker system prune
    WARNING! This will remove:
        - all stopped containers
        - all volumes not used by at least one container
        - all networks not used by at least one container
        - all dangling images
    Are you sure you want to continue? [y/N] y
    Deleted Containers:
    9bd48276fcd28b3882605fd67a156a7ea848f8fbfe16c3588b630955dfda253d
    74af4504bdefbc58b83a476c12d6ce10260f91349eed0e5bfca5e51b86fb8474
    d405d43eeb7908472b4b8712f668ca3b97f3796f6afc1c1d19a15879140ddf64
    9f645670de22840d04249351d07b2f68f77754e4dfebdb4b39c56d827d6b6805
    79d32c6b3c34bb359cbbf8309d7be34074da391d3b48083f456a5911259a5213
    ad7137d7e018144e17790a3c86a8217a95faecd07a39003bc4df275c8c8d351d
    b02eb42677a52663e50fc4a2b1344d37a8d2a2cca445fe5eae49648a0ca54b13
    0813454a5421cf380b72265c2e6832411a590479424dbc3470e47e1cc9cdd740
    a58b5f545ce1cd03a2899ba277641b5f52203802d9672c72f281f2d5a31fc219
    936e65f72c60d754cb2a1441d0cdeb18f9a7ebde353f091fc44a034733fed52c
    ad86565bd85af04f5847703ba213d226c7d7b69e5210211e4170671d3ea4051c

    Deleted Volumes:
    37620879ca58c052c47e7677a1851f45c25cbc0437c8883cc2fe8e70e1cb921a

    Total reclaimed space: 4.368 GB

4. The Docker system prune - a command cleans up more thoroughly. You can delete all Docker images without containers. Note that these two commands will delete the containers that you have temporarily closed and the Docker images that are not used... So be sure to think clearly before using them. I haven't used it, because it will clean the Docker image that is not open.

5. Migrate the / var/lib/docker directory.

5.1 stop docker service

    systemctl stop docker

5.2 create a new docker directory, execute the command df -h, and find a large disk. I created the / home/docker/lib directory under the / home directory. The command is:

    mkdir -p /home/docker/lib

5.3 migrate the files under / var/lib/docker directory to / home/docker/lib:

(rsync is a linux folder synchronization command, and the parameter - r should be added, because the folder needs recursive copy. If it is on the same server, direct cp is also possible.)

    rsync -r -avz /var/lib/docker /home/docker/lib/

5.4 configure / etc / SYSTEMd / system / docker service. d/devicemapper.conf. View devicemapper Conf exists. If it does not exist, create a new one.

    mkdir -p /etc/systemd/system/docker.service.d/
    vi /etc/systemd/system/docker.service.d/devicemapper.conf

5.5 and then in the {devicemapper Conf write: (synchronize the parent folder during synchronization. The actual directory should be / home/docker/lib/docker)

    [Service]
    ExecStart=
    ExecStart=/usr/bin/dockerd --graph=/home/docker/lib/docker

5.6 reload docker

    systemctl daemon-reload

    systemctl restart docker

    systemctl enable docker

5.7 in order to confirm that everything is running smoothly, the

    docker info

Command to check the root directory of Docker It will be changed to / home/docker/lib/docker

    ...
    Docker Root Dir: /home/docker/lib/docker
    Debug Mode (client): false
    Debug Mode (server): false
    Registry: https://index.docker.io/v1/
    ...

5.8 after successful startup, confirm that the previous image is still in use:

    root@nn0:~$ docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    AAA/AAA v2 7331b8651bcc 27 hours ago 3.85GB
    BBB/BBB v1 da4a80dd8424 28 hours ago 3.47GB

5.9 delete the files in / var/lib/docker / directory after confirming that the container is OK.

6. After migrating the / var/lib/docker directory, Docker cannot be started (error initializing graphdriver: driver not supported)

Startup information

    [root@nn0 ~]# systemctl start docker
    Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.

Start up details

    8 January 31:47:25 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
    -- Subject: Unit docker.service has begun start-up
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    -- 
    -- Unit docker.service has begun starting up.
    8 January 31:47:25 localhost.localdomain dockerd[2390]: time="2018-08-31T01:47:25.197015872+08:00" level=info msg="libcontainerd: new containerd process, pid: 2393"
    8 January 31:47:26 localhost.localdomain dockerd[2390]: time="2018-08-31T01:47:26.204103195+08:00" level=error msg="[graphdriver] prior storage driver overlay2 failed: driver not supported"
    8 January 31:47:26 localhost.localdomain dockerd[2390]: Error starting daemon: error initializing graphdriver: driver not supported
    8 January 31:47:26 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
    8 January 31:47:26 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.

Error reason: error initializing graphdriver: driver not supported

Solution: create daemon.doc in / etc/docker directory JSON file and add the following configuration

    touch daemon.json
    vi daemon.json
    {
      "storage-driver": "overlay2",
      "storage-opts": [
        "overlay2.override_kernel_check=true"
      ]
    }

Start again: systemctl start docker

7. Docker runc not installed on system

Problem description

When docker runs the image, an error is reported as follows:
(if the port mapping error is reported, start the image directly without mapping the port. Try whether it is this error, because if the image is started directly by mapping the port, this error will lead to the container startup failure, but the error is the port mapping failure.)

    [root@nn0 k8s]# docker run -it registry.helloworld.com/test/atsd:latest bash
    WARNING: IPv4 forwarding is disabled. Networking will not work.
    /usr/bin/docker-current: Error response from daemon: shim error: docker-runc not installed on system.

Problem solving:

    [root@nn0 k8s]# cd /usr/libexec/docker/
    [root@nn0 docker]# ln -s docker-runc-current docker-runc

Keywords: Docker

Added by stockton on Sun, 02 Jan 2022 04:29:37 +0200