11

When starting a container and specifying a volume you can optionally append a third field that's a comma separated list of options like rw.

docker run -v /some-host/path:/some-container/path:rw

This same options are applicable in the docker.compose.yml

services:
  myService:
    image: some/image
    volumes:
      - /some-host/path:/some-container/path:rw

I thought that specifing rw would mean that the container would be able to read from and write to that directory (regardless of user). Contrary to my belief, when the host directory doesn't exist, docker creates it as drwxr-xr-x 2 root root no matter what I specify. The application in the container is not running on root though, so it tries to write to the mounted drive and get's Permission denied.

I've dug through the docker documents, even found this github issue describing the same issue, but can't find anything definitive that explains expected behavior.

So what exactly does rw(read/write) mean when specified as a third option for bind mounted directories?

4
  • No, but doesn't it need to be an absolute path? I'm not sure if my code examples were too generic, causing confusion, so I updated.
    – bflemi3
    Dec 13, 2018 at 20:03
  • I'm guessing read/write
    – danny117
    Dec 13, 2018 at 20:05
  • lol good guess :)
    – bflemi3
    Dec 13, 2018 at 20:06
  • ...in the same way that / on your host is mounted read-write but isn’t world-writable on every file; if it were mounted read-only nobody could write any file.
    – David Maze
    Dec 13, 2018 at 20:09

1 Answer 1

7

As DavidMaze says in the comments

in the same way that / on your host is mounted read-write but isn’t world-writable on every file; if it were mounted read-only nobody could write any file.

And the docs:

If neither 'rw' or 'ro' is specified then the volume is mounted in read-write mode.

And

If you supply an absolute path for the host-dir, Docker bind-mounts to the path you specify.

The directory is "mounted" as rw by default. So think that to write in a directory it is not enough a rw mount, you also need file permissions on it. In the other hand, having full files permissions is not enough if the directory is mounted as read only. Think it as two layers permissions.

Also:

There is clear value in the ability to make bind mounts read-only, though. Containers are one example: an administrator may wish to create a container in which processes may be running as root. It may be useful for that container to have access to filesystems on the host, but the container should not necessarily have write access to those filesystems.

2
  • I think I understand... so I'd need to grant permission to the specific user in the container via Dockerfile or script. rw just says, "it can be written to IF the correct permissions are granted to the appropriate group/user"?
    – bflemi3
    Dec 14, 2018 at 13:30
  • 2
    Yes. I understand that it can be misleading for some people which want to be abstracted about the underliying implementation. You see rw which actually means rw-if-proper-permissions-are-set
    – Robert
    Dec 14, 2018 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.