Chancel's blog

1 分钟阅读

Docker 镜像存储位置调整技巧

  • Deploy

Docker镜像对小容量的系统盘很不友好,其默认路径是/var/lib/docker,该路径通常没有特别大的硬盘空间

通过修改docker.service的配置来更改docker容器与镜像的存储位置

修改systemd的配置单,根据不同的操作系统其路径略有不同,可以通过查找/etc目录来获取

例如:

sudo find / -name 'docker.service*'                                                                                               

暂停docker服务

sudo systemctl stop docker.service

我的docker systemd配置单路径:/etc/systemd/system/multi-user.target.wants/docker.service

编辑保存如下,在service节点的ExecStart中添加参数--graph

[Unit]description=Docker Application Container Engine
Documentation=https://docs.docker.com
...

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --graph=/mnt/sdb/docker-lib
ExecReload=/bin/kill -s HUP $MAINPID
...

迁移Docker的文件到新目录下

sudo cp -r /var/lib/docker /mnt/sdb/docker-lib

应用设置

sudo systemctl daemon-reload
sudo systemctl start docker.service

检查docker运行是否异常,无异常后可删除/var/lib/docker目录

如果空间不足时打包镜像可能会引发 returned a non-zero code: 100的错误,更改存储位置确认空间足够后删除构建缓存即可

互动

留言

已加载 1 / 1 条留言

博主

新版本已经不支持`-graph`参数了,修改`systemctl`配置文件后启动失败,可以手动运行`systemctl`的命令来查看具体错误信息 ```bash ➜ ~ /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --graph=/mnt/sdb/docker-lib Flag --graph has been deprecated, Use --data-root instead the "graph" config file option is deprecated; use "data-root" instead ``` 可以看到,在高版本的docker中,`--graph`参数已经更改成`data-root`参数了

发表留言