“Docker + Laravel issue – ‘ViewException'”

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

Docker + Laravel issue - "ViewException"

问题

以下是您要翻译的内容:

docker-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

default.conf:

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}"

我已翻译上述内容,如果您有任何其他需要,请随时提出。

英文:

docker-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

default.conf:

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 file:

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}"

I'm trying to run laravel application and I can tell for sure that database is working properly, .env file configuration is correct and application installation goes successfully (installation does database connection and creates all necessary tables correctly with correct default data inside them)

App works perfectly outside Docker container on Windows machine, problem emerges inside Docker container . My Docker uses linux containers. After reading laravel.log I see that ViewException gets thrown and none of the view files are loaded inside browser (instead I get 404 on homepage, on login page I get the exception which can be seen on screenshot below)

login page

I guess that app doesn't resolve correct path for view files and my question is if anyone here knows what could be causing this problem?

答案1

得分: 0

为 PHP 容器部分添加以下内容:

depends_on:
    - mysql

解决方案:

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

add

    depends_on:
        - mysql

for php container section.
solution

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

答案2

得分: 0

Update DB_HOST in .env

DB_HOST=mysql 

In your .env file the hostname for your database is localhost

DB_HOST=localhost 

The reason it's working locally is because you exposed 3306 from docker to the host machine 3306

But both containers can access each other in the internal docker network by using their docker hostname, which is mysql based on your docker-compose file

Another solution but I don't recommend it (Because since the mysql is already in the same stack no need to connect to host then go back to the mysql container since containers can access each other directly without using port exposed on host, and you can actually stop exposing this port later and it'll still work between the two containers)

DB_HOST=host.docker.internal 
英文:

Short

Update DB_HOST in .env

DB_HOST=mysql

More about that

In your .env file the hostname for your database is localhost

DB_HOST=localhost

The reason it's working locally is because you exposed 3306 from docker to the host machine 3306

But both containers can access each other in internal docker network by using their docker hostname which is mysql based on your docker-compose file

Another solution but I don't recommend it (Because since the mysql is already in the same stack no need to connect to host then go back to the mysql container since containers can access each other directly without using port exposed on host, and you can actually stop exposing this port later and it'll still work between the two containers)

DB_HOST=host.docker.internal

huangapple
  • 本文由 发表于 2020年1月7日 01:41:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616600.html
匿名

发表评论

匿名网友

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

确定