首页 > 解决方案 > Docker + Laravel 问题 - “ViewException”

问题描述

码头工人-compose.yml:

version: '3'

networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel
  mysql:
    image: mysql:5.7.22
    container_name: mysql
    restart: unless-stopped
    tty: true
    ports: 
      - "3306:3306"
    volumes: 
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel
  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes: 
      - ./src:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - laravel

默认配置:

server {
 listen 80;
 index index.php index.html;
 server_name localhost;
 error_log /var/log/nginx/error.log;
 access_log /var/log/nginx/access.log;
 root /var/www/html/public;


 location / {
  try_files $uri $uri/ /index.php?$query_string;
 }


 location ~ \.php$ {
  try_files $uri =404;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass php:9000;
  fastcgi_index index.php;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param PATH_INFO $fastcgi_path_info;
 }
}

Dockerfile:

FROM php:7.2-fpm-alpine

RUN docker-php-ext-install pdo pdo_mysql

.env 文件:

APP_NAME=Laraone
APP_ENV="local"
APP_KEY=base64:gw3VfvtH3/S/iYRWp0q8MsMB7LPthzkwoPHhJZhFF+o=
APP_DEBUG=true
APP_URL=http://localhost:8080

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laraone
DB_USERNAME=root
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=

MAIL_FROM_ADDRESS=noreply@example.com
MAIL_FROM_NAME="${APP_NAME}"

MAIL_SENDMAIL="/usr/sbin/sendmail -bs"

MAILGUN_DOMAIN=
MAILGUN_SECRET=
MAILGUN_ENDPOINT="api.mailgun.net"

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

我正在尝试运行 laravel 应用程序,我可以确定数据库工作正常,.env 文件配置正确并且应用程序安装成功(安装完成数据库连接并正确创建所有必要的表,其中包含正确的默认数据)

应用程序在 Windows 机器上的 Docker 容器外完美运行,问题出现在 Docker 容器内。我的 Docker 使用 linux 容器。阅读 laravel.log 后,我看到 ViewException 被抛出,并且没有在浏览器中加载任何视图文件(相反,我在主页上得到 404,在登录页面上我得到异常,可以在下面的屏幕截图中看到)

登录页面

我猜该应用程序无法解析视图文件的正确路径,我的问题是这里是否有人知道可能导致此问题的原因?

标签: phplinuxlaraveldockernginx

解决方案


添加

    depends_on:
        - mysql

对于 php 容器部分。解决方案

php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes: 
      - ./src:/var/www/html
    depends_on:
      - mysql
    ports:
      - "9000:9000"
    networks:
      - laravel

推荐阅读