gpt4 book ai didi

shell - 来自 Dockerfile 的 .sh 脚本 RUN 的结果未保存到镜像

转载 作者:行者123 更新时间:2023-12-02 20:46:19 27 4
gpt4 key购买 nike

之后 RUN ["./run.sh"] ,由run.sh 制作的文件夹从脚本内部可见,但一旦 Docker 继续,它就会丢失。

预期行为:
我想访问 公开/run.sh 生成的文件夹脚本。

Dockerfile

  ...
RUN mkdir -p /opt/site
WORKDIR /opt/site
VOLUME /opt/site
COPY . .

RUN ["chmod", "+x", "./run.sh"]
RUN ["./run.sh"]
RUN pwd
RUN ls
RUN ls public

FROM nginx
COPY --from=build-stage /opt/site/public /usr/share/nginx/html

脚本
  #!/usr/bin/env bash
rm -rf public/ node_modules/ node_modules/.bin/ package-lock.json yarn.lock
npm install
ls
touch newfile.txt
npm run build
ls
lsbuild 之后的 run.sh 脚本内部. 公众 文件夹存在。
...
Generated public/sw.js, which will precache 6 files, totaling 197705 bytes.
info Done building in 44.842 sec
*ls*
Dockerfile
config
gatsby-config.js
gatsby-node.js
newfile.txt
node_modules
package-lock.json
package.json
postcss.config.js
public
run.sh
src
static
tailwind.css
tailwind.js
ls从 Dockerfile 内部。 公众 文件夹丢失,尝试与之交互会导致失败。
Removing intermediate container 1692fb171673
---> 474d83267ccb
Step 10/14 : RUN pwd
---> Running in 7c351b151904
/opt/site
Removing intermediate container 7c351b151904
---> bae37da8b513
Step 11/14 : RUN ls
---> Running in 384daf575cae
Dockerfile
config
gatsby-config.js
gatsby-node.js
package-lock.json
package.json
postcss.config.js
run.sh
src
static
tailwind.css
tailwind.js
Removing intermediate container 384daf575cae
---> 1f6743a4adc1
Step 12/14 : RUN ls public
---> Running in 7af84c5d72a0
ls: cannot access public: No such file or directory
The command '/bin/sh -c ls public' returned a non-zero code: 2
ERROR: Job failed: exit code 2

最佳答案

您已使用所选目录创建了一个卷:

  VOLUME /opt/site

在镜像中定义时,将为从该镜像创建的每个容器创建一个卷。如果您没有指定卷的来源(在构建时不能这样做),docker 将创建一个匿名卷。对于命名卷和匿名卷,docker 会将内容初始化为该位置的图像的内容。

RUN 命令的结果如下:
  • 创建临时容器
  • 该临时容器运行您请求的命令并在继续之前验证退出代码
  • 如果成功,docker 会捕获图像和容器之间差异的结果。这主要是容器特定的读/写文件系统层。但是,它不包括任何外部卷。

  • 此行为是 documented by docker :

    • Changing the volume from within the Dockerfile: If any build steps change the data within the volume after it has been declared, those changes will be discarded.


    我的标准建议是从 Dockerfile 中删除任何卷定义。 如果您需要一个卷,请在运行时使用诸如 docker compose 文件之类的东西来定义它。这允许扩展镜像,并防止匿名卷弄乱文件系统。

    关于shell - 来自 Dockerfile 的 .sh 脚本 RUN 的结果未保存到镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53278556/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com