Docker COPY 递归 --chmod --chown

Bog*_*atu 2 docker dockerfile

我正在尝试将一些文件和目录递归复制到 Docker 映像中。源目录包含文件和一个子目录,其中包含一些文件。

\n
src/\n\xe2\x94\x9c\xe2\x94\x80 subdir/\n\xe2\x94\x82  \xe2\x94\x9c\xe2\x94\x80 sub_file_1\n\xe2\x94\x82  \xe2\x94\x9c\xe2\x94\x80 sub_file_2\n\xe2\x94\x9c\xe2\x94\x80 file_1\n\xe2\x94\x9c\xe2\x94\x80 file_2\n...\n\xe2\x94\x9c\xe2\x94\x80 file_n\n
Run Code Online (Sandbox Code Playgroud)\n

我在 Dockerfile 中运行以下命令:

\n

COPY --chown=user:user --chmod=600 src/ /dst/

\n

这些权限正确应用于所有顶级文件 ( file_1to file_n) 和子目录本身,但不适用于subdir(例如 sub_file_1) 中的文件。

\n

进入容器并运行ls,输出为:

\n
user@container:/$ ls -la /dst/subdir\nls: cannot access '/dst/subdir/sub_file_1': Permission denied\nls: cannot access '/dst/subdir/sub_file_2': Permission denied\n\ntotal 0\nd????????? ? ? ? ?            ? .\nd????????? ? ? ? ?            ? ..\n-????????? ? ? ? ?            ? sub_file_1\n-????????? ? ? ? ?            ? sub_file_2\n
Run Code Online (Sandbox Code Playgroud)\n

有没有办法递归应用命令的--chmod选项?--chownCOPY

\n

小智 7

  • 简短的回答: 您必须通过将您的 替换chmod为--chmod=700. 您可以在下面查找更多详细信息,或者直接谷歌“Linux 文件权限”。

  • 长答案:所以我重新创建了您的示例,正​​如您在下面看到的,我有一些脚本在我的服务器实际启动之前在各个阶段运行。我遵循良好的生产 Docker 实践,正如您所见,我还使用您的方法来更改脚本文件的权限。

    
# App files
COPY --chown=node:node --from=builder /usr/src/app/build ./build
# Migration files
COPY --chown=node:node ./migrations ./migrations
# Scripts
COPY --chown=node:node --chmod=600 ./scripts ./scripts
# Client
COPY --chown=node:node ./client ./build/client
EXPOSE 80
Run Code Online (Sandbox Code Playgroud)

毫不奇怪,我遇到了类似的错误。

    sh: ./scripts/wait-for.sh: Permission denied
    npm ERR! code ELIFECYCLE
    npm ERR! errno 126
    npm ERR! webpage-server@0.0.1 preprod: `./scripts/wait-for.sh page-db:5432 -- npm run migrate -- --config build/src/config/database.json -e production `
    npm ERR! Exit status 126
Run Code Online (Sandbox Code Playgroud)

这里的问题是你给你的 chmod 的代码。您的目标是使它们可执行,并且出于安全原因使它们仅对节点(默认)docker 用户可执行。现在,我们提供给我们的这些十进制数字chmod只是我们眼中的糖,实际上,这些数字将转换为二进制并分配给目录及其子文件/文件夹的每一个权限变量(在本例中)。Linux 文件系统权限中有 4 个值,一位表示文件或目录,3 位定义用户对文件/目录的访问权限,另外 3 位表示用户组,另外 3 位表示其他所有人,您想要授予执行权限对于当前用户,因此您将需要111=7最终的结果chmod看起来更像这样:

    # App files
    COPY --chown=node:node --from=builder /usr/src/app/build ./build
    # Migration files
    COPY --chown=node:node ./migrations ./migrations
    # Scripts
    COPY --chown=node:node --chmod=700 ./scripts ./scripts
    # Client
    COPY --chown=node:node ./client ./build/client
    EXPOSE 80
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,问题不再存在

    > webpage-server@0.0.1 preprod /usr/src/app
    > ./scripts/wait-for.sh page-db:5432 -- npm run migrate -- --config build/src/config/database.json -e production 
    
    
    > webpage-server@0.0.1 migrate /usr/src/app
    > db-migrate up "--config" "build/src/config/database.json" "-e" "production"
    
    [INFO] No migrations to run
    [INFO] Done
    
    > webpage-server@0.0.1 prod /usr/src/app
    > npm run start
    
    
    > webpage-server@0.0.1 start /usr/src/app
    > node build/src/server.js
    
    18:28:17 info: Initializing thanos webpage Server version: 0.0.1. Environment: development
    18:28:17 info: /usr/src/app/build/src
    18:28:17 info: Thanos web page server is listening on port -> 80
Run Code Online (Sandbox Code Playgroud)