Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

BKM: Change Docker Data Location

Xintian Wu edited this page Jul 25, 2022 · 11 revisions

The docker engine by default stores the data (images and containers) under the /var/lib/docker directory. Usually this is a space-limited root partition. Use the following steps to change the docker data directory:

  • Create new docker data directory under /home:
sudo mkdir /home/docker
sudo chown root.docker /home/docker
sudo chmod 700 /home/docker
  • Modify /etc/docker/daemon.json to point to the new directory:
{
    "graph": "/home/docker"
}
  • Restart docker:
sudo systemctl stop docker
sudo systemctl start docker
  • Remove the old data directory:
sudo rm -rf /var/lib/docker

Change Containerd Data Folder

If you haven't, create a containerd configuration file as follows:

sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml

Change the containerd data storage to /home/containerd:

sed -i 's|^root =.*|root = "/home/containerd"|' /etc/containerd/config.toml
sudo mkdir -p /home/containerd
sudo systemctl stop containerd
sudo rm -rf /var/lib/containerd
sudo systemctl restart containerd

Change the Kubelet data folder

Add --root-dir=/home/kubelet to the kubelet options in /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf, as follows:

[Service]
...
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS --root-dir=/home/kubelet

Then perform the following operations:

sudo mkdir /home/kubelet
sudo systemctl daemon-reload
sudo systemctl restart kubelet
  • To test if Kubernetes properly recognizes your new data folder, try the following script:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
  labels:
     app: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      enableServiceLinks: false
      containers:
        - name: test
          image: busybox
          imagePullPolicy: IfNotPresent
          command: ["sh","-c","while true; do df -h /mnt; sleep 1; done"]
          volumeMounts:
            - mountPath: /mnt
              name: storage
      volumes:
        - name: storage
          emptyDir: {}