---
# System prepended metadata

title: '[Linux] How to install docker in a disk other than the root disk(Azure VM)'
tags: [linux, 'docker ']

---



## Step 1: Check the disk size
```bash
df -h
```

## Step 2: Create a new disk
1. Go to Azure portal
2. Find the VM
3. Add a new disk

## Step 3: Format the disk
```bash
sudo mkfs -t ext4 /dev/sdc
```

## Step 4: Mount the disk
```bash
sudo mkdir /data
sudo mount /dev/sdc /data
```

## Step 5: Add the disk to /etc/fstab
```bash
echo "/dev/sdc /data ext4 defaults 0 0" | sudo tee -a /etc/fstab
```

## Step 6: Install docker
1. Remove the old docker and some related packages
```bash
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine \
                  podman \
                  buidah \
                  runc
2. Set up the repository
```bash
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
```
3. Install docker engine
    - List the available versions
    ```bash
    yum list docker-ce --showduplicates | sort -r
    ```
    - Install the latest version or a specific version
    ```bash
    sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
    ```
4. Start docker
```bash
sudo systemctl start docker
sudo systemctl enable docker
```
5. Verify the installation
```bash
sudo docker --version
sudo systemctl status docker
```
6. Add the current user to the docker group, let the user use docker without sudo
```bash
sudo usermod -aG docker $USER
newgrp docker
```

## Step 7: Modify the server initial location

1. new a folder in the new disk
```bash
sudo mkdir /data/docker
```
2. modify the docker service file
```bash
sudo vim /usr/lib/systemd/system/docker.service
```
- Original
```yaml
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[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
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target
```
- Modified (docker verion < 1.2)
```yaml
[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 -g /home/docker # modify this line
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
```
- Modified (docker version >= 1.2)
```yaml
[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 --data-root /home/docker
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
```
3. reload the service
```bash
sudo systemctl daemon-reload
sudo systemctl enable docker
sudo systemctl restart docker
```

## Reference
- [安裝docker及docker-compose (CentOS8-stream)
](https://hackmd.io/P_3HM5jRTf-VWEhRxanB5g)
- [使用入口網站將資料磁碟連結至 Linux VM](https://learn.microsoft.com/zh-tw/azure/virtual-machines/linux/attach-disk-portal?tabs=ubuntu)