Os.mkdir is not able to create new folder in Space

I have an API that is working smoothly on my local. Then, I tried to deploy my API with Space but I am always having a permission issue as below

File "/api/sabiduria_tool_api/services/service_sabiduria_tool.py", line 35, in save_files
    mkdir(self.data_store_path)
PermissionError: [Errno 13] Permission denied: '/api/sabiduria_tool_api/data/'

Here is the method that is failing

async def save_files(self) -> None:
    if isdir(self.data_store_path):
        old_files = self.get_files()
        for o_file in old_files:
            remove(f"{self.data_store_path}{o_file}")
    else:
        mkdir(self.data_store_path)
    for file in self.files:
        with open(f"{self.data_store_path}{file.filename}", "wb") as destination:
            copyfileobj(file.file, destination)

Here is my docker file

FROM python:3.9.7

COPY ./requirements.txt /api/requirements.txt
COPY ./sabiduria_tool_api /api/sabiduria_tool_api

WORKDIR /api

RUN pip install --upgrade pip
RUN pip install -r requirements.txt

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

Is there a way that I can create a folder?

Hello @serdarakyol,

Issue is your space is running with an unprivileged user while by default during a docker build commands are run as root.

To prevent this issue you need to copy your resources specifying the owner.

FROM python:3.9.7

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

# Switch to the "user" user
USER user

COPY --chown=user ./requirements.txt /api/requirements.txt
COPY --chown=user ./sabiduria_tool_api /api/sabiduria_tool_api

WORKDIR /api

RUN pip install --upgrade pip
RUN pip install -r requirements.txt

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

You’ll find more info regarding permissions issues in the documentation

2 Likes

let us know if this solves your issue @serdarakyol !

I have this in Dockerfile:

FROM python:3.9.12

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY . /app

RUN apt-get update && apt-get install -y \

libgl1-mesa-glx \

&& rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir flask tensorflow jsonify
RUN pip install --no-cache-dir gunicorn

EXPOSE 5000

CMD [“gunicorn”, “–workers”, “2”, “–timeout”, “180”, “–bind”, “0.0.0.0:5000”, “app:app”]

I am getting permission error when my app.py runs, telling:


os.makedirs(temp_folder, exist_ok=True)
File “/usr/local/lib/python3.9/os.py”, line 225, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: ‘temporary’
[2024-04-19 06:57:15 +0000] [8] [INFO] Worker exiting (pid: 8)
[2024-04-19 06:57:15 +0000] [1] [ERROR] Worker (pid:7) exited with code 3
[2024-04-19 06:57:15 +0000] [1] [ERROR] Worker (pid:8) was sent SIGTERM!
[2024-04-19 06:57:15 +0000] [1] [ERROR] Shutting down: Master
[2024-04-19 06:57:15 +0000] [1] [ERROR] Reason: Worker failed to boot.

hi @i-darrshan please make sure you the running user has permission, follow our guide here