首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PermissionError:[Errno 13]在装有WSL2和Docker的Windows上权限被拒绝

PermissionError:[Errno 13]在装有WSL2和Docker的Windows上权限被拒绝
EN

Stack Overflow用户
提问于 2021-10-14 17:43:31
回答 1查看 1.4K关注 0票数 0

我有一个Docker dev设置,可以在带有Hyper-V的Mac和Windows上运行良好,但不能在带有WSL2的Windows上运行。当Python试图执行makedirs命令时,我们得到一个PermissionError: [Errno 13] Permission denied错误。例如,当尝试制作/code/static时,我们会得到:

代码语言:javascript
运行
复制
PermissionError: [Errno 13] Permission denied: '/code/static'

类似于Django的makemigrations命令,它试图在/code/website/migrations上创建一个迁移目录,我们得到:

代码语言:javascript
运行
复制
PermissionError: [Errno 13] Permission denied: '/code/website/migrations'

但是我们的Dockerfile明确地给了apache用户访问/code/文件夹的权限。下面是完整的Dockerfile:

代码语言:javascript
运行
复制
# All Dockerfiles must start with a 'FROM' instruction, which specifies a base image
# See: https://docs.docker.com/engine/reference/builder/#format
# Note, some online sources say that you should put FROM django here (e.g., https://runnable.com/docker/python/dockerize-your-django-application)
# but, in fact, you should NOT do this according to the official docs (as this approach has been deprecated). 
# See: https://hub.docker.com/_/django/
FROM python:3.8

# Sometimes we get warnings about old pip, so take care of that here
RUN pip install --upgrade pip 

# See: https://www.quora.com/How-does-one-install-pip-in-a-Docker-container-using-a-Dockerfile
RUN apt-get update && apt-get --assume-yes install imagemagick ghostscript sqlite3

# The ENV instruction sets the environment variable <key> to the <value> in ENV <key> <value>. 
# See: https://docs.docker.com/engine/reference/builder/#environment-replacement
# In this case, we are setting the stdout/stderr streams in Python to be unbuffered
ENV PYTHONUNBUFFERED 1

#Create a system user which we'll use later.
#We're using the 'apache' user since that's what we're trying to map
#outside the container -- it could be called anything, but apache is convenient
RUN useradd -u 48 apache
RUN groupmod -g 48 apache

# The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. 
# The resulting committed image will be used for the next step in the Dockerfile.
# See: https://docs.docker.com/engine/reference/builder/#run
RUN mkdir /code

# The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions 
# that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used 
# in any subsequent Dockerfile instruction. 
# See: https://docs.docker.com/engine/reference/builder/#workdir
WORKDIR /code

#COPY the requirements.txt into the docker container

# As an fyi: Layering RUN instructions and generating commits conforms to the core concepts 
# of Docker where commits are cheap and containers can be created from any point in an image’s history, much like source control.
# See: https://docs.docker.com/engine/reference/builder/#run
COPY requirements.txt /code/
RUN pip3.8 install -r requirements.txt

## TEMP related to: https://github.com/jonfroehlich/makeabilitylabwebsite/issues/866
#RUN pip install django-ckeditor

# Our local user needs write access to a website and static files
RUN chown -R apache /code/

# Despite the above, still getting permission errors on WSL2
# -- PermissionError: [Errno 13] Permission denied: '/code/static'
# -- PermissionError: [Errno 13] Permission denied: '/code/website/migrations'
# RUN chown apache:apache -R /code/

COPY . /code/

#Run the process as our local user:
USER apache

COPY docker-entrypoint.sh docker-entrypoint.sh
CMD ["/code/docker-entrypoint.sh"] 

如果需要,您可以查看full repo here,包括Dockerfile、docker-compose.yml和docker-entrypoint.sh脚本。

更新

根据@LiquidDeath的建议,我在RUN mkdir /code语句后面的Dockerfile文件中添加了以下内容。

代码语言:javascript
运行
复制
RUN mkdir -p /code/static
RUN mkdir -p /code/website/migrations

RUN echo $(ls -al)

RUN echo $(ls -al /code)

RUN chmod -R 777 /code/static/
RUN chmod -R 777 /code/website/migrations/

我检查了这些目录确实是通过运行带有--progress=plain--no-cache标志的docker build命令创建的。

但是,一旦我实际运行docker-compose up,仍然会遇到相同的PermissionError: [Errno 13] Permission denied问题

代码语言:javascript
运行
复制
 | ****************** STEP 1: docker-entrypoint.sh ************************
 | 1. Collecting static files
 | ******************************************
 | Traceback (most recent call last):
 |   File "manage.py", line 10, in <module>
 |     execute_from_command_line(sys.argv)
 |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
 |     utility.execute()
 |   File "/usr/local/lib/python3.8/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.8/site-packages/django/core/management/base.py", line 316, in run_from_argv
 |     self.execute(*args, **cmd_options)
 |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 353, in execute
 |     output = self.handle(*args, **options)
 |   File "/usr/local/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 188, in handle
 |     collected = self.collect()
 |   File "/usr/local/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
 |     handler(path, prefixed_path, storage)
 |   File "/usr/local/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 353, in copy_file
 |     self.storage.save(prefixed_path, source_file)
 |   File "/usr/local/lib/python3.8/site-packages/django/core/files/storage.py", line 49, in save
 |     return self._save(name, content)
 |   File "/usr/local/lib/python3.8/site-packages/django/core/files/storage.py", line 236, in _save
 |     os.makedirs(directory)
 |   File "/usr/local/lib/python3.8/os.py", line 213, in makedirs
 |     makedirs(head, exist_ok=exist_ok)
 |   File "/usr/local/lib/python3.8/os.py", line 213, in makedirs
 |     makedirs(head, exist_ok=exist_ok)
 |   File "/usr/local/lib/python3.8/os.py", line 223, in makedirs
 |     mkdir(name, mode)
 | PermissionError: [Errno 13] Permission denied: '/code/static'
EN

Stack Overflow用户

回答已采纳

发布于 2021-10-15 16:25:51

这可能是因为在装有Hyper-V的Mac和Windows中,容器是以root权限运行的。但是WSL2不允许你以超级用户身份运行。作为一种解决方案,您可以对目录/文件授予必要的权限(写入/执行)。

在您的docker文件中添加以下行

代码语言:javascript
运行
复制
RUN chown -R apache /code/
COPY . /code/
RUN chmod -R 777 /code/static/
RUN chmod -R 777 /code/website/migrations/

777赋予所有者、用户和其他人完全权限,请根据您的要求更改此权限(请参阅linux中的权限)

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69575151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档