Docker默认目录在/var/lib/docker,linux中的/var本来分配的空间就不会太多,所以我们不能将镜像文件存储在这里,因此我们需要更改Docker的存储目录。
查找目前的存储目录
sudo docker info | grep "Docker Root Dir"
关闭所有在运行的Docker容器
docker ps | awk '{prinit $1}' | xargs docker stop
停止Docker服务
systemctl stop docker
拷贝先前镜像文件到新目录
我打算新目录放在用户的/home目录上。
也就是/home/bali16/data/docker/images
mkdir -p /home/bali16/data/docker/images
cd /var/lib/docker
cp -rp /var/lib/docker/* /home/bali16/data/docker/images/
设置Docker的配置文件
vim /etc/docker/daemon.json
{
"data-root": "/home/bali16/data/docker/"
}
重启docker
systemctl daemon-reload
systemctl start docker
踩坑
后来我docker安装gitlab死活都装不上,一直提示ArgumentError: could not find a temporary directory,找不到临时目录。
我很纳闷为什么会这样,后来看了一个帖子
Rails can’t find a temporary directory when run inside Docker container - Stack Overflow
Having had the same issue I supposed that your docker root is not '/var/lib/docker' but located somewhere else in your file system tree.
In my case the docker root was located on an external SSD formatted with NTFS. However, this filesystem did not support the required permissions for the temporary directory which is under '/tmp/'. So I resized the disk, created an additional EXT4 partition and configured docker to store its data on that partition. This solved the issue.
If this does not work try the following command: chmod +t /tmp for example: chmod +t /var/lib/docker/tmp
大意就是说修改了docker默认的存储目录,cp文件的时候会出现目标文件无权限写入的错误问题。
原因是cp时候的锅,
因此我们需要cp -p 参数,保留复制文件的权限。
也就是我们需要这样复制才可以保证不出文件权限问题。
cp -rp /var/lib/docker/* /home/bali16/data/docker/images/