怎么修改docker images的存储目录

发布网友 发布时间:2022-04-23 02:15

我来回答

2个回答

热心网友 时间:2022-04-26 13:29

Docker的镜像以及一些数据都是在/var/lib/docker目录下,它占用的是Linux的系统分区,也就是下面的/dev/vda1,当有多个镜像时,/dev/vda1的空间可能不足,我们可以把docker的数据挂载到数据盘,例如:/dev/vdb目录下。
[root@10-10-63-106 docker]# df -lhT
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 xfs 20G 3.8G 16G 20% /
devtmpfs devtmpfs 916M 0 916M 0% /dev
tmpfs tmpfs 921M 0 921M 0% /dev/shm
tmpfs tmpfs 921M 43M 878M 5% /run
tmpfs tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/vdb xfs 100G 11G 90G 11% /data
其中主要的步骤如下:
(1) 首先,备份fstab文件
sudo cp /etc/fstab /etc/fstab.$(date +%Y-%m-%d)
(2) 停止docker, 用rsync同步/var/lib/docker到新位置.
如果rsync没有安装,则使用yum -y intall rsync 进行安装,停止docker ,service docker stop,在数据分区中建立要挂载的目录,mkdir /data/docker 使用rsync工具同步,rsync -aXS /var/lib/docker/. /data/docker/,这可能需要花费的较长的时间,取决于/var/lib/docker的大小,
(3) 修改fstab
在该文件中把下面一行添加到fstab里,将新位置挂载到 /var/lib/docker
/data/docker /var/lib/docker none bind 0 0
文件的内如如下:
[root@10-10-63-106 docker]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jul 31 07:50:13 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/vda1 / xfs errors=remount-ro 0 1
/swapfile none swap defaults 0 0
/dev/vdb /data xfs defaults,noatime 0 0
/data/docker /var/lib/docker none bind 0 0
(4) 重新挂载
mount –a
(5) 使用下面的命令检查一下
df /var/lib/docker/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vdb 1048000 47204 104759196 1% /var/lib/docker
(6)进入Container查看我们的空间
bash-4.1# df -lhT
Filesystem Type Size Used Avail Use% Mounted on
rootfs rootfs 9.8G 1.4G 7.9G 15% /
tmpfs tmpfs 921M 0 921M 0% /dev
shm tmpfs M 0 M 0% /dev/shm
/dev/vdb xfs 100G 2.1G 98G 3% /etc/resolv.conf
/dev/vdb xfs 100G 2.1G 98G 3% /etc/hostname
/dev/vdb xfs 100G 2.1G 98G 3% /etc/hosts
tmpfs tmpfs 921M 0 921M 0% /run/secrets
tmpfs tmpfs 921M 0 921M 0% /proc/kcore
没有更改/var/lib/docker路径之前的情况:
bash-4.1# df -lhT
Filesystem Type Size Used Avail Use% Mounted on
rootfs rootfs 9.8G 1.4G 7.9G 15% /
tmpfs tmpfs 921M 0 921M 0% /dev
shm tmpfs M 0 M 0% /dev/shm
/dev/vda1 xfs 20G 13G 6.9G 66% /etc/resolv.conf
/dev/vda1 xfs 20G 13G 6.9G 66% /etc/hostname
/dev/vda1 xfs 20G 13G 6.9G 66% /etc/hosts
tmpfs tmpfs 921M 0 921M 0% /run/secrets
tmpfs tmpfs 921M 0 921M 0% /proc/kcore
宿主机中的分区大小信息:
[root@10-10-63-106 ~]# df -lhT
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 xfs 20G 13G 6.9G 65% /
devtmpfs devtmpfs 916M 0 916M 0% /dev
tmpfs tmpfs 921M 0 921M 0% /dev/shm
tmpfs tmpfs 921M M 832M 10% /run
tmpfs tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/vdb xfs 100G 33M 100G 1% /data

热心网友 时间:2022-04-26 14:47

第一种方式更改docker的配置文件
Ubuntu/Debian: edit your /etc/default/docker file with the -g option: DOCKER_OPTS="-dns 8.8.8.8 -dns 8.8.4.4 -g /mnt"
Fedora/Centos: edit /etc/sysconfig/docker, and add the -g option in the other_args variable: ex. other_args="-g /var/lib/testdir". If there's more than one option, make sure you enclose them in " ". After a restart, (service docker restart) Docker should use the new directory.
第二种方式使用连接
1) Stop docker: service docker stop. Verify no docker process is running ps faux
2) Double check docker really isn't running. Take a look at the current docker directory: ls /var/lib/docker/
2b) Make a backup - tar -zcC /var/lib docker > /mnt/pd0/var_lib_docker-backup-$(date +%s).tar.gz
3) Move the /var/lib/docker directory to your new partition: mv /var/lib/docker /mnt/pd0/docker
4) Make a symlink: ln -s /mnt/pd0/docker /var/lib/docker
5) Take a peek at the directory structure to make sure it looks like it did before the mv: ls /var/lib/docker/ (note the trailing slash to resolve the symlink)
6) Start docker back up service docker start
7) restart your containers

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com