smeeb
smeeb

Reputation: 29567

Docker Data Volumes and Mounting to Host

I just read through the Docker documentation and am trying to understand Docker Data Volumes a little better that the docs were a wee bit fuzzy on.

It is my understanding that there are two ways you can mount a Docker Volume:

But in order for the volume to persist across container restarts or even container swapout (old container is deleted, new one is created/started), wouldn't the volume have to be mounted to the host machine in both approaches? In other words, if I don't explicitly mount to the host via the "Simple Mount method", where is the volume actually being mounted to? And how does this location survive container swapouts?

Also, in both cases, I assume the volume is only local to the given host, and that if you have a Swarm or cluster running on multiple hosts, there's no way to use these commands so that containers running on different hosts could access these volumes, yes? (And I'm guessing this is where Data Volume Containers come into play, right?)

Upvotes: 1

Views: 1218

Answers (1)

Robert
Robert

Reputation: 36883

wouldn't the volume have to be mounted to the host machine in both approaches?!?

Yes, both approaches are based in a host directory that persists through container life cycle.

Use these commands to get more info:

docker volume ls
docker volume inspect <volume-id>
# and
docker inspect <container-id>

(approach 1) where is the volume actually being mounted to?

Do this:

docker inspect <container-id>

Your answer is in "Source":

"Mounts": [
            {
                "Type": "volume",
                "Name": "96f5e6531480cc716cd030f3f60f8927a55728a52d55ad55589752c2b89f2001",
                "Source": "/var/lib/docker/volumes/96f5e6531480cc716cd030f3f60f8927a55728a52d55ad55589752c2b89f2001/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

Note that if you are in OSX or Windows, that Source refers to a local directory inside the VM that runs docker.

And how does this location survive container swapouts?

They survive because they are based on host's local directories (and they are volumes, for that reason they exist).

if you have a Swarm or cluster running on multiple hosts, there's no way to use these commands so that containers running on different hosts could access these volumes, yes?

You are correct regarding simple volume configuration. This is where docker becomes tricky, the persistence. You can implement shared directories at host filesystem level, then mount as volumes in containers so you get shared volumes across cluster's hosts.

https://docs.docker.com/engine/tutorials/dockervolumes/#mount-a-shared-storage-volume-as-a-data-volume

Upvotes: 1

Related Questions