Home Explore Blog CI



docker

10th chunk of `content/manuals/engine/storage/volumes.md`
0c5da6bdc17fc05e374a42004a5863c10b07633dbc84943b000000010000085e
  --mount='type=volume,dst=/external-drive,volume-driver=local,volume-opt=device=/dev/loop5,volume-opt=type=ext4'
```

You can't run the `mount` command inside the container directly,
because the container is unable to access the `/dev/loop5` device.
That's why the `docker run` command uses the `--mount` option.

#### Example: Mounting a block device in a container

The following steps create an `ext4` filesystem and mounts it into a container.
The filesystem support of your system depends on the version of the Linux kernel you are using.

1. Create a file and allocate some space to it:

   ```console
   $ fallocate -l 1G disk.raw
   ```

2. Build a filesystem onto the `disk.raw` file:

   ```console
   $ mkfs.ext4 disk.raw
   ```

3. Create a loop device:

   ```console
   $ losetup -f --show disk.raw
   /dev/loop5
   ```

   > [!NOTE]
   >
   > `losetup` creates an ephemeral loop device that's removed after
   > system reboot, or manually removed with `losetup -d`.

4. Run a container that mounts the loop device as a volume:

   ```console
   $ docker run -it --rm \
     --mount='type=volume,dst=/external-drive,volume-driver=local,volume-opt=device=/dev/loop5,volume-opt=type=ext4' \
     ubuntu bash
   ```

   When the container starts, the path `/external-drive` mounts the
   `disk.raw` file from the host filesystem as a block device.

5. When you're done, and the device is unmounted from the container,
   detach the loop device to remove the device from the host system:

   ```console
   $ losetup -d /dev/loop5
   ```

## Back up, restore, or migrate data volumes

Volumes are useful for backups, restores, and migrations.
Use the `--volumes-from` flag to create a new container that mounts that volume.

### Back up a volume

For example, create a new container named `dbstore`:

```console
$ docker run -v /dbdata --name dbstore ubuntu /bin/bash
```

In the next command:

- Launch a new container and mount the volume from the `dbstore` container
- Mount a local host directory as `/backup`
- Pass a command that tars the contents of the `dbdata` volume to a `backup.tar` file inside the `/backup` directory.

Title: Example of Mounting and Backing up Data Volumes
Summary
The provided text continues the example of mounting a block device within a Docker container by creating an ext4 filesystem, allocating space, setting up a loop device, and running a container with the mounted device. It also shows how to detach the loop device after usage. Furthermore, it describes backing up data volumes using the `--volumes-from` flag. It demonstrates how to create a container, mount a volume from an existing container, mount a local host directory, and use `tar` to back up the volume's contents.