2019年8月24日 星期六

[ 常見問題 ] How to move docker's default /var/lib/docker to another directory

Source From Here 
Question 
The following config will guide you through a process of changing the docker's default /var/lib/docker storage disk space to another directory. There are various reasons why you may want to change docker's default directory from which the most obvious could be that ran out of disk space. The following guide should work for both Ubuntu and Debian Linux or any other systemd system. Make sure to follow this guide in the exact order of execution. 

How-To 
Let's get started by modifying systemd's docker start up script. Open file /usr/lib/systemd/system/docker.service with your favorite text editor and replace the following line where /new/path/docker is a location of your new chosen docker directory. First of all, let's check the status of docker service: 
# systemctl status docker
  1. ● docker.service - Docker Application Container Engine  
  2.    Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)  
  3.    Active: active (running) since Sat 2019-08-24 15:14:36 CST; 2min 3s ago  
  4.      Docs: https://docs.docker.com  
  5. Main PID: 16867 (dockerd)  
  6. ...  

# ps aux | grep docker | grep -v grep
root 16867 0.1 0.9 721104 73972 ? Ssl 15:14 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

# dockerd --help | grep '/var/lib/docker'
--data-root string Root directory of persistent Docker state (default "/var/lib/docker") 

From above, we know we can use argument --data-root to switch the root directory of docker service. Let's modify /usr/lib/systemd/system/docker.service to meet our request: 
# df -h // It seems /datas is a good path to be root of docker service
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 19G 1.8G 92% /
devtmpfs 3.7G 0 3.7G 0% /dev
tmpfs 3.7G 0 3.7G 0% /dev/shm
tmpfs 3.7G 8.5M 3.7G 1% /run
tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup
/dev/sda2 20G 1.1G 18G 6% /datas
tmpfs 748M 0 748M 0% /run/user/0


# systemctl stop docker // Stop docker service
# mkdir /datas/docker
# vi /usr/lib/systemd/system/docker.service
  1. # ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock  <--- original="" span="">  
  2. ExecStart=/usr/bin/dockerd --data-root /datas/docker -H fd:// --containerd=/run/containerd/containerd.sock  

# systemctl daemon-reload
# systemctl start docker // Start docker service
# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2019-08-24 15:24:52 CST; 4s ago
...


# ps aux | grep -i docker | grep -v grep
root 8548 0.2 0.8 590032 68412 ? Ssl 15:24 0:00 /usr/bin/dockerd --data-root /datas/docker -H fd:// --containerd=/run/containerd/containerd.sock

Now we can double confirm the effectiveness of our setting by pull image: 
# du --max-depth=1 -h /datas/docker/ | grep image
40K /datas/docker/image

// Seasrch image for PostgreSQL
# d search postgresql
... 
// Pull image of PostgreSQL
# d pull postgres
# du --max-depth=1 -h /datas/docker/ | grep image
1.2M /datas/docker/image

We can observe that the size of target folder is increasing as expected. 

Supplement 
FAQ - How to Change the Default Docker Directory t...e You Some Precious Disk Space

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...