本页面介绍了如何将 CIFS/SMB 网络文件系统与 Cloud Run 搭配使用。
如果您在 Cloud Run 上使用 NFS、Filestore 或 Cloud Storage FUSE,请参阅以下页面:
限制
使用
smbnetfs时,您必须指定第二代执行环境。由于 Cloud Run 执行环境存在限制,因此无法直接在 Cloud Run 中装载 CIFS/SMB。不过,您可以使用不依赖于内核装载的 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 配置。
创建装载点(例如
/mnt/smb),如以下命令所示:mkdir