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 Air 上本地构建容器时,这个问题只会发生在数字海洋中,这个问题不是他们的
在使用暴露端口之前尝试将用户设置为 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