Search code examples
dockerdocker-machinedocker-container

mounting a docker volume to a docker container


This Works

To create the docker volume without specifying the disk size:

  docker volume create disk1

To mount the volume(disk1) to a container

  docker run -itd -v disk1:/data ubuntu

This is Not Working

Now creating the docker volume by specifying a size of 100mb

  docker volume create --name disk2 --opt o=size=100m

To mount the volume(disk2 which is of size 100 MB) to a container

  docker run -itd -v disk2:/data ubuntu

when I run these commands I was getting the following error

docker: Error response from daemon: error while mounting volume '/var/lib/docker/volumes/disk2/_data': missing device in volume options.


Solution

  • This error occurs because set of driver options are missing

    "--opt type=" and "--opt device=" is mandatory when you are providing with size of the docker volume "--opt o=size="

    So create volume with all the mandatory options and link with the container.

    try

     docker volume create --name disk2 --opt type=tmpfs --opt device=tmpfs --opt o=size=100m
    

    then

     docker run -itd -v disk2:/data ubuntu
    

    It works.