ssl Laravel + Docker:文件无法在追加模式下打开:无法打开流:权限被拒绝

n8ghc7c1  于 5个月前  发布在  Docker
关注(0)|答案(1)|浏览(78)
FROM php:7.4-apache

# Install dependencies
RUN apt-get update && \
    apt-get install -y \
    libzip-dev \
    zip

# Enable Apache modules
RUN a2enmod rewrite
RUN a2enmod headers
RUN a2enmod ssl

RUN mkdir -p /etc/apache2/ssl  # Create the ssl directory

# Generate a self-signed certificate
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/cert-key.pem -out /etc/apache2/ssl/cert.pem \
    -subj "/C=US/ST=California/L=New York/O=Zylu/OU=Zylu/CN=zylu.co"

COPY ./apache/000-default.conf /etc/apache2/sites-available/000-default.conf

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

ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# Copy the application code
COPY . /var/www/html

# Set the working directory
WORKDIR /var/www/html

RUN chown -R www-data:www-data /var/www/html/storage \
    && chown -R www-data:www-data /var/www/html/bootstrap/cache

RUN chmod -R 777 /var/www/html/storage \
    && chmod -R 777 /var/www/html/bootstrap/cache

# Expose port 80 and 443 and start Apache
EXPOSE 80
EXPOSE 443

字符串
我在DigitalOcean上部署Docker容器时遇到权限错误。具体来说,报告的错误是:

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


有趣的是,在我的Mac M1 Air上本地构建容器时不会发生这个问题。为了解决DigitalOcean上的问题,我临时使用了以下命令的解决方案:

docker exec -u 0 -it <container-id> /bin/bash
RUN chmod -R 777 /var/www/html/storage


这解决了权限问题,但我正在寻求一个直接从docker文件工作的解决方案,因为我想自动化部署。
此外,这个问题只发生在数字海洋,当我尝试在我的mac m1空气本地构建容器时,这个问题不是他们的

kd3sttzy

kd3sttzy1#

尝试在使用暴露端口之前将user设置为www-data

USER www-data

字符串
并修改您的

COPY . /var/www/html


COPY --chown=www-data:www-data . /var/www/html


因此在复制过程中,所有权被转移到容器内的www-data。同时将权限限制为775,它应该可以正常工作,直到和除非你有任何其他问题。
你的最终Dockerfile应该看起来像这样

FROM php:7.4-apache

# Install dependencies
RUN apt-get update && \
    apt-get install -y \
    libzip-dev \
    zip

# Enable Apache modules
RUN a2enmod rewrite
RUN a2enmod headers
RUN a2enmod ssl

RUN mkdir -p /etc/apache2/ssl  # Create the ssl directory

# Generate a self-signed certificate
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/cert-key.pem -out /etc/apache2/ssl/cert.pem \
    -subj "/C=US/ST=California/L=New York/O=Zylu/OU=Zylu/CN=zylu.co"

COPY ./apache/000-default.conf /etc/apache2/sites-available/000-default.conf

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

ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# Copy the application code
COPY --chown=www-data:www-data . /var/www/html

# Set the working directory
WORKDIR /var/www/html

RUN chown -R www-data:www-data /var/www/html/storage \
    && chown -R www-data:www-data /var/www/html/bootstrap/cache

RUN chmod -R 775 /var/www/html/storage \
    && chmod -R 775 /var/www/html/bootstrap/cache

# Set user to www-data to start apache
USER www-data

# Expose port 80 and 443 and start Apache
EXPOSE 80
EXPOSE 443

相关问题