Movatterモバイル変換


[0]ホーム

URL:


Hugging Face's logoHugging Face

Hub documentation

Docker Spaces

Hub

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

Collaborate on models, datasets and Spaces
Faster examples with accelerated inference
Switch between documentation themes

to get started

Docker Spaces

Spaces accommodate customDocker containers for apps outside the scope of Streamlit and Gradio. Docker Spaces allow users to go beyond the limits of what was previously possible with the standard SDKs. From FastAPI and Go endpoints to Phoenix apps and ML Ops tools, Docker Spaces can help in many different setups.

Setting up Docker Spaces

SelectingDocker as the SDK whencreating a new Space will initialize your Space by setting thesdk property todocker in yourREADME.md file’s YAML block. Alternatively, given an existing Space repository, setsdk: docker inside theYAML block at the top of your SpacesREADME.md file. You can also change the default exposed port7860 by settingapp_port: 7860. Afterwards, you can create a usualDockerfile.

---title:BasicDockerSDKSpaceemoji:🐳colorFrom:purplecolorTo:graysdk:dockerapp_port:7860---

Internally you could have as many open ports as you want. For instance, you can install Elasticsearch inside your Space and call it internally on its default port 9200.

If you want to expose apps served on multiple ports to the outside world, a workaround is to use a reverse proxy like Nginx to dispatch requests from the broader internet (on a single port) to different internal ports.

Secrets and Variables Management

You can manage a Space’s environment variables in the Space Settings. Read morehere.

Variables

Buildtime

Variables are passed asbuild-args when building your Docker Space. ReadDocker’s dedicated documentation for a complete guide on how to use this in the Dockerfile.

# Declare your environment variables with the ARG directiveARG MODEL_REPO_NAMEFROM python:latest# [...]# You can use them like environment variablesRUN predict.py$MODEL_REPO_NAME

Runtime

Variables are injected in the container’s environment at runtime.

Secrets

Buildtime

In Docker Spaces, the secrets management is different for security reasons. Once you create a secret in theSettings tab, you can expose the secret by adding the following line in your Dockerfile:

For example, ifSECRET_EXAMPLE is the name of the secret you created in the Settings tab, you can read it at build time by mounting it to a file, then reading it with$(cat /run/secrets/SECRET_EXAMPLE).

See an example below:

# Expose the secret SECRET_EXAMPLE at buildtime and use its value as git remote URLRUN --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 requestRUN --mount=type=secret,id=SECRET_EXAMPLE,mode=0444,required=true \curltest -H'Authorization: Bearer $(cat /run/secrets/SECRET_EXAMPLE)'

Runtime

Same as for public Variables, at runtime, you can access the secrets as environment variables. For example, in Python you would useos.environ.get("SECRET_EXAMPLE"). Check out thisexample of a Docker Space that uses secrets.

Permissions

The container runs with user ID 1000. To avoid permission issues you should create a user and set itsWORKDIR before anyCOPY or download.

# Set up a new user named "user" with user ID 1000RUN useradd -m -u 1000 user# Switch to the "user" userUSERuser# Set home to the user's home directoryENV HOME=/home/user \PATH=/home/user/.local/bin:$PATH# Set the working directory to the user's home directoryWORKDIR$HOME/app# Try and run pip command after setting the user with `USER user` to avoid permission issues with PythonRUN pip install --no-cache-dir --upgrade pip# Copy the current directory contents into the container at $HOME/app setting the owner to the userCOPY --chown=user .$HOME/app# Download a checkpointRUNmkdir contentADD --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.

If you still face permission issues, you might need to usechmod orchown in yourDockerfile to grant the right permissions. For example, if you want to use the directory/data, you can do:

RUNmkdir -p /dataRUNchmod 777 /data

You should always avoid superfluous chowns.

Updating metadata for a file creates a new copy stored in the new layer. Therefore, a recursive chown can result in a very large image due to the duplication of all affected files.

Rather than fixing permission by runningchown:

COPYcheckpoint .RUN chown -Rusercheckpoint

you should always do:

COPY --chown=usercheckpoint .

(same goes forADD command)

Data Persistence

The data written on disk is lost whenever your Docker Space restarts, unless you opt-in for apersistent storage upgrade.

If you opt-in for a persistent storage upgrade, you can use the/data directory to store data. This directory is mounted on a persistent volume, which means that the data written in this directory will be persisted across restarts.

At the moment,/data volume is only available at runtime, i.e. you cannot use/data during the build step of your Dockerfile.

You can also use our Datasets Hub for specific cases, where you can store state and data in a git LFS repository. You can find an example of persistencehere, which uses thehuggingface_hub library for programmatically uploading files to a dataset repository. This Space example along withthis guide will help you define which solution fits best your data type.

Finally, in some cases, you might want to use an external storage solution from your Space’s code like an external hosted DB, S3, etc.

Docker container with GPU

You can run Docker containers with GPU support by using one of our GPU-flavoredSpaces Hardware.

We recommend using thenvidia/cuda from Docker Hub as a base image, which comes with CUDA and cuDNN pre-installed.

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).

Read More

Update on GitHub


[8]ページ先頭

©2009-2026 Movatter.jp