3

I'm reading Docker's user guide section on volumes at: https://docs.docker.com/engine/userguide/containers/dockervolumes/

At the very first part it indicates how to create and add a volume to a container, this I will call "method 1":

You can use the -v multiple times to mount multiple data volumes. Now, mount a single volume in your web application container.

$ docker run -d -P --name web -v /webapp training/webapp python app.py

This will create a new volume inside a container at /webapp.

Later on it goes to talk about Data volume containers, this I will call "method 2":

If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it’s best to create a named Data Volume Container, and then to mount the data from it.

$ docker create -v /dbdata --name dbstore training/postgres /bin/true

Here's what I don't get, what's the difference between implementing a volume through simply using the -v command with docker run (method 1) v.s. implementing a volume by using a "Data volume container" (method 2)?

It seems both ways you are just creating a volume that is persistent through the life of image's non-persistent containers, correct me if I'm wrong.

Jose V
  • 1,655
  • 1
  • 17
  • 31
  • My understanding is that `create` creates a container but does not run it, so you get a data only container that does not consume any CPU. You can then share the volumes from this container with many other containers. – Xiongbing Jin Apr 20 '16 at 14:43
  • @warmoverflow Actually you can `create` a volume without putting it into a container, when i posted the question i was very confused so it's not very clear, but the difference is that in method 1 you don't put the volume inside a container, in method 2 you put it inside a container, making it a "Data Container" or something. – Jose V Apr 20 '16 at 15:48
  • In method 1 the volume is inside the container, as the documentation says. And yes you can create a volume without container, but you need to use `docker volume create`, not `docker create -v`. – Xiongbing Jin Apr 20 '16 at 15:52
  • oh yeah sorry, that's right, the question should be what's the difference between using `docker create -v /dbdata --name dbstore training/postgres /bin/true` vs using `docker volume create --name hello`, notice both are done with `create` but the first one gets created as a container (the documentation calls it "a data volume container"), both are meant to work as a volume that you can bind with a container when you initialize it with `run`. I might as well re-submit the question, the docker document is just kind of confusing about it. – Jose V Apr 20 '16 at 16:04
  • 1
    This is just the evolution of Docker. It used to be that you need to create a container to use the data volume container pattern. I believe that `docker volume create` is a recent addition to simplify this. – Xiongbing Jin Apr 20 '16 at 17:11

1 Answers1

1

In my understanding, at least from docker 1.12 on, a volume is always the same thing: some persistant data stored somewhere on the host. But there are different ways to manage a volume:

  1. Create it and mount it to a container (your method 1). With this, you can use "volumes-from" when you want to mount volumes of one container into another container
  2. Create the volume using docker volume create, and let docker manage it. It will be saved somewhere on the host system (e.g. /var/lib/docker), but you don't really care where and how exactly.
  3. Mount a host directory as a volume, in which case you know where it is mounted on your host and your are "responsible" for it.

Then, there are "tricks" (as described here. For instance:

"if you create a named volume by running a new container from image by docker run -v my-precious-data:/data imageName, the data within the container under /data will be copied into the named volume."

So the way you create/manage your volume might differ a bit, but it essentially always is a volume.

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95