更改 Windows 主机上 docker 容器内挂载文件夹的文件权限

Change file permissions in mounted folder inside docker container on Windows Host

Disclaimer/Edit 2

几年后,对于阅读此问题的每个人 - 如果您使用 Windows 并希望将 docker 与 linux 容器一起使用,我强烈建议您不要使用 docker windows 而不是完全在 VM 内启动整个 docker 环境。这个 Ext3 NTFS 问题会在很多不同的层面上打破你的脖子,安装 docker-machine 可能甚至不值得付出努力。


编辑:

我正在使用 docker-machine,它在 Virtualbox VM 中启动一个 boot2docker 实例,在 /c/Users 上有一个共享文件夹,您可以从中将卷装载到容器中。所述卷的权限是问题所在。虚拟机存储在 /c/Users/tom/.docker/

我选择在 Hyper-V 上使用 docker-machine Virtualbox 工作流,因为我在日常工作流中需要 VBox,并且 运行不可能在一个系统上同时使用 Hyper-V 和 Virtualbox,因为不同管理程序之间的不兼容性。

原题

我目前正在尝试在 windows 上的容器中设置 PHPMyAdmin,但我无法更改 config.inc.php 文件的权限。

我发现: 并认为这可能有些相关,但它似乎仅适用于 MongoDB。

这是我的docker-compose.yml

    version: "3"

    services:
      pma:
        image: (secrect company registry)/phpmyadmin
        ports: 
          - 9090:80
        volumes:
          - /c/Users/tom/projects/myproject/data/var/www/public/config.inc.php:/var/www/public/config.inc.php

现在,当我 docker exec -it [container] bash 并更改挂载目录时,我尝试 运行 chmod config.inc.php 但由于某种原因,它失败了 默默.

root@22a4bag43245: ls -la config.inc.php
-rw------- 1 root root 0 Aug 11 15:11 config.inc.php
root@22a4bag43245: chmod 655 config.inc.php
root@22a4bag43245: ls -la config.inc.php
-rw------- 1 root root 0 Aug 11 15:11 config.inc.php

考虑到链接的答案,我认为我可以将卷移出我的 Userhome,但 vbox 根本不会装载该文件夹。

如何永久更改 /var/www/public/config.inc.php 的文件权限?

尝试以下方法之一:

  1. 如果可以重建image image: image: (secrect company registry)/docker-stretchimal-apache2-php7-pma 然后在docker文件里面,添加如下内容

    USER root RUN chmod 655 config.inc.php

然后您可以重建映像并将其推送到注册表,您所做的应该会起作用。这应该是您的首选解决方案,因为您不希望每次启动新容器时都手动更改权限

  1. 尝试显式使用 root 用户执行

    docker exec -it -u root [container] bash

我遇到了同样的问题,即使在使用 chown 之后也无法更改所有权。正如我 researched,这是因为 NTFS 卷安装在 ext 文件系统中。所以我用了另一种方法。

docker 的内部卷没有这些问题。因此,您可以将文件挂载到内部 docker 卷上,然后在本地文件夹中的任意位置创建指向该文件的硬符号链接:

sudo ln $(docker volume inspect --format '{{ .Mountpoint }}' <project_name>_<volume_name>) <absolute_path_of_destination>

这样您就可以将文件放在所需的位置,在 docker 内并且没有任何权限问题,并且由于硬符号链接,您将能够像在正常卷安装中一样修改文件的内容。

Here is a working implementation of this process which mounts and links a directory. In case you wanna know about the details, see possible fix section in issue.

编辑

实施此方法的步骤:

  1. 将相关文件装入内部 docker 卷(也称为 named volumes)。
  2. 在创建硬链接之前,请确保那里存在卷和相关文件。为确保这一点,您应该至少有一次 运行 您的容器,或者如果您想自动创建此文件,您可以包含一个 docker 运行 来创建所需的文件并退出。

    docker run --rm -itd \ -v "<Project_name>_<volume_name>:/absolute/path" \ <image> bash -c "touch /absolute/path/<my_file>"

这 docker 运行 将创建卷和所需的文件。这里,container 是我的项目名称,默认情况下,它是项目所在文件夹的名称,<volume_name> 与我们要在原始容器中使用的名称相同。 <image> 可以与您的原始容器中已经使用的相同。

  1. 在您的 OS 中创建一个硬链接到您系统上的实际文件位置。您可以使用 docker volume inspect --format '{{ .Mountpoint }}' <project_name>_<volume_name>/<my_file> 找到文件位置。 Linux 用户可以在终端中使用 ln,windows 用户可以在命令提示符中使用 mklink

在第 3 步中我们没有使用 /absolute/path,因为 <volume_name> 已经引用了那个位置,我们只需要引用文件。