【问题标题】:Docker user cannot write to mounted folderDocker 用户无法写入已安装的文件夹
【发布时间】:2019-05-07 20:59:22
【问题描述】:

我有以下设置:

  selenium-chrome:
    image: selenium/node-chrome-debug:3.141.59-neon
    container_name: chrome-e2e
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
      - SHM-SIZE=2g
      - GRID_DEBUG=false
      - NODE_MAX_SESSION=1
      - NODE_MAX_INSTANCES=5
      - TZ=Europe/Brussels
    hostname: chrome-e2e
    networks:
      - build-network
    ports:
      - 5900:5900
    volumes:
      - ./target:/home/seluser/Downloads

Selenium 测试在容器内运行,实际的测试代码在容器外。使用 Maven,我们处理容器的生命周期。 如您所见,我将 Chrome 下载文件夹(在容器内)安装到我的应用程序的 target-文件夹中。一切都安装得很好,但是当 Chrome 尝试下载文件时,写入/home/seluser/Downloads 的权限被拒绝。 /home/seluser/Downloads 的 UID 和 GID 被 Docker 设置为 2100:2100。 Chrome 本身通过seluseruser 运行。

我需要怎么做才能给seluser 写入2100 拥有的文件夹的权限?

提前致谢。 问候

【问题讨论】:

    标签: docker


    【解决方案1】:

    Linux 中的绑定挂载不会对 uid 或 gid 执行任何命名空间,并且主机挂载在后台运行绑定挂载。因此,如果容器内的 uid 与主机上的 uid 不同,则会出现权限问题。我已经在其他容器中使用 fix-perms 脚本解决了这个问题。实现类似于以下 Dockerfile:

    FROM selenium/node-chrome-debug:3.141.59-neon
    COPY --from=sudobmitch/base:scratch /usr/bin/gosu /usr/bin/fix-perms /usr/bin/
    COPY entrypoint.sh /entrypoint.sh
    # use a chmod here if you cannot fix permissions outside of docker
    RUN chmod 755 /entrypoint.sh 
    USER root
    ENTRYPOINT [ "/entrypoint.sh" ]
    

    entrypoint.sh 看起来像:

    #!/bin/sh
    if [ "$(id -u)" = "0" -a -d "/home/seluser/Downloads" ]; then
      fix-perms -r -u seluser /home/seluser/Downloads
      exec gosu seluser /opt/bin/entry_point.sh "$@"
    else
      exec /opt/bin/entry_point.sh "$@"
    fi
    

    这里发生的情况是容器以 root 身份启动,并且 fix-perms 脚本调整容器内的 seluser 以匹配 /home/seluser/Downloads 目录的 uid。 exec gosu 然后将您的容器进程作为 seluser 作为新的 pid 1 运行。

    您可以在以下位置查看用于实现此功能的代码:https://github.com/sudo-bmitch/docker-base

    我已经在我的几个演讲中讨论过这种方法,包括:https://sudo-bmitch.github.io/presentations/dc2019/tips-and-tricks-of-the-captains.html#fix-perms

    【讨论】:

      【解决方案2】:

      您可以创建自己的 Dockerfile:

      FROM selenium/node-chrome-debug:3.141.59-neon
      
      VOLUME /home/seluser/Downloads
      

      docker-compose.yml 应该是:

        selenium-chrome:
          build: .
          container_name: chrome-e2e
          depends_on:
            - selenium-hub
        ...
      

      VOLUME 指令将使用当前用户(在本例中为 seluser)创建目录,这样 Docker 就不必使用其他用户创建目录。

      【讨论】:

      • 为什么我需要创建一个单独的 Dockerfile?这不能通过 docker-compose 本身来完成吗?
      • 文件夹需要从图像内部创建,只能通过这种方式完成。如果您使用 docker-compose 映射卷,那么您将控制权交给 docker,您就会得到报告的问题。这是我的解决方案,但希望有人可以提出更符合您期望的建议
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 2015-07-18
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多