系统是CentOS7,DOCKER 1.13版本(之后版本未测试过),以上步骤在该操作系统和Docker版本下已经测试过。
解决方案
在默认情况下,Docker镜像和容器的默认存放位置为:/var/lib/docker
一般根下分区我们不会给太大。镜像和容器越存越多一般我们有两种解决方法:
1. 挂载大分区到/var/lib/docker
一般选择建立逻辑分区lvm,方便后期扩展集体。
-
建立新分区,并格式化
<pre class="has" name="code"><code class="hljs perl"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># lvcreate -L 300G lv_docker vg_home </span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># mkfs.ext4 /dev/vg_home/lv__docker</span></div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre> </li> <li> <p>挂载新分区到临时挂载点</p> <pre class="has" name="code"><code class="hljs perl"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># mkdir /mnt/docker </span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># mount /dev/vg_home/lv_docker /mnt/docker/</span></div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre> </li> <li> <p>停掉docker服务后,复制/var/lib/docker下数据到临时挂载点</p> <pre class="has" name="code"><code class="hljs perl"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># service docker stop </span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># cp -r /var/lib/docker/* /mtn/docker</span></div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre> </li> <li> <p>修改/var/lib/docker为/var/lib/docker.bak,并创建/var/lib/docker</p> <pre class="has" name="code"><code class="hljs perl"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># mv /var/lib/docker{,.bak} </span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># mkdir /var/lib/docker</span></div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre> </li> <li> <p>挂载新分区到/var/lib/docker,并设置开机自动挂载</p> <pre class="has" name="code"><code class="hljs cs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[<span class="hljs-meta">root@localhost lib</span>]<span class="hljs-meta"># mount /dev/vg_home/lv_docker /var/lib/docker </span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[<span class="hljs-meta">root@localhost lib</span>]<span class="hljs-meta"># vim /etc/fstab</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">--- </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> /dev/vg_home/lv_docker /<span class="hljs-keyword">var</span>/lib/docker ext4 defaults <span class="hljs-number">0</span> <span class="hljs-number">0</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">----</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre> </li> <li> <p>启动Docker服务,检查Docker服务是否可用, 数据是否完整</p> <pre class="has" name="code"><code class="hljs perl"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># /etc/init.d/docker start</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># docker images </span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># docker ps -a</span></div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre> </li> <li> <p>确认无误后卸载临时挂载点,删除/var/lib/docker.bak</p> <pre class="has" name="code"><code class="hljs perl"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># umount /mnt/docker </span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">[root@localhost lib]<span class="hljs-comment"># rm -rf /var/lib/docker.bak</span></div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre> </li>
2. 修改镜像和容器的存放路径
在Docker服务配置文件/etc/docker/daemon.json.中修改镜像和容器存放路径的参数, 在配置文件中加入:
-
{
-
-
"registry-mirrors": [
"http://hub-mirror.c.163.com"],
-
-
"graph":
"/var/data/docker"
-
-
}
具体执行步骤如下:
- 停止Docker服务
[root@localhost lib]# service docker stop
- 备份数据到新的存放路径
[root@localhost lib]# cp -rf /var/lib/docker /data/
- 修改备份/var/lib/docker路径
[root@localhost lib]# mv /var/lib/docker{,.bak}
- 启动Docker服务
[root@localhost lib]# service docker start
- 测试Docker服务
[root@localhost lib]# docker info
更多Docker相关教程见以下内容:
Docker安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm
Ubuntu 16.04 服务器上配置使用 Docker http://www.linuxidc.com/Linux/2017-06/145176.htm
Ubuntu 15.04下安装Docker http://www.linuxidc.com/Linux/2015-07/120444.htm
Docker 安装实例 http://www.linuxidc.com/Linux/2017-04/142666.htm
Docker 创建基础镜像 http://www.linuxidc.com/Linux/2017-05/144112.htm
在 Ubuntu 15.04 上如何安装Docker及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm
Ubuntu 16.04上Docker使用手记 http://www.linuxidc.com/Linux/2016-12/138490.htm
使用Docker分分钟启动常用应用 http://www.linuxidc.com/Linux/2017-04/142649.htm
Ubuntu 16.04下Docker修改配置文件不生效解决办法 http://www.linuxidc.com/Linux/2017-05/143862.htm
Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-11/148371.htm