Blog

How to create Docker volume device or host path?

Docker

How to create Docker volume device or host path?

Creating Docker volumes with either device or host paths allows you to manage data persistently outside the container. There are different types of volumes, including host path volumes, which allow you to mount a directory from the host machine into the container.

Lets see how you can create a Docker volume using a host path:

Docker Volume with Host Path

  1. Create a Docker Volume:

Use the docker volume create command to create a volume, linking it to a host path:

docker volume create --driver local \
  --opt type=none \
  --opt device=/host/path \
  --opt o=bind my_volume

Replace /host/path with the actual path on your host machine.

2. Use the Volume in a Container:

Run a container and mount the volume:

docker run -d --name my_container -v my_volume:/path/in/container my_image

Replace /path/in/container with the path in the container where you want to mount the volume.

Docker Volume with Device :

  1. Create a Block Device:

Create a block device on your host machine. For instance, /dev/sdb can be used as a device. Ensure the device is initialized and formatted properly if needed (like using mkfs).

2. Mount Device to Docker Container:

Use the device when starting your Docker container by specifying it with the –device flag:

docker run -d --name my_container --device=/dev/sdb:/path/in/container my_image

Replace /dev/sdb with your device path and /path/in/container with the path in the container where you want to mount the device.

These methods allow you to create Docker volumes using device paths or host paths for persistent data storage.

Spread the love

Leave your thought here

Your email address will not be published. Required fields are marked *