我有一个带有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版本有关?
USER
语句移到Dockerfile的末尾,并删除COPY --chown
选项。这样所有文件都将由root拥有,但是可以被全局读取。你的应用程序将正常运行,但作为一个小的安全改进,它将被禁止意外或故意地覆盖自己的代码。 - David Maze