php 使用 Laravel 和 Docker 的权限被拒绝错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48619445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:42:44  来源:igfitidea点击:

Permission Denied Error using Laravel & Docker

phplaraveldockernginxdocker-compose

提问by Anand Naik B

I have two docker containers: Nginx and App.
The app container extends PHP-fpm and also has my Laravel Code.

我有两个 docker 容器:Nginx 和 App。
应用程序容器扩展了 PHP-fpm 并且还有我的 Laravel 代码。

In my docker-compose.ymlI'm doing:

在我的docker-compose.yml我正在做:

version: '2'
services:
    nginx:
        build:
            context: ./nginx
            dockerfile: ./Dockerfile
        ports:
            - "80:80"
        links:
            - app
    app:
        build:
            context: ./app
            dockerfile: ./Dockerfile

In my Nginx Dockerfile i'm doing:

在我的 Nginx Dockerfile 中,我正在做:

FROM nginx:latest
WORKDIR /var/www
ADD ./nginx.conf /etc/nginx/conf.d/default.conf
ADD . /var/www
EXPOSE 80

In my App Dockerfile I'm doing:

在我的应用程序 Dockerfile 中,我正在做:

FROM php:7-fpm
WORKDIR /var/www
RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client && docker-php-ext-install mcrypt pdo_mysql
ADD . /var/www

After successfully running docker-compose up, I have the following error when I try localhost

成功运行docker-compose up后,尝试localhost时出现以下错误

The stream or file "/var/www/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied

流或文件“/var/www/storage/logs/laravel.log”无法打开:无法打开流:权限被拒绝

From my understanding, the storage folder needs to writable by the webserver.
What should I be doing to resolve this?

根据我的理解,存储文件夹需要由网络服务器写入。
我应该怎么做才能解决这个问题?

回答by vivekyad4v

Make your Dockerfile something as below -

使您的 Dockerfile 如下所示 -

FROM php:7-fpm
WORKDIR /var/www
RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client && docker-php-ext-install mcrypt pdo_mysql
ADD . /var/www
RUN chown -R www-data:www-data /var/www

This makes directory /var/wwwowned by www-datawhich is the default user for php-fpm.

这使得目录/var/www所属的目录www-data成为php-fpm.

Since it is compiled with user www-data.

因为它是用 user 编译的www-data

Ref-

参考-

https://github.com/docker-library/php/blob/57b41cfc2d1e07acab2e60d59a0cb19d83056fc1/7.0/jessie/fpm/Dockerfile

https://github.com/docker-library/php/blob/57b41cfc2d1e07acab2e60d59a0cb19d83056fc1/7.0/jessie/fpm/Dockerfile

回答by Khachornchit Songsaen

I found similar problem and I fixed it by

我发现了类似的问题,并通过以下方式修复了它

chown -R www-data:www-data /var/www

chmod -R 755 /var/www/storage

回答by Hamid Mohayeji

When using bind mounts in Docker, the original permissions in the Docker host are preserved in the container. This enables us to set appropriate permissions on the Docker host, to be used inside the container.

在 Docker 中使用绑定挂载时,Docker 主机中的原始权限保留在容器中。这使我们能够在 Docker 主机上设置适当的权限,以在容器内使用。

First, you should find the uidand gidof the nginx, for example:

首先,您应该找到nginx的uidgid,例如:

docker-compose exec nginx id www-data

docker-compose exec nginx id www-data

The output of this command would be something like this:

此命令的输出将是这样的:

uid=33(www-data) gid=33(www-data) groups=33(www-data)

uid=33(www-data) gid=33(www-data) groups=33(www-data)

Then, you should use these uidand gidto set permissions on Docker host, which will be used by the container too. So, run the following command on the Docker host:

然后,您应该使用这些uidgid在 Docker 主机上设置权限,容器也将使用这些权限。因此,在 Docker 主机上运行以下命令:

sudo chown -R 33:33 site

sudo chown -R 33:33 site

Now everything must be working.

现在一切正常。

回答by Abdulla Nilam

You can do 3 things as I used/using

你可以做 3 件事,因为我使用/使用

  1. Grant 777 permission to storagefolder (sudo chmod 777 storage/ storage/*)
  2. RUN chown -R www-data:www-data /var/www RUN chmod 755 /var/www

  3. Move project folder to /homewhich is no need of special permission to write on that directory(Personally Recommend this) (In mine I have used /home/projects/and all of them placed there).

  1. storage文件夹 ( sudo chmod 777 storage/ storage/*)授予 777 权限
  2. RUN chown -R www-data:www-data /var/www RUN chmod 755 /var/www

  3. /home不需要特殊权限的项目文件夹移动到该目录上(个人推荐这个)(在我的项目中我已经使用过/home/projects/并且所有这些都放在那里)。



Note:If the project is in /var/www/then if you wrote file upload also you need permission to that folders too. this will avoid such error when you moved that to /home

注意:如果项目在,/var/www/那么如果你写了文件上传,你也需要对该文件夹的权限。当您将其移动到时,这将避免此类错误/home

回答by rogal127

u can just right click on your laravel folder, click on properties,then permissions, and from 'group' field, select the 'docker' and give necessary permissions to 'docker' group :) thats work for me

您可以右键单击您的 Laravel 文件夹,单击属性,然后单击权限,然后从“组”字段中选择“泊坞窗”并为“泊坞窗”组授予必要的权限:) 这对我有用