我运行docker-compose up后由于权限错误而无法编辑本地文件

时间:2018-12-23 04:05:31

标签: docker docker-compose composer-php file-permissions

我运行docker-compose up -d,然后ssh进入容器。我可以很好地通过localhost加载站点,但是当我尝试在本地编辑源代码时,由于权限错误,它不允许我进行加载。这是容器vs本地上的ls -la输出:

容器:

enter image description here

本地

enter image description here

我的dockerfile具有chown命令:

enter image description here

我的本​​地用户称为 pwm 。我尝试从主机运行chown -R pwm:pwm ../app,此时我可以编辑文件,但随后出现laravel权限被拒绝错误。然后,我需要再次运行chown -R www-data:www-data ../app进行修复。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

对于开发环境,我的解决方案是在以root身份启动的容器内设置入口点脚本,更改容器内的用户以使其与卷装载中文件/目录所有者的用户相匹配(将是您在主机上的用户),然后切换到该用户以运行该应用。我在我的基本映像存储库中有一个示例,以及在您自己的容器中实现此操作所需的脚本:https://github.com/sudo-bmitch/docker-base

在那儿,fix-perms脚本可以完成繁重的工作,包括如下代码:

  INPUT_METHOD_MANAGER__SERVED_VIEW(SDK_INT >= ICE_CREAM_SANDWICH_MR1 && SDK_INT <= N_MR1) {
    @Override void add(ExcludedRefs.Builder excluded) {
      String reason = "When we detach a view that receives keyboard input, the InputMethodManager"
          + " leaks a reference to it until a new view asks for keyboard input."
          + " Tracked here: https://code.google.com/p/android/issues/detail?id=171190"
          + " Hack: https://gist.github.com/pyricau/4df64341cc978a7de414";
      excluded.instanceField("android.view.inputmethod.InputMethodManager", "mNextServedView")
          .reason(reason);
      excluded.instanceField("android.view.inputmethod.InputMethodManager", "mServedView")
          .reason(reason);
      excluded.instanceField("android.view.inputmethod.InputMethodManager",
          "mServedInputConnection").reason(reason);
    }
  },

  INPUT_METHOD_MANAGER__ROOT_VIEW(SDK_INT >= ICE_CREAM_SANDWICH_MR1 && SDK_INT <= M) {
    @Override void add(ExcludedRefs.Builder excluded) {
      excluded.instanceField("android.view.inputmethod.InputMethodManager", "mCurRootView")
          .reason("The singleton InputMethodManager is holding a reference to mCurRootView long"
              + " after the activity has been destroyed."
              + " Observed on ICS MR1: https://github.com/square/leakcanary/issues/1"
              + "#issuecomment-100579429"
              + " Hack: https://gist.github.com/pyricau/4df64341cc978a7de414");
    }
  },

该脚本在启动时以root用户身份在容器内运行。我运行的入口点的最后一步将调用:

# update the uid
if [ -n "$opt_u" ]; then
  OLD_UID=$(getent passwd "${opt_u}" | cut -f3 -d:)
  NEW_UID=$(stat -c "%u" "$1")
  if [ "$OLD_UID" != "$NEW_UID" ]; then
    echo "Changing UID of $opt_u from $OLD_UID to $NEW_UID"
    usermod -u "$NEW_UID" -o "$opt_u"
    if [ -n "$opt_r" ]; then
      find / -xdev -user "$OLD_UID" -exec chown -h "$opt_u" {} \;
    fi
  fi
fi

以应用程序用户身份运行容器命令,作为新的pid 1可执行文件。