Loading the pre-trained model of torch and sentence_transformers when running in a docker container failing
I am getting below error while loading the pre-trained model of torch and sentence_transformers(&quot;distilbert-base-nli-stsb-mean-tokens&quot;) when trying to run in a docker container.
Error: Invalid value for '-A' / '--app': 
 Unable to load celery application.
 While trying to load the module app.celery the following error occurred:
 Traceback (most recent call last):
   File &quot;/usr/local/lib/python3.8/site-packages/celery/bin/celery.py&quot;, line 53, in convert
     return find_app(value)
   File &quot;/usr/local/lib/python3.8/site-packages/celery/app/utils.py&quot;, line 384, in find_app
     sym = symbol_by_name(app, imp=imp)
   File &quot;/usr/local/lib/python3.8/site-packages/kombu/utils/imports.py&quot;, line 56, in symbol_by_name
     module = imp(module_name, package=package, **kwargs)
   File &quot;/usr/local/lib/python3.8/site-packages/celery/utils/imports.py&quot;, line 100, in import_from_cwd
     return imp(module, package=package)
   File &quot;/usr/local/lib/python3.8/importlib/__init__.py&quot;, line 127, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1014, in _gcd_import
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 991, in _find_and_load
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 975, in _find_and_load_unlocked
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 671, in _load_unlocked
   File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 783, in exec_module
   File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 219, in _call_with_frames_removed
   File &quot;/code/app.py&quot;, line 997, in &lt;module&gt;
     load_model()
   File &quot;/code/app.py&quot;, line 255, in load_model
     embedder = SentenceTransformer('distilbert-base-nli-stsb-mean-tokens')
   File &quot;/usr/local/lib/python3.8/site-packages/sentence_transformers/SentenceTransformer.py&quot;, line 48, in __init__
     os.makedirs(model_path, exist_ok=True)
   File &quot;/usr/local/lib/python3.8/os.py&quot;, line 213, in makedirs
     makedirs(head, exist_ok=exist_ok)
   File &quot;/usr/local/lib/python3.8/os.py&quot;, line 213, in makedirs
     makedirs(head, exist_ok=exist_ok)
   File &quot;/usr/local/lib/python3.8/os.py&quot;, line 213, in makedirs
     makedirs(head, exist_ok=exist_ok)
   [Previous line repeated 1 more time]
   File &quot;/usr/local/lib/python3.8/os.py&quot;, line 223, in makedirs
     mkdir(name, mode)
 PermissionError: [Errno 13] Permission denied: '/nonexistent'

Here it is saying permission denied error while creating the folder. But I have tried providing USER root in the Dockerfile. Stuck with this issue for long time. Please anyone help me here.
Updated:
My Dockerfile:
FROM python:3.8.5-slim

WORKDIR /code

ENV ENVIRONMENT='LOCAL'
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update &amp;&amp; apt-get install -y sudo netcat apt-utils
RUN apt-get install -y python3-dev  build-essential python3-pip

COPY ./requirements_local.txt /code/requirements_local.txt
RUN pip install -r /code/requirements_local.txt

EXPOSE 8000
COPY . /code/

CMD [ &quot;gunicorn&quot;, &quot;app:app&quot;, &quot;-b&quot;, &quot;0.0.0.0:8000&quot;,&quot;--timeout&quot;,&quot;7200&quot;]

Docker-compose:
services:
  web:
    build: 
      context: .
      dockerfile: ./Dockerfile.prod
    hostname: flaskapp
    env_file:
      - ./.env.prod
    links:
      - redis
      - celery
    depends_on:
      - redis
    volumes:
      - data:/code
      - type: bind
        source: /home/ubuntu/models
        target: /mnt/models


sentence-transformers downloads and stores model in ~/.cache directory (or whatever the cache_folder evaluates to be in - https://github.com/UKPLab/sentence-transformers/blob/a13a4ec98b8fdda83855aca7992ea793444a207f/sentence_transformers/SentenceTransformer.py#L63). For you this looks like the /nonexistant directory. The permission denied error suggests you do not have permission to access that directory (to create cache folder).
You can modify the Dockerfile to create this directory and make it accessible to any user that needs to access this:-
RUN mkdir ~/.cache
RUN chmod -R 777 ~/.cache # don't do this in production - modify command to give permission to users who require it.

Or you could try downloading the model in the Dockerfile itself -
RUN python -c 'from sentence-transformers import SentenceTransformer; embedder = SentenceTransformer(&quot;distilbert-base-nli-stsb-mean-tokens&quot;)'

