簡體   English   中英

Dockerfile的.sh腳本RUN的結果未保存到映像

[英]Result of .sh script RUN from Dockerfile is not saved to image

RUN ["./run.sh"] ,可以從腳本內部看到run.sh生成的run.sh夾,但是一旦Docker繼續RUN ["./run.sh"] ,該文件夾就會丟失。

預期的行為:我想訪問run.sh腳本生成的public /文件夾。

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

build之后從run.sh腳本內部獲取ls 公用文件夾是存在的。

...
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將捕獲映像與容器之間的差異結果。 這主要是容器特定的讀/寫文件系統層。 但是,它不包括任何外部卷。

docker記錄了此行為:

  • 從Dockerfile內更改卷:如果在聲明了卷后有任何構建步驟更改了卷內的數據,則這些更改將被丟棄。

我的標准建議是從Dockerfile中刪除任何卷定義。 如果需要卷,請在運行時使用諸如docker compose文件之類的文件對其進行定義。 這允許擴展映像,並防止匿名卷使文件系統混亂。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM