简体   繁体   中英

PermissionError: [Errno 13] Permission denied: '/app/manage.py' when trying to create project with docker-compose

I was following a tutorial on how to create Django REST Framework API with Docker and succeeded to run the project on the first attempt, but then it's not possible to recreate it due to PermissionError .

The directory structure looks in the following way:

project_directory
  - Dockerfile
  - docker-compose.yml
  - requirements.txt
  - app/ # this directory was created manually

Successful configuration looks this way:

Dockerfile:

FROM python:3.7-alpine
LABEL author="aqv"

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client
RUN apk add --update --no-cache --virtual .tmp-build-deps \
    gcc libc-dev linux-headers postgresql-dev

RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN adduser -D user
USER user

requirements.txt:

Django>=2.1.3,<2.2.0
djangorestframework>=3.9.0,<3.10.0
psycopg2>=2.7.5,<2.8.0

docker-compose.yml:

version: "3"

services:
  app:
    build:
      context: .
    ports:
      - "3005:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py wait_for_db &&
             python manage.py migrate &&
             python manage.py runserver 0.0.0.0:8000"
    environment:
      - DB_HOST=db
      - DB_NAME=app
      - DB_USER=postgresuser
      - DB_PASS=<pass>
    depends_on:
      - db

  db:
    image: postgres:10-alpine
    environment:
      - POSTGRES_DB=app
      - POSTGRES_USER=postgresuser
      - POSTGRES_PASSWORD=<pass>

First step was running (1) docker build. in the project directory, then came (2) docker-compose build (which made the 1st command redundant, but didn't break anything) and finally (3) docker-compose run app sh -c "django-admin.py startproject app." .

The last command now ends up with:

Starting project_t_db_1 ... done
Traceback (most recent call last):
  File "/usr/local/bin/django-admin.py", line 5, in <module>
    management.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 3.7, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 353, 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: '/app/manage.py'

The /app directory is empty, there are only the files listed in the project_directory above, so /app/manage.py doesn't exist.

The attempts to re-run creation of the project were taken on Windows 10 machine (both CMD and PowerShell, including ran as admin), Ubuntu on Windows and a remote Ubuntu server. Ownership of files and directories was checked with both root and regular user.

All Docker containers were killed ( docker kill $(docker ps -q) ), images were removed ( docker rm $(docker ps -a -q) and docker rmi $(docker images -q) ) and creation of the environment was run from scratch.

I observed, that I can successfully create the project on newly set up server. But when trying to create another one or replace the existing one with another, the issue comes up again.

What would you suggest to check?

I'm creating the app directory as users home directory - this way the user has the correct permissions for it

RUN adduser \
    --disabled-password \
    --gecos "" \
    --home /app \
    app
USER app

WORKDIR /app
COPY ./app/ /app/

I have faced the similar issue on Mac. The only changes I did was changed the volumes setup in docker-compose.yml

volumes: -./app /app to below

volumes: -./app:/app

It worked for me. The simple docker-compose.yml file looks like below

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 faced the same issue. I found out that this was due to the windows (or third party's) firewall blocking file sharing between Windows and Docker. When I tried to share 'C' and 'D' drive of the Windows in Docker's setting, the following message appeared, (error message from Docker) and I could not share drives.

In Docker's documentation , they suggested to open TCP port 445 for dockers, for allowing file sharing. I used Kaspersky for security, so I looked for ways to open TCP port in Kaspersky's firewall. I found my solution in this link . You can also find other solutions for this problem in this stackoverflow page as well.

After I successfully shared drives between Windows and Docker, problem solved.

This was bugging me for 2 days, until I decided to reinstall docker. Which solved the problem for me.

Before:

Docker --version: Docker version 20.10.20, build 9fdeb9c

After:

Docker --version: Docker version 20.10.21, build baeda1f

uname -a

Linux fedora 6.0.9-602.inttf.fc37.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Nov 18 16:20:56 EET 2022 x86_64 x86_64 x86_64 GNU/Linux

I used an old project which I have in production right now so I knew it should work, but I got the same permission denied on my brand new Fedora 37 development machine.

Steps to solve the issue (install docker from repository as documented here https://docs.docker.com/engine/install/fedora/#install-using-the-repository ):

sudo dnf remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

sudo dnf -y install dnf-plugins-core

download https://download.docker.com/linux/fedora/docker-ce.repo

sudo dnf config-manager --add-repo path/to/docker-ce.repo

sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin

sudo systemctl start docker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM