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

新手上路,请多包涵

我有两个 docker 容器:Nginx 和 App。

应用程序容器扩展了 PHP-fpm 并且还有我的 Laravel 代码。

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

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

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

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

在我的应用程序 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

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

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

据我了解,存储文件夹需要由网络服务器写入。

我应该怎么做才能解决这个问题?

原文由 Anand Naik B 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

使您的 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

这使得目录 /var/www 归 --- 拥有,这是 --- php-fpm www-data 的默认用户。

由于它是使用用户 www-data 编译的。

参考-

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

原文由 vivekyad4v 发布,翻译遵循 CC BY-SA 3.0 许可协议

您需要创建一个与您机器上的用户(您创建项目文件时使用的用户)匹配的用户。因此,在应用程序 Dockerfile 中,像这样更改您的文件:

 FROM php:fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client

# Install PHP extensions
RUN docker-php-ext-install mcrypt pdo_mysql

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

# Set the user
USER $user

# Copy your files
COPY . . # You can use "." as a destination since you already changed the workdir

快速说明:如果您将 docker 纯粹用于本地开发,则无需复制任何内容,只需使用绑定挂载即可。

在您的 docker-compose 文件中,进行以下更改:

 app:
    build:
        args:
            user: essana3 # your username on your machine, you can get it by running echo $USER
            uid: 1000 # your uid which you can get by running id -u
        context: ./app
        dockerfile: Dockerfile # if your dockerfile is called Dockerfile, you don't need to add the dockerfile option
    volumes:
        - ./app:/var/www

奖励:您可以使用上面的 dockerfile 在 docker-compose 文件中创建实用程序容器来运行 artisan 和 composer 命令:

   composer:
    build:
      args:
        user: essana3 # your username (echo $USER)
        uid: 1000 # your uid (id -u)
      context: ./app
    working_dir: /var/www
    volumes:
      - ./app:/var/www
    entrypoint: ['composer']

  artisan:
    build:
      args:
        user: essana3 # your username (echo $USER)
        uid: 1000 # your uid (id -u)
      context: ./app
    working_dir: /var/www
    volumes:
      - ./app:/var/www
    entrypoint: ['php', 'artisan']

原文由 essana3 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题