Try   HackMD

🛠️ 【軟體】必學的 Docker 🛠️

Container 文件權限問題

下圖中,device-coding/ 是被 mount 進 container 的。
其中 .cache 是當容器運作時產生的快取檔案。

如果我直接在 host machine 中刪除,就會跳出如右錯誤

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

當然,看到 permission denied 就知道加 sudo 即可解決。
但這裡會多補充一些事情。



1. 原因

通常這是發生在容器是以 root 身分運行,而 host machine 是以 user 身分登入。
所以 user 沒有權限刪除由 root 生出的檔案。

可用 ls -la 確認這點:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

終端訊息解釋
  • 左大欄 dwxr
    • 第一欄:d 為目錄、- 為普通文檔
    • 右邊九個要分成 3|3|3 看,分別代表 owner | group user | other users。
      • w 為寫權限、r 為讀權限、x 為可執行權限。
  • 數字欄位
    • 對於目錄文件:含有的子目錄數。
    • 對於普通文檔:鏈接數量。
  • 兩欄名稱欄位
    • 第一欄:user name
    • 第二欄:group name
  • 容量欄位
  • 右大欄日期欄位

2. 解決方法

  • Method 1:在容器內直接刪除。

  • Method 2:在主機上以 root 身分刪除,sudo rm -rf

  • Method 3:更改文件擁有者:sudo chown

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • Method 4:更改 volume 路徑。
    不要 mount 整個 device-decoding/ 進 container 內,而是將 device-decoding/ 內的各子文件或目錄一一掛載進容器內。

  • Method 5:volumes 參數。

    ​volumes: ​ - /device-docding:/device-docding:rw