Hub documentation

Docker Spaces

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Docker Spaces

Spaces 支持自定义 Docker 容器,用于部署 Streamlit 和 Gradio 范围之外的应用。Docker Spaces 允许用户突破标准 SDK 此前所能实现的功能限制。无论是 FastAPI 和 Go 端点,还是 Phoenix 应用和 ML Ops 工具,Docker Spaces 都能适用于多种不同的配置场景。

设置 Docker Spaces

在创建新 Space 时选择 Docker 作为 SDK,会通过在 README.md 文件的 YAML 块中将 sdk 属性设置为 docker 来初始化您的 Space。或者,对于现有的 Space 仓库,请在 Spaces README.md 文件顶部的 YAML 块中设置 sdk: docker。您还可以通过设置 app_port: 7860 来更改默认暴露的端口 7860。之后,您可以创建一个常规的 Dockerfile。

---
title: Basic Docker SDK Space
emoji: 🐳
colorFrom: purple
colorTo: gray
sdk: docker
app_port: 7860
---

在内部,您可以开放任意数量的端口。例如,您可以在您的 Space 中安装 Elasticsearch,并在内部通过其默认端口 9200 调用它。

如果您希望将运行在多个端口上的应用暴露给外部网络,一种变通方法是使用 Nginx 等反向代理,将来自更广泛互联网(单一端口)的请求分发到不同的内部端口。

密钥和变量管理

您可以在 Space 设置中管理 Space 的环境变量。更多信息请参见此处。

变量

构建时

在构建您的 Docker Space 时,变量会作为 build-arg 传入。有关如何在 Dockerfile 中使用此功能的完整指南,请参阅 Docker 的专用文档。

	# Declare your environment variables with the ARG directive
	ARG MODEL_REPO_NAME

	FROM python:latest
	# [...]
	# You can use them like environment variables
	RUN predict.py $MODEL_REPO_NAME

运行时

变量会在运行时注入到容器的环境中。

Secrets

构建时

在 Docker Spaces 中,出于安全考虑,密钥管理方式有所不同。在 设置选项卡 中创建密钥后,您可以在 Dockerfile 中添加以下行来暴露该密钥:

例如,如果 SECRET_EXAMPLE 是您在 Settings 选项卡中创建的密钥的名称,您可以在构建时通过将其挂载到文件,然后使用 $(cat /run/secrets/SECRET_EXAMPLE) 来读取它。

以下是一个示例:

# Expose the secret SECRET_EXAMPLE at buildtime and use its value as git remote URL
RUN --mount=type=secret,id=SECRET_EXAMPLE,mode=0444,required=true \
 git init && \
 git remote add origin $(cat /run/secrets/SECRET_EXAMPLE)
# Expose the secret SECRET_EXAMPLE at buildtime and use its value as a Bearer token for a curl request
RUN --mount=type=secret,id=SECRET_EXAMPLE,mode=0444,required=true \
	curl test -H 'Authorization: Bearer $(cat /run/secrets/SECRET_EXAMPLE)'

运行时

与公开变量类似,在运行时,您可以将密钥作为环境变量访问。例如,在 Python 中,您可以使用 os.environ.get("SECRET_EXAMPLE")。请查看这个使用密钥的 Docker Space 示例。

权限

容器以用户 ID 1000 运行。为避免权限问题,您应在执行任何 COPY 或下载操作之前创建用户并设置其 WORKDIR。

# Set up a new user named "user" with user ID 1000
RUN useradd -m -u 1000 user

# Switch to the "user" user
USER user

# Set home to the user's home directory
ENV HOME=/home/user \
	PATH=/home/user/.local/bin:$PATH

# Set the working directory to the user's home directory
WORKDIR $HOME/app

# Try and run pip command after setting the user with `USER user` to avoid permission issues with Python
RUN pip install --no-cache-dir --upgrade pip

# Copy the current directory contents into the container at $HOME/app setting the owner to the user
COPY --chown=user . $HOME/app

# Download a checkpoint
RUN mkdir content
ADD --chown=user https://<SOME_ASSET_URL> content/<SOME_ASSET_NAME>
Always specify the `--chown=user` with `ADD` and `COPY` to ensure the new files are owned by your user.

如果您仍然遇到权限问题,可能需要在 Dockerfile 中使用 chmod 或 chown 来授予正确的权限。例如,如果您想使用目录 /data,可以执行以下操作:

RUN mkdir -p /data
RUN chmod 777 /data

您应始终避免不必要的 chown 操作。

更新文件的元数据会在新的层中创建一个新副本。因此,递归执行 chown 操作可能会因复制所有受影响的文件而导致镜像体积非常大。

与其通过运行 chown 来修复权限:

COPY checkpoint .
RUN chown -R user checkpoint

您应该始终执行以下操作:

COPY --chown=user checkpoint .

(ADD 命令也是如此)

数据持久性

每次 Docker Space 重启时,写入磁盘的数据都会丢失。若要在重启后保留数据,您可以将 Storage Bucket 附加到您的 Space。

目前,/data 卷仅在运行时可用,即您无法在 Dockerfile 的构建步骤中使用 /data。

您也可以使用我们的 Datasets Hub 来处理特定场景,在其中可以将状态和数据存储在 git LFS 仓库中。您可以在此处找到一个持久化的示例,该示例使用 huggingface_hub 库 以编程方式将文件上传到数据集仓库。此 Space 示例以及本指南将帮助您确定哪种方案最适合您的数据类型。

最后,在某些情况下,您可能希望从您的 Space 代码中使用外部存储解决方案,例如外部托管的数据库、S3 等。

带有 GPU 的 Docker 容器

您可以使用我们的 GPU 版 Spaces Hardware 之一来运行支持 GPU 的 Docker 容器。

我们建议将 Docker Hub 上的 nvidia/cuda 作为基础镜像,该镜像已预装 CUDA 和 cuDNN。

During Docker buildtime, you don't have access to a GPU hardware. Therefore, you should not try to run any GPU-related command during the build step of your Dockerfile. For example, you can't run `nvidia-smi` or `torch.cuda.is_available()` building an image. Read more [here](https://github.com/NVIDIA/nvidia-docker/wiki/nvidia-docker#description).

阅读更多

Update on GitHub