简体   繁体   English

尝试以非root用户身份从容器内部写入已装入的卷时出现问题

[英]Issue when trying to write to a mounted volume from inside a container as a non-root user

I'm working with a container that will be running ZooKeeper but I'm running into issues with permissions on the host volumes that I mount into my container. 我正在使用一个将运行ZooKeeper的容器,但是我遇到了关于我装入容器的主机卷的权限问题。

This is my setup: 这是我的设置:

On the host machine (Ubuntu 14.04): 在主机上(Ubuntu 14.04):

  • Created a "zookeeper" system user (id=106) and group (id=111). 创建了“zookeeper”系统用户(id = 106)和组(id = 111)。
  • Created the directory "/var/log/zookeeper" and set its ownership to zookeeper (ie. chown zookeeper:zookeeper). 创建目录“/ var / log / zookeeper”并将其所有权设置为zookeeper(即chown zookeeper:zookeeper)。 This is the directory that I will be mounting into my container. 这是我将安装到容器中的目录。

Inside the container (Ubuntu 14.04): 容器内部(Ubuntu 14.04):

  • Also created a "zookeeper" system user (id=102) and group (id=105) which I use as the user from which to execute the command in the ENTRYPOINT. 还创建了一个“zookeeper”系统用户(id = 102)和group(id = 105),我将其用作在ENTRYPOINT中执行命令的用户。
  • Create the same directory "/var/log/zookeeper" that will be mounted to and also set its ownership to zookeeper:zookeeper (although I don't think this matters). 创建将要挂载的相同目录“/ var / log / zookeeper”,并将其所有权设置为zookeeper:zookeeper(虽然我认为这不重要)。

Once I start up my container with the /var/log/zookeeper mount and I open up a shell inside of the container as the zookeeper user (that was created inside the container), I find that I get a "Permission Denied" error if I try to create a file in the mounted directory /var/log/zookeeper. 一旦我使用/ var / log / zookeeper mount启动我的容器,我在容器内部打开一个shell作为zookeeper用户(在容器内创建),我发现我得到一个“Permission Denied”错误,如果我尝试在挂载目录/ var / log / zookeeper中创建一个文件。 When I do an "ls -l" to look at the ownership of this directory (still inside of the container) it looks something like this: 当我执行“ls -l”查看此目录的所有权(仍然在容器内)时,它看起来像这样:

drwxr-xr-x 2  106  111   4096 Jun 30 17:18 zookeeper

The 106 and 111 in this case correspond to the zookeeper user and group ids of the host machine which I think is where the issue lies. 在这种情况下,106和111对应于主机的zookeeper用户和组ID,我认为这是问题所在。 I tried opening a shell inside of the container but this time I went in as the root user and the scenario that I described above worked perfectly fine just that root was the owner of the file that got created (which was expected). 我尝试在容器内部打开一个shell,但这次我以root用户身份进入,上面描述的场景完全正常,只是root是创建的文件的所有者(这是预期的)。

From this I concluded that I need to either: 由此我得出结论,我需要:

(a) Run the application inside of my container as the default root user instead of the zookeeper user that I create. (a)在我的容器内运行应用程序作为默认的root用户,而不是我创建的zookeeper用户。

(b) Create a zookeeper user and group both on my host machine and inside the container whose id's are exactly the same. (b)在我的主机上和id完全相同的容器内创建一个zookeeper用户和组。

Neither case is ideal because for (a) , running the application as the root user could have potential security issues (from what I've read anyways), and for (b) , it can be very difficult to get the id's to match due to the fact that they might already be taken by other users that were created (which you don't have any control over). 这两种情况都不理想,因为对于(a) ,以root用户身份运行应用程序可能会产生潜在的安全问题(无论如何我都读过),对于(b) ,要使id匹配到期是非常困难的事实上,他们可能已被其他已创建的用户(您无法控制)使用。

Has anyone ever dealt with something like this before? 有没有人曾经处理过这样的事情? Are there other possible solutions that I might be overlooking? 我可能会忽略其他可能的解决方案吗?

据我所知,容器内和主机上的用户ID和组ID应该匹配,以便让主机授予您对共享目录的权限。

Very important to see the difference between running a production and a development container. 查看运行生产开发容器之间的区别非常重要。 Afaik, there's no real issue if your Docker container runs as root, even on production. Afaik,如果您的Docker容器以root身份运行,即使在生产中也没有真正的问题。 However you should never want or need to mount a volume of production. 但是,您永远不应该或者需要安装大量的生产。 If you want to run it as a zookeeper feel free to do so. 如果您想以动物园管理员的身份运行它,请随意这样做。

// Edit: The more I read, the more I'm convinced there actually might be a security issue when running stuff as root, so you better not doing so on production. //编辑:我读的越多,我就越相信在以root身份运行东西时可能存在安全问题,所以最好不要在生产中这样做。

The solution to try and match uid and gid is viable only for small/local project - it really does make it unportable. 尝试匹配uidgid的解决方案仅适用于小型/本地项目 - 它确实使其无法移植。 You can try and set an arbitrary high uid and gid and then do the same on each of your devs machines, but that doesn't mean it'll always be fine. 您可以尝试设置任意高的uidgid ,然后在每台开发机器上执行相同操作,但这并不意味着它总是很好。

tl;dr : On development run chmod -R 0777 on existing files and then umask 0000 to setup permissions on files and directories created later. tl; dr开发时在现有文件上运行chmod -R 0777 ,然后使用umask 0000来设置稍后创建的文件和目录的权限。 Then you can mount and edit your files as you please, no matter what user created it. 然后,无论用户创建文件,您都可以随意安装和编辑文件。

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

相关问题 docker 无法使用非 root 用户写入已安装的卷 - docker can not write on mounted volume with non-root user 如何让 Docker 容器中的非 root 用户访问安装在主机上的卷 - How to give non-root user in Docker container access to a volume mounted on the host 以非 root 用户身份运行的 docker 容器进程无法写入 docker 卷 - docker container process running as non-root user cannot write to docker volume 如何在docker中以非root用户身份写入卷容器? - How do I write to a volume container as non-root in docker? 如何在Docker容器中为非root用户设置卷权限 - How to set volume permission in docker container for non-root user 以非root用户身份对IBM Containers Volume的写许可权 - Write permissions for IBM Containers Volume as non-root user 以非 root 用户身份启动自定义 Docker MySQL 容器时,ibdata1 写入出错 - Error with ibdata1 write when starting customised Docker MySQL container as non-root user 以非 root 用户身份在 Docker 容器内运行 tcpdump - Running tcpdump inside a Docker container as non-root user Docker:在容器内使用非root用户执行命令 - Docker: executng commands with non-root user inside container Permission denied error invoking Docker on Mac host from inside Docker Ubuntu 容器作为非根用户 - Permission denied error invoking Docker on Mac host from inside Docker Ubuntu container as non-root user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM