"docker volume" vs "docker run -v" - docker

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

Related

interest of docker create volume command?

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

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

invalid argument for "--mount" flag: unexpected key 'addr'

I want to use docker nfs volume.
What I have tried:
1. Create a volume first then use it, it's OK
docker volume create --driver local --opt type=nfs --opt o=nfsvers=4,addr=10.192.244.109 --opt device=:/var/lib/lava/dispatcher/tmp my1
docker run -it --rm --name nfs-test -v my1:/data alpine sh
2. Directly use volume when docker run, it's also OK
docker run -it --rm --name nfs-test --mount type=volume,volume-driver=local,dst=/data,volume-opt=type=nfs,volume-opt=device=:/var/lib/lava/dispatcher/tmp,"volume-opt=o=addr=10.192.244.109" alpine sh
The problem happens when I want to specify nfsvers=4 in docker run:
# docker run -it --rm --name nfs-test --mount type=volume,volume-driver=local,dst=/data,volume-opt=type=nfs,volume-opt=device=:/var/lib/lava/dispatcher/tmp,"volume-opt=o=nfsvers=4,addr=10.192.244.109" alpine sh
invalid argument "type=volume,volume-driver=local,dst=/data,volume-opt=type=nfs,volume-opt=device=:/var/lib/lava/dispatcher/tmp,volume-opt=o=nfsvers=4,addr=10.192.244.109" for "--mount" flag: unexpected key 'addr' in 'addr=10.192.244.109'
See 'docker run --help'.
You could see Item1 shows we could specify nfs version when use nfs volume, while Item2 shows we could directly use nfs volume within docker run without pre-create a volume.
But, how I could specify nfs version when directly use docker run? What's the correct format here?
This works for me:
--mount 'type=volume,dst=/data,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/lib/lava/dispatcher/tmp,"volume-opt=o=addr=10.192.244.109,rw,nfsvers=4"'
Seems like the argument parser is picky with the quotes.
It is parsed as an extra argument then.
Alternately you can use
..,volume-opt=o=nfsvers=4,volume-opt=o=addr=10.192.244.109

How to set options for volumes specified in Dockerfile

I can set volume options when creating a volume:
$ docker volume create --driver local \
--opt type=tmpfs \
--opt device=tmpfs \
--opt o=size=100m,uid=1000 \
foo
or when I run a container with a --mount flag:
$ docker run \
--mount 'type=volume,src=<VOLUME-NAME>,dst=<CONTAINER-PATH>,volume-driver=local,volume-opt=type=nfs,volume-opt=device=<nfs-server>:<nfs-path>,"volume-opt=o=addr=<nfs-address>,vers=4,soft,timeo=180,bg,tcp,rw"'
<IMAGE>
But how to set options for volumes created in Dockerfile?:
FROM ubuntu
VOLUME /myvol
Looking at the docs, I can only see a flag for setting just a volume driver:
--volume-driver Optional volume driver for the container
In general, if there are "options" for things you might specify in a Dockerfile, you can't set them there. For a VOLUME you can't specify any specific host path , named volume, or device; for an EXPOSEd port you can't specify that it be published on a specific host interface; and so on.
In most cases I'd suggest avoiding a Dockerfile VOLUME declaration, since it mostly has only confusing side effects (notably, preventing any later RUN command from modifying that directory). You will always need to use a docker run -v or similar option to mount a named volume into the container, and that doesn't need a matching VOLUME in the image.
If you do docker run -v to explicitly mount something on a directory declared as a VOLUME, that mount replaces the implicitly created anonymous volume.

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

Resources