Idan Shalom Agamy
Idan Shalom Agamy

Reputation: 31

Trying to understand VOLUME command in dockerfile and its directories

I've started using docker on my local computer using windows just recently and I'm in a stage where I'm trying to understand volume. To my understanding, in the commend line you can simply connect local directory to the container directory with adding the command:

-v [host location]:[container location]

And when using dockerfile you specify the container directory:

VOLUME [container location]

So when this is running using the

docker volume ls

I can see a volume created on my computer

DRIVER              VOLUME NAME
local               3d25e0608d5500c4dba29f8c3bc7b5ba4d5348e6fbd54644a20a5701f0b9f427

My problem is I try to understand WHERE on my computer this volume is?

using inspect giving me this result:

[
    {
        "CreatedAt": "2019-05-01T13:47:41Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/3d25e0608d5500c4dba29f8c3bc7b5ba4d5348e6fbd54644a20a5701f0b9f427/_data",
        "Name": "3d25e0608d5500c4dba29f8c3bc7b5ba4d5348e6fbd54644a20a5701f0b9f427",
        "Options": null,
        "Scope": "local"
    }
]

and I can't understand WHERE is "/var/lib/docker/volumes/..." exactly. Is it on the virtual linux drive the docker is using?

Upvotes: 2

Views: 104

Answers (1)

David Maze
David Maze

Reputation: 158657

You're not supposed to be able to directly access Docker named volumes (or anything else that lives in /var/lib/docker). The official Docker examples on, for example, backup and restore for named volumes launch additional containers mounting the same volumes for basic data-management operation.

If you need to directly access the data in a volume, using a host-directory bind-mount is probably the easiest option.

You should avoid writing VOLUME directives in your Dockerfile. You don't need it to use docker run -v to mount a directory into a container; its key effect is to automatically add a -v option for you if you didn't already have one. In practice, you'll get no benefit and suffer from some confusing side effects (notably, RUN steps will make no persistent changes inside a VOLUME directory).

The anonymous volume you're seeing in docker volume ls is probably the result of your VOLUME directive.

Upvotes: 1

Related Questions