我使用Docker已经有一段时间了,在处理持久数据时不断发现同样的问题。

我创建Dockerfile并公开一个卷或使用——volumes-from在容器中挂载一个主机文件夹。

主机上的共享卷需要申请哪些权限?

我可以想到两个选择:

So far I've given everyone read/write access, so I can write to the folder from the Docker container. Map the users from host into the container, so I can assign more granular permissions. Not sure this is possible though and haven't found much about it. So far, all I can do is run the container as some user: docker run -i -t -user="myuser" postgres, but this user has a different UID than my host myuser, so permissions do not work. Also, I'm unsure if mapping the users will pose some security risks.

还有其他选择吗?

你们是如何处理这个问题的?


当前回答

尝试在Dockerfile中添加命令

RUN usermod -u 1000 www-data

Credits转至https://github.com/denderello/symfony-docker-example/issues/2#issuecomment-94387272

其他回答

如果你这样做是为了开发,一个很好的解决方案是使用bindfs:

保持容器用户拥有源代码。(如果可能的话,让容器克隆源代码。) 使用bindfs并为主机用户映射文件夹。

下面是我的docker-compose设置现在的样子:

project:
  web/src # Container clones it using init scripts.
  web/log
  __web__/src # Host user uses this. It's just bindfs mirror.
  __web__/log

我已经考虑这个问题一年多了,bindfs是我遇到过的最简单的选择。除了克隆,没有运行成本。

在大多数情况下,这可能不是最好的方法,但它还没有被提及,所以也许它会帮助到某些人。

绑定挂载主机卷 主机文件夹FOOBAR挂载在容器/卷/FOOBAR中 修改容器的启动脚本,以找到感兴趣的卷的GID $ TARGET_GID=$(stat -c "%g" /volume/FOOBAR) 确保您的用户属于一个具有此GID的组(您可能必须创建一个新组)。对于本例,我将假装我的软件在容器内以nobody用户运行,因此我希望确保没有人属于组id等于TARGET_GID的组

  EXISTS=$(cat /etc/group | grep $TARGET_GID | wc -l)

  # Create new group using target GID and add nobody user
  if [ $EXISTS == "0" ]; then
    groupadd -g $TARGET_GID tempgroup
    usermod -a -G tempgroup nobody
  else
    # GID exists, find group name and add
    GROUP=$(getent group $TARGET_GID | cut -d: -f1)
    usermod -a -G $GROUP nobody
  fi

我喜欢这样做是因为我可以轻松地修改主机卷上的组权限,并且知道这些更新的权限适用于docker容器内部。这种情况无需对我的主机文件夹/文件进行任何权限或所有权修改,这让我很高兴。

我不喜欢这种方法,因为它假定将自己添加到容器内的任意组(碰巧使用您想要的GID)中没有危险。它不能与Dockerfile中的USER子句一起使用(除非该用户具有根权限)。此外,它是一种黑客工作;-)

如果你想要硬核,你显然可以在许多方面扩展它——例如,搜索任何子文件上的所有组,多个卷,等等。

为了保护和更改docker容器和docker主机的根,请尝试使用——uidmap和——private-uids选项

https://github.com/docker/docker/pull/4572#issuecomment-38400893

此外,您还可以删除docker容器中的几个功能(——cap-drop)以确保安全性

http://opensource.com/business/14/9/security-for-docker

更新支持应该出现在docker > 1.7.0中

更新版本1.10.0 (2016-02-04)add——users -remap标志 https://github.com/docker/docker/blob/master/CHANGELOG.md#security-2

我的方法是检测当前的UID/GID,然后在容器中创建这样的用户/组,并在他下面执行脚本。因此,他将创建的所有文件都将与主机上的用户匹配:

# get the location of this script no matter what your current folder is, this might break between shells so make sure you run bash
LOCAL_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# get current IDs
USER_ID=$(id -u)
GROUP_ID=$(id -g)

echo "Mount $LOCAL_DIR into docker, and match the host IDs ($USER_ID:$GROUP_ID) inside the container."

docker run -v $LOCAL_DIR:/host_mount -i debian:9.4-slim bash -c "set -euo pipefail && groupadd -r -g $GROUP_ID lowprivgroup && useradd -u $USER_ID lowprivuser -g $GROUP_ID && cd /host_mount && su -c ./runMyScriptAsRegularUser.sh lowprivuser"

如果你使用Docker Compose,以特权模式启动容器:

wordpress:
    image: wordpress:4.5.3
    restart: always
    ports:
      - 8084:80
    privileged: true