interest of docker create volume command? - docker

i actually play a lot with docker,
and i really don't understand the interest of using the command
docker volume create <volume>
In fact, doing this
docker volume create my_data
docker run --rm -ti -v my_data:/src bash
and only this
docker run --rm -ti -v my_data:/src bash
give exactly the same result, as, in the two scenarii, docker
creates the volume
makes the mapping perfectly
So: what is the interest of the 'create' command ?

As #David Maze already said, you can specify non-default volume options with the docker volume create command, such as labels, a custom driver and options for this driver. The documentation has some interesting examples:
For example, the following creates a tmpfs volume called foo with a size of 100 megabyte and uid of 1000.
docker volume create --driver local \
--opt type=tmpfs \
--opt device=tmpfs \
--opt o=size=100m,uid=1000 \
foo
Another example that uses btrfs:
docker volume create --driver local \
--opt type=btrfs \
--opt device=/dev/sda2 \
foo
Another example that uses nfs to mount the /path/to/dir in rw mode from 192.168.1.1:
docker volume create --driver local \
--opt type=nfs \
--opt o=addr=192.168.1.1,rw \
--opt device=:/path/to/dir \
foo

Related

How to mount a samba directory in docker container from a windows host?

I'm using docker v20.10.17 in a windows system. I'd like to run a container (jupyterhub/jupyterhub) and mount a directory into it to share data (for all users with write permission inside the container system).
The shared directory is from another NAS (//192.168.1.5/folder/shared_data), which can be opened properly from the host system. I followed the Create CIFS/Samba volumes instruction here to create a samba volume first:
PS C:\Users\Administrator> docker volume create \
--driver local \
--opt type=cifs \
--opt device=//192.168.1.5/folder/shared_data \
--opt o=addr=192.168.1.5,username=myusername,password=mypassword,file_mode=0777,dir_mode=0777 \
--name cif-volume
Which successfully created a volume named cif-volume. Then:
PS C:\Users\Administrator>docker run --rm -it -p 18000:8000 \
--name jhubcontainer \
--cap-add SYS_ADMIN \
--cap-add DAC_READ_SEARCH \
--privileged \
-v cif-volume:/etc/skel/shared_data jupyterhub-image
And I got error message like this:
docker: Error response from daemon: failed to mount local volume: mount //192.168.1.5/folder/shared_data:/var/lib/docker/volumes/cif-volume/_data, data: username=myusername,password=mypassword,file_mode=0777,dir_mode=0777: operation not supported.
See 'docker run --help'.
Need help with this.
Update 2022/11/03 with updated message:
I tried to update my command based on the help of Slava Kuravsky, but still got errors. Within the previous question, I used a pseudo address and username. I'll paste the exact command I used with the real address and username, without any modifications.
PS C:\Users\Administrator> docker volume create --driver local --opt type=cifs --opt device="//172.16.90.50/public/shared_data" --opt o=addr=172.16.90.50,username=212,password=ziyuan,file_mode=0777,dir_mode=0777,vers=2.0 --name cif-volume
cif-volume
PS C:\Users\Administrator> docker run -it --rm -p 18000:8000 --name jhubcontainer -v cif-volume:/etc/skel/shared_data jupyterhub-20221021-mountsmb
docker: Error response from daemon: failed to mount local volume: mount //172.16.90.50/public/shared_data:/var/lib/docker/volumes/cif-volume/_data, data: addr=172.16.90.50,username=212,password=ziyuan,file_mode=0777,dir_mode=0777,vers=2.0: invalid argument.
See 'docker run --help'.
PS C:\Users\Administrator>
The docker image is "jupyterhub/jupyterhub", and "jupyterhub-20221021-mountsmb" is a backup after installed some other python packages and configures.
To make sure the address is accessable, I tried:
PS C:\Users\Administrator> net use m: \\172.16.90.50\public\shared /user:212 ziyuan
命令成功完成。
The printout "命令成功完成" means "Command succeed". And I can see my mounted driver "M:" in explorer
Add the cifs version to the volume options: vers=2.0
docker volume create \
--driver local \
--opt type=cifs \
--opt device=//192.168.1.5/folder/shared_data \
--opt o=addr=192.168.1.5,username=myusername,password=mypassword,file_mode=0777,dir_mode=0777,vers=2.0 \
--name cif-volume
Worked for me without any additional privileges and capabilities
docker run -it --rm --name cifs -v cif-volume:/mnt ubuntu:latest ls /mnt

How to mount EFS with TLS (Encryption) inside a docker container on premises?

I am Trying to mount an EFS inside a docker container running on Premise.
I have tried it with no tls:
docker volume create \
--driver local \
--opt type=nfs \
--opt o=addr=IP-12.29.29.29,rw,nfsvers=4.1,rsize=1048576,tls,wsize=1048576,hard,timeo=600,retrans=2,noresvport \
--opt device=:/ efs-test
docker run --rm -it -v /nfs:/nfs/test -v efs-test:/efs-test --name ubuntu -d ubuntu
it works fine. I can access EFS and write data.
If I use the encryption option while creating the volume, i create a en error:
docker volume create \
--driver local \
--opt type=nfs \
--opt o=addr=IP-12.29.29.29,rw,nfsvers=4.1,rsize=1048576,tls,wsize=1048576,hard,timeo=600,retrans=2,noresvport \
--opt device=:/ efs-test
docker run --rm -it -v /nfs/efs/:/nas/test -v efs-test:/efs-test --name sync -d sync
docker: Error response from daemon: error while mounting volume
'/global/docker/volumes/efs-test/_data': failed to mount local volume: mount :/:/global/docker/volumes/efs-test/_data,
data: addr=10.3.77.107,nfsvers=4.1,rsize=1048576,tls,wsize=1048576,hard,timeo=600,retrans=2,noresvport: invalid argument.
How do I mount an EFS with TLS from on premises using Docker?

Receive "connection refused" when I try to run a Docker Container with NFS Volume

I'm working with NFS Volume.
I created a NFS server and on my rasberry pi I set the client and if I mount the directory exposed I can see the content, It's mean that the configuration works.
My goal is to create a volume with the following command:
sudo docker volume create --driver local \
--opt type=nfs \
--opt o=addr=10.0.0.5,rw \
--opt device=:/export/users/reddata \
foo
As I saw in the documentation the create a NFS volume.
My problem is the follow, when I run the container:
sudo docker run -it -p 1880:1880 -v foo:/data --name mynodered -d nodered/node-red
I receive the following error:
docker: Error response from daemon: failed to mount local volume: mount :/export/users/reddata:/var/lib/docker/volumes/foo/_data, data: addr=10.0.0.5: connection refused.
See 'docker run --help'.
I this that something it's not authorized, but I also think that I can mount my shared directory on my pi the configuration previous did should works.
Do you have any idea?
Thanks for your time
Alessandro
I solved the problem.
The problem was the way to create the volume, the right way is:
sudo docker volume create --driver local \
--opt type=nfs \
--opt o=addr=10.0.0.5,nfsvers=4 \
--opt device=:/export/users/reddata \
foo
It's necessary to specify the nfsvers=4.
Another important configuration, in my case it's not necessary to supervise the host that access my folder, in the server my /etc/exports it the follow:
export/users/reddata *(rw,sync,no_subtree_check,insecure)
With the *, I specify all IPs on the network.
Best Regards
Alessandro

"docker volume" vs "docker run -v"

https://docs.docker.com/engine/reference/commandline/volume_create/#driver-specific-options
docker volume create --driver local \
--opt type=nfs \
--opt o=addr=192.168.1.1,rw \
--opt device=:/path/to/dir \
foo
if I create a volume as such, how would I consume it?
docker run -v foo:/foo?
Then, what's the benefits of creating the volume first? couldnt I also do
docker run -v /path/to/dir:/foo?
if /path/to/dir is already NFS mounted on the host?
Originally, the -v or --volume flag was used for standalone containers and the --mount flag was used for swarm services.
You can also use --mount with standalone containers. In general, --mount is more explicit and verbose.
The biggest difference
the -v syntax combines all the options together in one field.
the --mount syntax separates them.
If you need to specify volume driver options, you must use --mount
Read details here

How to specify the size of a shared Docker volume?

If I would like to create a data volume of let´s say 15GB that would be of type ext4, how would I do that?
docker volume create --name vol just creates an empty volume.
docker volume create --opt type=ext4 --name vol creates an ext4 volume but I cannot specify the size of it since ext4 does not support it according to the mount options of ext4.
It is possible to specify the size limit while creating the docker volume using size as per the documentation
Here is example command provided in the documentation to specify the same
docker volume create -d flocker -o size=20GB my-named-volume
UPDATE Some more examples from git repository:
The built-in local driver on Linux accepts options similar to the linux
mount command:
$ docker volume create --driver local --opt type=tmpfs --opt device=tmpfs --opt o=size=100m,uid=1000
Another example:
$ docker volume create --driver local --opt type=btrfs --opt device=/dev/sda2
Using Docker Compose I was able to do it the following way:
volumes:
tmpfs:
# For details, see:
# https://docs.docker.com/engine/reference/commandline/volume_create/#driver-specific-options
driver: local
driver_opts:
o: "size=$TMPFS_SIZE"
device: tmpfs
type: tmpfs
Leaning on the answer of Rao, there are now some udpated docs and example on their docs.docker page:
Create a volume and pass the size option with the opt flag
This example volume creation is taken from this page:
$ docker volume create --driver local \
--opt type=tmpfs \
--opt device=tmpfs \
--opt o=size=100m,uid=1000 \
foo

Resources