我尝试为 laravel 5.5 应用程序和 nginx 创建 dockerfile。我使用类似于以下 dockerfile:
FROM richarvey/nginx-php-fpm:1.3.7
# we cannot directly work in /var/www/html beaouse in richarvey/nginx-php-fpm is VOLUME directive so if we create
# files in this directory - they will 'disappear' - so we use /tmp dir.
WORKDIR /tmp/project
# due to docker cache
COPY ./composer.json .
RUN composer install --no-scripts --no-autoloader
ADD . .
RUN composer dump-autoload --optimize
RUN php artisan key:generate
ADD ./config/server-nginx.conf /etc/nginx/sites-available/default.conf
WORKDIR /var/www/html
CMD if [ -f index.php ]; then \
rm index.php &&\
mv /tmp/project/* /var/www/html/ && \
mv /tmp/project/.* /var/www/html/ | : &&\
php artisan config:clear &&\
php artisan cache:clear ; \
fi &&\
echo "Try connect to db and set up schema..." && \
php artisan migrate --seed --force &&\
/start.sh
我的项目/config/server-nginx.conf 看起来像这样:
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/html/public;
index index.php index.html index.htm;
server_name _;
# Add stdout logging
error_log /dev/stdout info;
access_log /dev/stdout;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
...
}
}
问题是在我的 macOS 和 ubuntu 中一切正常,但是我使用 Docker Claud 和 DigitalOcean 的客户端在容器运行后出现以下问题(所以构建步骤很好,但在容器运行后它被 docker 杀死 - 所以在 CMD dockerfile 部分):
Fatal error: Uncaught Error: Class 'Illuminate\Foundation\Application' not found in /var/www/html/vendor/autoload.php:14
Stack trace:
#0 /var/www/html/artisan(18): require()
最佳答案
因此,当我们在某个目录(带有 php 项目)中使用 composer ...
然后我们移动/重命名该目录(通过 CMD dockerfile 部分中的 mv ...
bash 命令)时,有时会出现问题(! - 在某些主机上 - 为什么?) - 所以如果我们这样做它(在某些主机中)然后文件 autoload.php (由 Composer 生成) will have not proper paths to php classes 。
但是在这种情况下,我们使用 dir tmp/project
(并在内部调用 composer
)然后将其移动到/var/www/html,因为我们不能直接在/var/www/html 中工作,因为 richarvey/nginx-php-fpm
是 VOLUME 指令,所以如果我们在这个目录——它们会“消失”——所以我们使用/tmp 目录。
另外,当我们想通过 ln
/var/www/html
链接到 /tmp/project
并使用 chown -R nginx:nginx /tmp/project
时,也会出现问题,因为它将进入无限循环并且永远不会结束...... :((为什么?)。
解决方案
是将 project/config/server-nginx.conf
中的根目录更改为:
root /var/www/app/public;
然后将 dockerfile 中的 workdir 更改为:
WORKDIR /var/www/app
并在 dockerfile CMD 部分添加
chown -R nginx:nginx /var/www/app/storage
仅授予对 laravel 的写入权限到存储目录(laravel 在其中保存工作数据)(如果我们使用
chown -R nginx:nginx /var/www/app/storage
它将更新“结束”(进入无限循环):()通过这种方式,我们避免移动
composer
编译目录,并且所有主机上的一切都正常工作:)
关于php - 在某些 docker 主机上的第 14 行的 autoload.php 中找不到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46950688/