Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between -volume and -volumes-from in Docker Volumes

Tags:

What is the exact difference between the two flags used in docker volume commands -v and --volumes-from. It seems to me that they are doing the same work, consider the following scenario.

First lets create a volume named myvol using command:

$ docker volume create myvol

Now create and run a container named c1 that uses myvol and get into his bash:

$ docker run -it --name c1 -v myvol:/data nginx bash

Lets create a file test.txt in the mounted directory of the container as:

root@766f90ebcf37:/# touch /data/test.txt
root@766f90ebcf37:/# ls /data
test.txt

Using -volume flag:

Now create another container named c2 that also uses myvol:

$ docker run -it --name c2 -v myvol:/data nginx bash

As expected, the new generated container c2 also have access to all the files of myvol

root@393418742e2c:/# ls /data
test.txt  

Now doing the same thing with --volumes-from

Creating a container named c3 using volumes from container c1

$ docker run -it --name c3 --volumes-from c1 nginx bash

This will result the same thing in c3:

root@27eacbe25f92:/# ls /data
test.txt

The point is if -v and --volumes-from are working the same way i.e. to share data between containers then why they are different flags and what --volumes-from can do that -v cannot do?

like image 318
Haseeb Anwar Avatar asked Oct 17 '19 14:10

Haseeb Anwar


1 Answers

The point is if -v and --volumes-from are working the same way i.e. to share data between containers

-v and --volumes-from are not working the same way, but with both of them you can share data between containers.

what --volumes-from can do that -v cannot do?

E.g. it can connect to another containers volumes without knowing how volumes are named and you do not specify path. You are able to add suffixes to containers ID's, with permissions like :ro or :rw. More details here - Mount volumes from container (--volumes-from) section

The other question is what -v can do that --volumes-from cannot do?
E.g. it can mount named volumes, you can mount host directories or tmpfs (this one you cannot share between containers). In --volume-from you cannot share data with host directly.

The conclusion is: the purpose of --volume is to share data with host. (here you can find more use cases).
The purpose of --volumes-from is to share the data between containers.
They both works nice together.

like image 197
Rumid Avatar answered Sep 29 '22 09:09

Rumid