为什么在 GitHub Actions 上 Docker 文件系统权限表现不同(服务器上出现权限被拒绝的问题)

4

我有一个带有Dockerfile的应用程序:

# From here on we use the least-privileged `node` user to run the backend.
USER node
WORKDIR /app


# This switches many Node.js dependencies to production mode.
ENV NODE_ENV production

# Copy repo skeleton first, to avoid unnecessary docker cache invalidation.
# The skeleton contains the package.json of each package in the monorepo,
# and along with yarn.lock and the root package.json, that's enough to run yarn install.
COPY --chown=node:node yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./
RUN tar xzf skeleton.tar.gz && rm skeleton.tar.gz

这是由官方的 Backstage CLI 生成的Dockerfile的一部分 (https://github.com/backstage/backstage/issues/15421)。
当我在我的Mac上运行docker build,以及在我的同事的Windows机器上运行时,构建就可以正常工作。
然而,当我们尝试在 GitHub Actions 或 Microsoft ADO 上进行相同的构建时,docker build 就会因为文件系统权限错误而失败。
Step 7/11 : RUN tar xzf skeleton.tar.gz && rm skeleton.tar.gz
 ---> Running in b20314a0495a
tar: packages: Cannot mkdir: Permission denied
tar: packages/app/package.json: Cannot open: No such file or directory
tar: packages: Cannot mkdir: Permission denied
tar: packages/backend/package.json: Cannot open: No such file or directory
tar: Exiting with failure status due to previous errors

我进行了一些谷歌搜索,发现在tar操作之前创建并更改目录的所有权为“node”USER可以解决问题。

因此,实际上这个Dockerfile在我的电脑和GitHub Actions上都可以工作。请注意前两行代码—这就是Dockerfiles之间的全部区别:

RUN mkdir -p /app
RUN chown node /app

# From here on we use the least-privileged `node` user to run the backend.
USER node
WORKDIR /app


# This switches many Node.js dependencies to production mode.
ENV NODE_ENV production

# Copy repo skeleton first, to avoid unnecessary docker cache invalidation.
# The skeleton contains the package.json of each package in the monorepo,
# and along with yarn.lock and the root package.json, that's enough to run yarn install.
COPY --chown=node:node yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./
RUN tar xzf skeleton.tar.gz && rm skeleton.tar.gz

我不明白的是:为什么第一个Dockerfile在Mac/Windows上构建成功,而在GitHub Actions(Linux?)上失败了?为什么GitHub Actions版本需要在文件夹级别上进行额外的所有者更改,而Mac/Windows版本不需要?也许这与Docker版本有关?

1
虽然这不是你所问问题的答案,但我建议将USER语句移到Dockerfile的末尾,并删除COPY --chown选项。这样所有文件都将由root拥有,但是可以被全局读取。你的应用程序将正常运行,但作为一个小的安全改进,它将被禁止意外或故意地覆盖自己的代码。 - David Maze
1个回答

3

我非常确定这个问题是在Linux上特别发生的,因为在Windows和macOS上它运行在一个虚拟机中。它会在“真正的Linux”机器中发生而不在VM中的原因是因为用户ID和组ID在“docker主机”和docker容器之间共享(更多信息请参见这里

Github Actions可能已经为包含在tar.gz文件中的uids/gids以及tar.gz本身指定了权限,而在您的本地macOS/Windows上,Docker的专用VM并没有使用Linux进行任何“真正”的用户管理。

当我使用Bitbucket的CI时,我遇到了类似的问题,它有一些关于uids/gids的奇怪策略。这可能是一个类似的情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接