使用 CIFS/SMB 網路檔案系統

Cloud Run 可讓您將容器執行個體連結至 CIFS 和 SMB 網路檔案共用區,以便讀取及寫入共用檔案。您可以使用 smbnetfs 在使用者空間 (而非核心空間) 掛接共用區,或使用 smbclient 傳輸檔案。

如要瞭解 Cloud Run 的其他儲存空間選項,請參閱如何為服務、工作、worker 集區或執行個體設定 NFS 或 Filestore 磁碟區。您也可以為服務、工作、worker 集區或執行個體設定 Cloud Storage FUSE 磁碟區。

限制

  • 使用 smbnetfs 時,必須指定第二代執行環境。

  • 由於 Cloud Run 執行環境限制,CIFS/SMB 無法直接掛接在 Cloud Run 中。不過,您可以使用不依賴核心掛接的 SMB 用戶端,例如 smbnetfs (以 FUSE 為基礎) 或 smbclient。

  • Cloud Run 旨在快速擴充至大量執行個體。不過,大多數網路檔案系統並非設計為可供大量用戶端同時使用。如果是 Cloud Run 服務,建議使用執行個體數量上限功能,限制 Cloud Run 執行個體數量。

正在使用 smbnetfs

smbnetfs 是以 FUSE 為基礎的檔案系統,可讓您將 SMB 共用區掛接為本機檔案系統階層的一部分。

如要搭配使用 smbnetfs 和 Cloud Run,請更新 Dockerfile 來安裝 smbnetfs 及其依附元件,並設定憑證。

在 Dockerfile 中設定 smbnetfs

在 Dockerfile 中新增下列指令:

# Install smbnetfs, fuse and other dependencies
RUN apt-get update && apt-get install -y smbnetfs fuse && rm -rf /var/lib/apt/lists/*

# Setup smbnetfs configuration
RUN mkdir -p ~/.smb
# Check if files exist before copying, as strict locations might vary or be missing in minimal images
RUN [ -f /etc/smbnetfs.conf ] && cp /etc/smbnetfs.conf ~/.smb/ || echo "smbnetfs.conf not found"

# Create the auth file
# In production, use mechanisms like [Secret Manager](/run/docs/configuring/services/secrets) to handle credentials
# securely instead of placing them directly in the Dockerfile.
RUN echo 'auth "<smb-server-ip>" "<smb-username>" "<smb-password>"' > ~/.smb/smbnetfs.auth
RUN chmod 600 ~/.smb/smbnetfs.auth

# Create the host file
RUN echo 'host <smb-server-ip> visible=true' > ~/.smb/smbnetfs.host
RUN chmod 600 ~/.smb/smbnetfs.host

# Enable the includes in the main config if they exist
RUN if [ -f ~/.smb/smbnetfs.conf ]; then \
    sed -i 's/^#include "smbnetfs.auth"/include "smbnetfs.auth"/' ~/.smb/smbnetfs.conf && \
    sed -i 's/^#include "smbnetfs.host"/include "smbnetfs.host"/' ~/.smb/smbnetfs.conf; \
    fi

將 <smb-server-ip>、<smb-username> 和 <smb-password> 替換為 SMB 伺服器的連線詳細資料。

在容器啟動期間掛接 SMB 共用區

在容器啟動後、應用程式啟動前,執行 smbnetfs 來掛接共用區。如果容器是以非超級使用者身分執行,您可能需要調整權限或 FUSE 設定。

  1. 建立掛接點,例如 /mnt/smb,如以下指令所示:

    mkdir /mnt/smb

  2. 使用 smbnetfs 指令掛接 SMB 檔案系統:

    smbnetfs /mnt/smb

  3. 使用下列指令,在 /mnt/smb/<smb-server-ip>/<share-name> 下存取共用區:

    ls /mnt/smb/smb-server-ip/share-name

疑難排解

使用 smbnetfs 搭配 O_APPEND 旗標寫入檔案時,可能會發生輸入/輸出錯誤。詳情請參閱 Stack Overflow 討論。

使用 smbclient

smbclient 是指令列工具,提供類似 FTP 的介面,可用於存取 SMB 共用區。您可以使用這個工具,在 SMB 伺服器上轉移檔案或執行指令。

  • 為避免在每個指令中提供憑證,您可以將憑證儲存在檔案中。

    1. 將憑證儲存在檔案中,例如 smb-cred.txt:

      username = <smb-username>
      password = <smb-password>
      
    2. 除了憑證檔案,您也可以使用 -U 或 --user 選項,在每個指令中提供憑證,例如:--user=[DOMAIN\]USERNAME[%PASSWORD]。

    3. 使用 smbclient 工具搭配 -A 旗標,指定憑證檔案。例如,如要列出共用區中的檔案,請執行下列指令:

      smbclient //smb-server-ip/share-name -A smb-cred.txt -c 'ls'

  • 如要使用 smbclient 工具修改檔案,請下載檔案、在本機修改,然後按照下列步驟將檔案上傳回共用區。

    1. 執行 get 指令,下載檔案:

      smbclient //smb-server-ip/share-name -A smb-cred.txt -c 'get remote local'

    2. 在本機修改檔案。

    3. 執行 put 指令,上傳修改後的檔案:

      smbclient //smb-server-ip/share-name -A smb-cred.txt -c 'put local remote'

使用 smbclient 工具時,SMB 伺服器會根據您提供的憑證識別您的身分。這樣伺服器才能套用正確的檔案權限。 如需 smbclient 指令的完整清單,請參閱官方 smbclient 說明文件。