本頁說明如何在 Cloud Run 中使用 CIFS/SMB 網路檔案系統。
如果您在 Cloud Run 上使用 NFS、Filestore 或 Cloud Storage FUSE,請參閱下列頁面:
限制
使用
smbnetfs時,您必須指定第二代執行環境。由於 Cloud Run 執行環境的限制,CIFS/SMB 無法直接掛接在 Cloud Run 內。不過,您可以使用不依賴核心掛接的 SMB 用戶端,例如
smbnetfs(以 FUSE 為基礎) 或smbclient。Cloud Run 的設計宗旨是快速擴充至大量執行個體。不過,大多數網路檔案系統並非為大量用戶端同時使用而設計。如果是 Cloud Run 服務,建議使用執行個體數量上限功能,限制 Cloud Run 執行個體數量。
正在使用 smbnetfs
smbnetfs 是以 FUSE 為基礎的檔案系統,可讓您掛接 SMB 共用區,做為本機檔案系統階層的一部分。
如要在 Cloud Run 中使用 smbnetfs,請更新 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 設定。
建立掛接點,例如
/mnt/smb,如下列指令所示:mkdir /mnt/smb使用
smbnetfs指令掛接 SMB 檔案系統:smbnetfs /mnt/smb在
/mnt/smb/<smb-server-ip>/<share-name>下存取共用資料夾,如下列指令所示:ls /mnt/smb/smb-server-ip/share-name
疑難排解
使用 O_APPEND 旗標搭配 smbnetfs 將內容寫入檔案時,可能會發生輸入/輸出錯誤。詳情請參閱 Stack Overflow 討論。
使用 smbclient
smbclient 是一項指令列工具,提供類似 FTP 的介面,可存取 SMB 共用資料夾。您可以使用這項工具在 SMB 伺服器上傳輸檔案或執行指令。
為避免在每個指令中提供憑證,您可以將憑證儲存在檔案中。
將憑證儲存在檔案中,例如
smb-cred.txt:username = <smb-username> password = <smb-password>除了憑證檔案,您也可以使用
-U或--user選項,在每個指令中提供憑證,例如:--user=[DOMAIN\]USERNAME[%PASSWORD]。使用
smbclient工具和-A旗標指定憑證檔案。舉例來說,如要列出共用資料夾中的檔案,請執行下列指令:smbclient //smb-server-ip/share-name -A smb-cred.txt -c 'ls'
如要使用 smbclient 工具修改檔案,請下載檔案、在本機修改,然後按照下列步驟將檔案上傳回共用資料夾。
執行 get 指令來下載檔案:
smbclient //smb-server-ip/share-name -A smb-cred.txt -c 'get remote local'
在本機修改檔案。
修改檔案後,執行 put 指令上傳檔案:
smbclient //smb-server-ip/share-name -A smb-cred.txt -c 'put local remote'
使用 smbclient 工具時,SMB 伺服器會使用您提供的憑證來識別您。這樣伺服器就能套用正確的檔案權限。如需完整的 smbclient 指令清單,請參閱 smbclient 官方說明文件。