docker的存储目录迁移
(转载的文章,亲测有效)
docker的默认存储目录是/var/lib/docker
,目录结构如下:
[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]#
docker所有自身的东西都会存在这些文件夹下面包括镜像文件和容器,所以会占用越来越多的磁盘空间。
如果docker所在的磁盘空间比较小,磁盘空间被占满没有空间的时候,显然会导致docker容器运行失败,此时必然要迁移docker的存储目录到一个更大的磁盘下面。
1.查看磁盘占用情况: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.查看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.docker system prune
命令可以用于清理磁盘,删除关闭的容器、无用的数据卷和网络,以及dangling镜像(即无tag的镜像)。
[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.docker system prune -a
命令清理得更加彻底,可以将没有容器使用Docker镜像都删掉。注意,这两个命令会把你暂时关闭的容器,以及暂时没有用到的Docker镜像都删掉了…所以使用之前一定要想清楚.。我没用过,因为会清理 没有开启的 Docker 镜像。
5.迁移/var/lib/docker
目录。
5.1 停止docker服务
systemctl stop docker
5.2 创建新的docker目录,执行命令df -h,找一个大的磁盘。 我在 /home目录下面建了 /home/docker/lib
目录,执行的命令是:
mkdir -p /home/docker/lib
5.3 迁移/var/lib/docker
目录下面的文件到 /home/docker/lib
:
(rsync是linux文件夹同步的命令,参数要加一个-r,因为文件夹要递归copy,如果在同一台服务器上面,直接cp也是可以的)
rsync -r -avz /var/lib/docker /home/docker/lib/
5.4 配置 /etc/systemd/system/docker.service.d/devicemapper.conf
。查看 devicemapper.conf
是否存在。如果不存在,就新建。
mkdir -p /etc/systemd/system/docker.service.d/
vi /etc/systemd/system/docker.service.d/devicemapper.conf
5.5 然后在 devicemapper.conf
写入:(同步的时候把父文件夹一并同步过来,实际上的目录应在 /home/docker/lib/docker )
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --graph=/home/docker/lib/docker
5.6 重新加载 docker
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
5.7 为了确认一切顺利,运行
docker info
命令检查Docker 的根目录.它将被更改为 /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 启动成功后,再确认之前的镜像还在:
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 确定容器没问题后删除/var/lib/docker/
目录中的文件。
6.迁移/var/lib/docker
目录后,Docker无法启动 (error initializing graphdriver: driver not supported)
启动信息
[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.
启动详情
8月 31 01: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月 31 01: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月 31 01: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月 31 01:47:26 localhost.localdomain dockerd[2390]: Error starting daemon: error initializing graphdriver: driver not supported
8月 31 01:47:26 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
8月 31 01:47:26 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.
错误原因:error initializing graphdriver: driver not supported
解决办法:在 /etc/docker
目录下创建daemon.json文件,并且加入以下配置
touch daemon.json
vi daemon.json
{
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
再次启动:systemctl start docker
7. docker-runc not installed on system 问题
问题描述
docker运行镜像的时候,报错如下:
(如果报错端口映射的错误,那就先不映射端口直接启动镜像,试一下是不是这个错误,因为如果直接映射端口启动镜像的话,这个错误导致容器启动失败,但是报错是端口映射失败)
[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.
问题解决:
[root@nn0 k8s]# cd /usr/libexec/docker/
[root@nn0 docker]# ln -s docker-runc-current docker-runc