简体   繁体   English

共享卷/文件权限/所有权(Docker)

[英]Shared volume/file permissions/ownership (Docker)

I'm having a slightly annoying issue while using a Docker container (I'm on Ubuntu, so no virtualization like VMWare or b2d). 在使用Docker容器时我遇到了一个有点烦人的问题(我在Ubuntu上,所以没有像VMWare或b2d那样的虚拟化)。 I've built my image, and have a running container that has one shared (mounted) directory from my host, and one shared (mounted) file from my host. 我已经构建了我的映像,并且有一个正在运行的容器,它有一个来自我的主机的共享(挂载)目录,以及一个来自我的主机的共享(挂载)文件。 Here's the docker run command in full: 这是完整的docker run命令:

docker run -dit \
-p 80:80 \
--name my-container \
-v $(pwd)/components:/var/www/components \
-v $(pwd)/index.php:/var/www/index.php \
my-image

This works great, and both /components (and its contents) and the file are shared appropriately. 这很好用,两个/components (及其内容)和文件都是适当共享的。 However, when I want to make changes to either the directory (eg adding a new file or folder), or edit the mounted file (or any file in the directory), I'm unable to do so due to incorrect permissions. 但是,当我想要更改目录(例如添加新文件或文件夹),或编辑已安装的文件(或目录中的任何文件)时,由于权限不正确,我无法这样做。 Running ls- lFh shows that the owner and group for the mounted items have been changed to libuuid:libuuid . 运行ls- lFh表示已挂载项的ownergroup已更改为libuuid:libuuid Modifying either the file or parent directory requires root permissions, which impedes my workflow (as I'm working from Sublime Text, not Terminal, I'm presented with a popup for admin privs). 修改文件或父目录需要root权限,这会阻碍我的工作流程(因为我使用的是Sublime Text,而不是Terminal,我会看到管理员权限的弹出窗口)。

Why does this occur? 为什么会这样? How can I work around this / handle this properly? 我该如何解决这个/正确处理? From Managing Data Volumes: Mount a Host File as a Data Volume : 管理数据卷:将主机文件作为数据卷安装

Note: Many tools used to edit files including vi and sed --in-place may result in an inode change. 注意:许多用于编辑文件的工具(包括vi和sed --in-place)可能会导致inode更改。 Since Docker v1.1.0, this will produce an error such as “sed: cannot rename ./sedKdJ9Dy: Device or resource busy”. 从Docker v1.1.0开始,这将产生一个错误,例如“sed:无法重命名./sedKdJ9Dy:设备或资源忙”。 In the case where you want to edit the mounted file, it is often easiest to instead mount the parent directory. 在您要编辑已安装文件的情况下,通常最容易安装父目录。

This would seem to suggest that instead of mounting /components and /index.php , I should instead mount the parent directory of both. 这似乎表明,而不是mount /components/index.php ,我应该安装两者的父目录。 Sounds great in theory, but based on the behavior of the -v option and how it interacts with /directory , it would seem that every file in my parent directory would be altered to be owned by libuuid:libuuid . 理论上听起来很棒,但是基于-v选项的行为以及它与/directory交互方式,似乎我父目录中的每个文件都会被libuuid:libuuid所拥有。 Additionally, I have lots of things inside the parent directory that are not needed in the container - things like build tools, various files, some compressed folders, etc. Mounting the whole parent directory would seem to be wasteful. 另外,我在容器中有很多东西在容器中不需要 - 比如构建工具,各种文件,一些压缩文件夹等。挂载整个父目录似乎是浪费。

Running chown user:group on /components and /index.php on my host machine allow me to work around this and seem to continue to sync with the container. 在我的主机上运行chown user:group on /components/index.php允许我解决这个问题并且似乎继续与容器同步。 Is this something I'll need to do every time I run a container with mounted host volumes? 这是我每次运行装有主机卷的容器时都需要做的事吗? I'm guessing that there is a more efficient way to do this, and I'm just not finding an explanation for my particular use-case anywhere. 我猜测有一种更有效的方法可以做到这一点,我只是在任何地方找不到我的特定用例的解释。

I am using this container for development of a module for another program, and have no desire to manage a data-only container - the only files that matter are from my host; 我正在使用这个容器来开发另一个程序的模块,并且不想管理一个只有数据的容器 - 唯一重要的文件来自我的主机; persistence isn't needed elsewhere (like a database, etc). 其他地方不需要持久性(如数据库等)。

  • Dockerfile Dockerfile
  • /setup /建立
  • Created on pastebin to avoid an even longer post. 在pastebin上创建,以避免更长的帖子。 Never expires. 永不过期。

After creating the image, this is the run command I'm using: 创建映像后,这是我正在使用的运行命令:

docker run -dit \
    -p 80:80 \
    --name my-container \
    -v $(pwd)/components:/var/www/wp-content/plugins/my-plugin-directory/components \
    -v $(pwd)/index.php:/var/www/wp-content/plugins/my-plugin-directory/index.php \
    my-image

It looks like your chown -R nginx:nginx ... commands inside your container are changing the ownership bits on your files to be owned by libuuid on your host machine. 看起来您容器中的chown -R nginx:nginx ...命令正在更改您的文件上的所有权位,以便由libuuid上的libuuid拥有。

See Understanding user file ownership in docker: how to avoid changing permissions of linked volumes for a basic explanation on how file ownership bits work between your host and your docker containers. 请参阅了解docker中的用户文件所有权:如何避免更改链接卷的权限,以获取有关文件所有权位在主机和docker容器之间如何工作的基本说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM