skip to Main Content

I am encountering a PermissionError when trying to run the docker compose run command for creating a Django project. The error message is as follows :-

docker compose run --rm app sh -c "django-admin startproject myproject ."
[+] Building 0.0s (0/0)                                                                                                                   docker:desktop-linux 
[+] Building 0.0s (0/0)                                                                                                                   docker:desktop-linux
Traceback (most recent call last):
  File "/py/bin/django-admin", line 8, in <module>
    sys.exit(execute_from_command_line())
  File "/py/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/py/lib/python3.9/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/py/lib/python3.9/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/py/lib/python3.9/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/py/lib/python3.9/site-packages/django/core/management/commands/startproject.py", line 21, in handle
    super().handle("project", project_name, target, **options)
  File "/py/lib/python3.9/site-packages/django/core/management/templates.py", line 205, in handle
    with open(new_path, "w", encoding="utf-8") as new_file:
PermissionError: [Errno 13] Permission denied: '/app/manage.py'
.
.
.

And this is my Dockerfile :-

FROM python:3.9-slim

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt

COPY ./app /app

WORKDIR /app

EXPOSE 8000

RUN python -m venv /py && 
    /py/bin/pip install --upgrade pip && 
    /py/bin/pip install -r /requirements.txt && 
    adduser --disabled-password --no-create-home app



ENV PATH="/py/bin:$PATH"

USER app



.
.

and this is my docker-compose.yml file :-

services:
  app:
    build:
      context: .
    image: my-django-image
    ports:
      - 8000:8000
    volumes:
      - ./app:/app
    command: >


i tried few of the solutions but nothing worked..
I’m using Zorin OS

2

Answers


  1. This creates the /app folder inside the container, and it is owned by root:

    COPY ./app /app
    

    Then you change to another user:

    USER app
    

    … which tries to execute the command django-admin startproject myproject . but it does not have permission to write to the /app folder, and so you get the error.

    Why are you trying to run that command inside the container anyway? The usual practice is to create the files locally and then copy them into the container, rather than creating them dynamically inside the container.

    Login or Signup to reply.
  2. The Error is likely related to file permissions within the Docker container. It indicates a PermissionError when attempting to create a file /app/manage.py inside that container. Now to solve this Please Update your Dockerfile to set proper permissions for the /app directory and its contents. You can do this by adding the following lines to the Dockerfile:

    FROM python:3.9-slim
    
    ENV PYTHONUNBUFFERED 1
    
    COPY ./requirements.txt /requirements.txt
    
    COPY ./app /app
    
    WORKDIR /app
    
    EXPOSE 8000
    
    RUN python -m venv /py && 
        /py/bin/pip install --upgrade pip && 
        /py/bin/pip install -r /requirements.txt && 
        adduser --disabled-password --no-create-home app
    
    # Set proper permissions
    RUN chown -R app /app
    
    ENV PATH="/py/bin:$PATH"
    
    USER app
    

    This line RUN chown -R app /app ensures that the app user has ownership of the /app directory and its contents.

    After applying above changes to your Dockerfile, rebuild the Docker image via

    docker-compose build
    

    Then Run Docker Compose Again

    docker-compose run --rm app sh -c "django-admin startproject myproject ."

    After that Django project should be created inside the container without encountering permission errors.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search