PermissionError: [Errno 13] Permission denied: '/manage.py'

58,934

Solution 1

In your dockerfile, you are pointing to a new user dockuser.

RUN adduser -D dockuser
USER dockuser

Hence your container will start with user dockuser which don't seems to have proper permissions to run /manage.py.

You can either

  • remove the above mentioned lines where you creates and point to dockuser.

OR

  • provide appropriate permission to user dockuser using chown and chmod commands in your dockerfile for /manage.py file.

I have answered such similar question here.

Solution 2

ubuntu 21.04

I got here searching for PermissionError: [Errno 13] Permission denied: so i'll just leave this here.

note: the below answer doesn't work for multi user systems ... see this answer instead for another possible solution


If you want to set it and forget it for 1 user, your own user ... here's what I have on my dev machine.

I didn't own the unix socket, so I chowned it. ( this got it working straight away )

sudo chown $(whoami):$(whoami) /var/run/docker.sock

Another, more permanent solution for your dev environment, is to modify the user ownership of the unix socket creation. This will give your user the ownership, so it'll stick between restarts:

sudo nano /etc/systemd/system/sockets.target.wants/docker.socket

docker.socket:

[Unit]
Description=Docker Socket for the API

[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=YOUR_USERNAME_HERE
SocketGroup=docker

[Install]
WantedBy=sockets.target

Again another less hacky solution: https://stackoverflow.com/a/70124863/2026508

Solution 3

I had the same issue and solved it by adding my user to the docker group:

$ sudo usermod -aG docker $(whoami)

Solution 4

add this to your Dockerfile after RUN adduser -D dockuser:

RUN chown dockuser:dockuser -R /app/

and why you COPYthe files if you already mount them ?

if you want to keep the mount , you need to add rw persmission on the folder on the HOST system not on the Container

Solution 5

If you're on mac this might work for you.

After 4 days of troubleshooting this error (and other strange errors) I found out that I needed to fix dockers permissions in my file system. To do this go to:

System Preferences -> Security & Privacy -> Privacy tab -> Full Disk Access (on the left, somewhere in the list) -> Click on the + -> Docker application

Terribly frustrating problem to debug, hope it helps.

Share:
58,934

Related videos on Youtube

Anubrij Chandra
Author by

Anubrij Chandra

Updated on July 09, 2022

Comments

  • Anubrij Chandra
    Anubrij Chandra almost 2 years

    I am trying to run the following command in docker-composer, to start project with django-admin:

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

    This produces the error:

        Traceback (most recent call last):
      File "/usr/local/bin/django-admin", line 10, in <module>
        sys.exit(execute_from_command_line())
      File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
        utility.execute()
      File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
        output = self.handle(*args, **options)
      File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/startproject.py", line 20, in handle
        super().handle('project', project_name, target, **options)
      File "/usr/local/lib/python3.7/site-packages/django/core/management/templates.py", line 155, in handle
        with open(new_path, 'w', encoding='utf-8') as new_file:
    PermissionError: [Errno 13] Permission denied: '/manage.py'
    

    The Dockerfile is as follows:

    FROM python:3.7-alpine
    MAINTAINER anubrij chandra
    
    ENV PYTHONUNBUFFERED 1
    
    COPY ./requirements.txt /requirements.txt
    
    RUN pip install -r /requirements.txt
    
    RUN mkdir /app
    COPY ./app /app
    
    
    
    RUN adduser -D dockuser
    USER dockuser
    

    My docker-compose.yml:

    version: "3"
    
    services:
      app:
        build:
          context: .
        ports:
          - "8000:8000"
        volumes:
          - ./app:/app
        command: >
          sh -c "python manage.py runserver 0.0.0.0:8000"
    

    I applied the solution suggested in this Stack Overflow thread, but it didn't work.

    I'm using Ubuntu 18.04.

  • Pran Kumar Sarkar
    Pran Kumar Sarkar almost 5 years
    Step 11/11 : RUN chown user:user -R /app/ ---> Running in 4692ee51488d chown: /app/: Operation not permitted chown: /app/: Operation not permitted The command '/bin/sh -c chown user:user -R /app/' returned a non-zero code: 1
  • Ebram Shehata
    Ebram Shehata over 4 years
    Yeah, CHOWNing the folder did the thing.
  • Muhammad Usman
    Muhammad Usman about 4 years
    @PranKumarSarkar you've to do it before USER user line.
  • Scott Stoltzman
    Scott Stoltzman almost 4 years
    This is absolutely important -- will likely be a huge problem for a lot of people.
  • Development FlazHost
    Development FlazHost about 3 years
    How to do this on Ubuntu or Windows? I got the same error.
  • ViaTech
    ViaTech almost 3 years
    Nothing else worked for me, thank you for listing this out!
  • waqasgard
    waqasgard over 2 years
    This deserves more upvotes. Really really saved my day. Thanks man!!!
  • ervinbosenbacher
    ervinbosenbacher over 2 years
    10 stars, solved my problem
  • Shōgun8
    Shōgun8 over 2 years
    Can't do this on a multi-user system. It's better just to add yourself to the docker user group
  • jmunsch
    jmunsch over 2 years
    @Shōgun8 could potentially add multiple users to the docker group see : stackoverflow.com/a/70124863/2026508 in lieu of making changes to the systemd sockets.target
  • Shōgun8
    Shōgun8 over 2 years
    @jmunsch - I'm not sure if you understood my comment. I suggested adding self to docker group because *you can't have multiple users chowning a file, as in chown $(whoami) *
  • jmunsch
    jmunsch over 2 years
    @Shōgun8 i'll add a note
  • panepeter
    panepeter over 2 years
    Note: this will not take effect within your running session – you'll need to login afresh.