SaiMupparaju commited on
Commit
c6e1c92
·
1 Parent(s): a1b8a33

Fix permission issues by using a non-root user and dedicated cache directories

Browse files
Files changed (2) hide show
  1. Dockerfile +14 -4
  2. app.py +4 -10
Dockerfile CHANGED
@@ -2,12 +2,16 @@ FROM python:3.9
2
 
3
  WORKDIR /code
4
 
5
- # Create cache directories with appropriate permissions
6
- RUN mkdir -p /code/model_cache /code/hf_home
 
 
 
 
7
 
8
  # Set environment variables for the cache
9
- ENV TRANSFORMERS_CACHE=/code/model_cache
10
- ENV HF_HOME=/code/hf_home
11
 
12
  COPY ./requirements.txt /code/requirements.txt
13
 
@@ -15,6 +19,12 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
15
 
16
  COPY . /code
17
 
 
 
 
18
  EXPOSE 7860
19
 
 
 
 
20
  CMD ["python", "app.py"]
 
2
 
3
  WORKDIR /code
4
 
5
+ # Create a non-root user to run the app
6
+ RUN useradd -m appuser
7
+
8
+ # Create cache directories and set permissions
9
+ RUN mkdir -p /home/appuser/cache /home/appuser/hf_home && \
10
+ chown -R appuser:appuser /home/appuser/cache /home/appuser/hf_home
11
 
12
  # Set environment variables for the cache
13
+ ENV TRANSFORMERS_CACHE=/home/appuser/cache
14
+ ENV HF_HOME=/home/appuser/hf_home
15
 
16
  COPY ./requirements.txt /code/requirements.txt
17
 
 
19
 
20
  COPY . /code
21
 
22
+ # Give ownership of the code to appuser
23
+ RUN chown -R appuser:appuser /code
24
+
25
  EXPOSE 7860
26
 
27
+ # Switch to appuser for running the application
28
+ USER appuser
29
+
30
  CMD ["python", "app.py"]
app.py CHANGED
@@ -8,19 +8,13 @@ import json
8
 
9
  app = Flask(__name__)
10
 
11
- # Set cache directory to a location where we have write permissions
12
- cache_dir = os.path.join(os.getcwd(), 'model_cache')
13
- hf_home = os.path.join(os.getcwd(), 'hf_home')
14
  os.environ['TRANSFORMERS_CACHE'] = cache_dir
15
  os.environ['HF_HOME'] = hf_home
16
 
17
- # Make sure the directories exist with proper permissions
18
- try:
19
- os.makedirs(cache_dir, exist_ok=True)
20
- os.makedirs(hf_home, exist_ok=True)
21
- print(f"Created cache directories: {cache_dir} and {hf_home}", file=sys.stderr)
22
- except Exception as e:
23
- print(f"Error setting up cache directories: {e}", file=sys.stderr)
24
 
25
  # Load GPT-2 small model
26
  try:
 
8
 
9
  app = Flask(__name__)
10
 
11
+ # Use environment variables for cache locations
12
+ cache_dir = os.environ.get('TRANSFORMERS_CACHE', '/home/appuser/cache')
13
+ hf_home = os.environ.get('HF_HOME', '/home/appuser/hf_home')
14
  os.environ['TRANSFORMERS_CACHE'] = cache_dir
15
  os.environ['HF_HOME'] = hf_home
16
 
17
+ print(f"Using cache directories: {cache_dir} and {hf_home}", file=sys.stderr)
 
 
 
 
 
 
18
 
19
  # Load GPT-2 small model
20
  try: