Laravel docker image unable to change permission to /var/www/storage/logs/ always is root owner

huangapple go评论81阅读模式
英文:

Laravel docker image unable to change permission to /var/www/storage/logs/ always is root owner

问题

I have the next Dockerfile:

FROM php:8.1.0-fpm

# 设置工作目录
WORKDIR /var/www

# 添加 Docker PHP 扩展仓库
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

# 安装 PHP 扩展
RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
    install-php-extensions mbstring pdo_mysql zip exif pcntl gd memcached

# 安装依赖
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    unzip \
    git \
    curl \
    lua-zlib-dev \
    libmemcached-dev \
    nginx

# 安装 Supervisor
RUN apt-get install -y supervisor

# 安装 Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# 清除缓存
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# 为 Laravel 应用程序添加用户
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# 将代码复制到 /var/www
COPY --chown=www:www-data . /var/www

# 将 root 添加到 www 组
RUN chmod -R ug+w /var/www/storage

# 复制 Nginx/PHP/Supervisor 配置
RUN cp docker/supervisor.conf /etc/supervisord.conf
RUN cp docker/php.ini /usr/local/etc/php/conf.d/app.ini
RUN cp docker/nginx.conf /etc/nginx/sites-enabled/default

# PHP 错误日志文件
RUN mkdir /var/log/php
RUN touch /var/log/php/errors.log && chmod 777 /var/log/php/errors.log

# 部署步骤
RUN composer update
RUN composer install --optimize-autoloader --no-dev
RUN chmod +x /var/www/docker/run.sh

EXPOSE 80
ENTRYPOINT ["/var/www/docker/run.sh"]

and this is always adding root permission to the /var/www/storage/logs directory.

Laravel docker image unable to change permission to /var/www/storage/logs/ always is root owner

Here all seems to be ok, but when I did ls -l !Surprise! the logs/laravel.log file has root permission:

Laravel docker image unable to change permission to /var/www/storage/logs/ always is root owner

And when I tried do login by using postman here is the error:

<!-- The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied
The exception occurred while attempting to log: The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied

I don't know what to do to prevent this problem guys, if you have any idea to fix this problem I will appreciate it.

I have tried to add it:

sudo chmod -R ugo+rw storage

But the logs/laravel.log file always has root permission. Why happens it?

thanks so much.

英文:

I have the next Dockerfile:

FROM php:8.1.0-fpm
# Set working directory
WORKDIR /var/www
# Add docker php ext repo
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
# Install php extensions
RUN chmod +x /usr/local/bin/install-php-extensions &amp;&amp; sync &amp;&amp; \
install-php-extensions mbstring pdo_mysql zip exif pcntl gd memcached
# Install dependencies
RUN apt-get update &amp;&amp; apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
unzip \
git \
curl \
lua-zlib-dev \
libmemcached-dev \
nginx
# Install supervisor
RUN apt-get install -y supervisor
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Clear cache
RUN apt-get clean &amp;&amp; rm -rf /var/lib/apt/lists/*
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy code to /var/www
COPY --chown=www:www-data . /var/www
# add root to www group
RUN chmod -R ug+w /var/www/storage
# Copy nginx/php/supervisor configs
RUN cp docker/supervisor.conf /etc/supervisord.conf
RUN cp docker/php.ini /usr/local/etc/php/conf.d/app.ini
RUN cp docker/nginx.conf /etc/nginx/sites-enabled/default
# PHP Error Log Files
RUN mkdir /var/log/php
RUN touch /var/log/php/errors.log &amp;&amp; chmod 777 /var/log/php/errors.log
# Deployment steps
RUN composer update
RUN composer install --optimize-autoloader --no-dev
RUN chmod +x /var/www/docker/run.sh
EXPOSE 80
ENTRYPOINT [&quot;/var/www/docker/run.sh&quot;]

and this is always adding root permission to the /var/www/storage/logs directory.

Laravel docker image unable to change permission to /var/www/storage/logs/ always is root owner

Here all seems to be ok, but when I did ls -l !Surprise! the logs/laravel.log file has root permission:

Laravel docker image unable to change permission to /var/www/storage/logs/ always is root owner

And when I tried do login by using postman here is the error:

&lt;!-- The stream or file &amp;quot;/var/www/storage/logs/laravel.log&amp;quot; could not be opened in append mode: Failed to open stream: Permission denied
The exception occurred while attempting to log: The stream or file &amp;quot;/var/www/storage/logs/laravel.log&amp;quot; could not be opened in append mode: Failed to open stream: Permission denied

I don't know what to do to prevent this problem guys, if you have any idea to fix this problem I will appreciate it.

I have tried to add it:

sudo chmod -R ugo+rw storage

But the logs/laravel.log file always has root permission. Why happens it?

thanks so much.

答案1

得分: 2

USER www

RUN composer update
RUN composer install --optimize-autoloader --no-dev
RUN chmod +x /var/www/docker/run.sh

英文:

Seems like you forgot to change user to www before starting the app, and Laravel is creating all files under root

# Here it goes
USER www
# Deployment steps
RUN composer update
RUN composer install --optimize-autoloader --no-dev
RUN chmod +x /var/www/docker/run.sh

huangapple
  • 本文由 发表于 2023年5月22日 06:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定