PermissionError when writing to HF_HOME directory on startup

When starting my Hugging Face Space with Docker (during setup will do chmod -R 777 /app/hf_home, then export HF_HOME=/app/hf_home on launch) I get this error:

The cache for model files in Transformers v4.22.0 has been updated. Migrating your old cache. This is a one-time only operation. You can interrupt this and resume the migration later on by calling `transformers.utils.move_cache()`.
0it [00:00, ?it/s]
0it [00:00, ?it/s]
There was a problem when trying to write in your cache folder (/app/hf_home/hub). You should set the environment variable TRANSFORMERS_CACHE to a writable directory.
Traceback (most recent call last):
  File "/app/", line 22, in <module>
    snapshot_download(repo_id=model, revision="main", token=HF_AUTH_TOKEN)
Downloading AI model (mistralai/Mistral-7B-Instruct-v0.2)...
  File "/usr/local/lib/python3.11/site-packages/huggingface_hub/utils/", line 114, in _inner_fn
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.11/site-packages/huggingface_hub/", line 261, in snapshot_download
    with open(ref_path, "w") as f:
PermissionError: [Errno 13] Permission denied: '/app/hf_home/hub/models--mistralai--Mistral-7B-Instruct-v0.2/refs/main'

hi @LunarN0v4 , could you please share your Dockerfile? you could use this example

and add an user and give the folder permission to write.

FROM python:3.9


COPY ./requirements.txt /code/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

RUN useradd -m -u 1000 user

USER user

ENV HOME=/home/user \


COPY --chown=user . $HOME/app

CMD ["uvicorn", "main:app", "--host", "", "--port", "7860"]

Hi radames, here is my Dockerfile:

FROM python:3.11.9-bookworm
COPY ./* /app/
RUN python -m pip install --upgrade pip
RUN pip install requests huggingface_hub fastapi transformers[torch] uvicorn
RUN mkdir -p ./hf_home/
RUN chmod -R 777 ./hf_home/
RUN sh -c "export HF_HOME=/app/hf_home/ && python"
CMD ["sh", "-c", "export HF_HOME=/app/hf_home/ && python"]

I am pretty solid on trying to get it to run as root, since that works very well on my main system, however, I can certainly try it with an individual user instead.