“Docker + Laravel issue – ‘ViewException'”

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

Docker + Laravel issue - "ViewException"

问题

以下是您要翻译的内容:

docker-compose.yml:

  1. version: '3'
  2. networks:
  3. laravel:
  4. services:
  5. nginx:
  6. image: nginx:stable-alpine
  7. container_name: nginx
  8. ports:
  9. - "8080:80"
  10. volumes:
  11. - ./src:/var/www/html
  12. - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
  13. depends_on:
  14. - php
  15. - mysql
  16. networks:
  17. - laravel
  18. mysql:
  19. image: mysql:5.7.22
  20. container_name: mysql
  21. restart: unless-stopped
  22. tty: true
  23. ports:
  24. - "3306:3306"
  25. volumes:
  26. - ./mysql:/var/lib/mysql
  27. environment:
  28. MYSQL_DATABASE: homestead
  29. MYSQL_USER: homestead
  30. MYSQL_PASSWORD: secret
  31. MYSQL_ROOT_PASSWORD: secret
  32. SERVICE_TAGS: dev
  33. SERVICE_NAME: mysql
  34. networks:
  35. - laravel
  36. php:
  37. build:
  38. context: .
  39. dockerfile: Dockerfile
  40. container_name: php
  41. volumes:
  42. - ./src:/var/www/html
  43. ports:
  44. - "9000:9000"
  45. networks:
  46. - laravel

default.conf:

  1. server {
  2. listen 80;
  3. index index.php index.html;
  4. server_name localhost;
  5. error_log /var/log/nginx/error.log;
  6. access_log /var/log/nginx/access.log;
  7. root /var/www/html/public;
  8. location / {
  9. try_files $uri $uri/ /index.php?$query_string;
  10. }
  11. location ~ \.php$ {
  12. try_files $uri =404;
  13. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  14. fastcgi_pass php:9000;
  15. fastcgi_index index.php;
  16. include fastcgi_params;
  17. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  18. fastcgi_param PATH_INFO $fastcgi_path_info;
  19. }
  20. }

Dockerfile:

  1. FROM php:7.2-fpm-alpine
  2. RUN docker-php-ext-install pdo pdo_mysql

.env文件:

  1. APP_NAME=Laraone
  2. APP_ENV="local"
  3. APP_KEY=base64:gw3VfvtH3/S/iYRWp0q8MsMB7LPthzkwoPHhJZhFF+o=
  4. APP_DEBUG=true
  5. APP_URL=http://localhost:8080
  6. LOG_CHANNEL=stack
  7. DB_CONNECTION=mysql
  8. DB_HOST=localhost
  9. DB_PORT=3306
  10. DB_DATABASE=laraone
  11. DB_USERNAME=root
  12. DB_PASSWORD=secret
  13. BROADCAST_DRIVER=log
  14. CACHE_DRIVER=file
  15. SESSION_DRIVER=file
  16. QUEUE_DRIVER=sync
  17. REDIS_HOST=127.0.0.1
  18. REDIS_PASSWORD=
  19. REDIS_PORT=6379
  20. MAIL_DRIVER=smtp
  21. MAIL_HOST=mailtrap.io
  22. MAIL_PORT=2525
  23. MAIL_USERNAME=
  24. MAIL_PASSWORD=
  25. MAIL_ENCRYPTION=
  26. MAIL_FROM_ADDRESS=noreply@example.com
  27. MAIL_FROM_NAME="${APP_NAME}"
  28. MAIL_SENDMAIL="/usr/sbin/sendmail -bs"
  29. MAILGUN_DOMAIN=
  30. MAILGUN_SECRET=
  31. MAILGUN_ENDPOINT="api.mailgun.net"
  32. PUSHER_APP_ID=
  33. PUSHER_APP_KEY=
  34. PUSHER_APP_SECRET=
  35. PUSHER_APP_CLUSTER=mt1
  36. MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
  37. MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

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

英文:

docker-compose.yml:

  1. version: '3'
  2. networks:
  3. laravel:
  4. services:
  5. nginx:
  6. image: nginx:stable-alpine
  7. container_name: nginx
  8. ports:
  9. - "8080:80"
  10. volumes:
  11. - ./src:/var/www/html
  12. - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
  13. depends_on:
  14. - php
  15. - mysql
  16. networks:
  17. - laravel
  18. mysql:
  19. image: mysql:5.7.22
  20. container_name: mysql
  21. restart: unless-stopped
  22. tty: true
  23. ports:
  24. - "3306:3306"
  25. volumes:
  26. - ./mysql:/var/lib/mysql
  27. environment:
  28. MYSQL_DATABASE: homestead
  29. MYSQL_USER: homestead
  30. MYSQL_PASSWORD: secret
  31. MYSQL_ROOT_PASSWORD: secret
  32. SERVICE_TAGS: dev
  33. SERVICE_NAME: mysql
  34. networks:
  35. - laravel
  36. php:
  37. build:
  38. context: .
  39. dockerfile: Dockerfile
  40. container_name: php
  41. volumes:
  42. - ./src:/var/www/html
  43. ports:
  44. - "9000:9000"
  45. networks:
  46. - laravel

default.conf:

  1. server {
  2. listen 80;
  3. index index.php index.html;
  4. server_name localhost;
  5. error_log /var/log/nginx/error.log;
  6. access_log /var/log/nginx/access.log;
  7. root /var/www/html/public;
  8. location / {
  9. try_files $uri $uri/ /index.php?$query_string;
  10. }
  11. location ~ \.php$ {
  12. try_files $uri =404;
  13. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  14. fastcgi_pass php:9000;
  15. fastcgi_index index.php;
  16. include fastcgi_params;
  17. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  18. fastcgi_param PATH_INFO $fastcgi_path_info;
  19. }
  20. }

Dockerfile:

  1. FROM php:7.2-fpm-alpine
  2. RUN docker-php-ext-install pdo pdo_mysql

.env file:

  1. APP_NAME=Laraone
  2. APP_ENV="local"
  3. APP_KEY=base64:gw3VfvtH3/S/iYRWp0q8MsMB7LPthzkwoPHhJZhFF+o=
  4. APP_DEBUG=true
  5. APP_URL=http://localhost:8080
  6. LOG_CHANNEL=stack
  7. DB_CONNECTION=mysql
  8. DB_HOST=localhost
  9. DB_PORT=3306
  10. DB_DATABASE=laraone
  11. DB_USERNAME=root
  12. DB_PASSWORD=secret
  13. BROADCAST_DRIVER=log
  14. CACHE_DRIVER=file
  15. SESSION_DRIVER=file
  16. QUEUE_DRIVER=sync
  17. REDIS_HOST=127.0.0.1
  18. REDIS_PASSWORD=
  19. REDIS_PORT=6379
  20. MAIL_DRIVER=smtp
  21. MAIL_HOST=mailtrap.io
  22. MAIL_PORT=2525
  23. MAIL_USERNAME=
  24. MAIL_PASSWORD=
  25. MAIL_ENCRYPTION=
  26. MAIL_FROM_ADDRESS=noreply@example.com
  27. MAIL_FROM_NAME="${APP_NAME}"
  28. MAIL_SENDMAIL="/usr/sbin/sendmail -bs"
  29. MAILGUN_DOMAIN=
  30. MAILGUN_SECRET=
  31. MAILGUN_ENDPOINT="api.mailgun.net"
  32. PUSHER_APP_ID=
  33. PUSHER_APP_KEY=
  34. PUSHER_APP_SECRET=
  35. PUSHER_APP_CLUSTER=mt1
  36. MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
  37. 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 容器部分添加以下内容:

  1. depends_on:
  2. - mysql

解决方案:

  1. php:
  2. build:
  3. context: .
  4. dockerfile: Dockerfile
  5. container_name: php
  6. volumes:
  7. - ./src:/var/www/html
  8. depends_on:
  9. - mysql
  10. ports:
  11. - "9000:9000"
  12. networks:
  13. - laravel
英文:

add

  1. depends_on:
  2. - mysql

for php container section.
solution

  1. php:
  2. build:
  3. context: .
  4. dockerfile: Dockerfile
  5. container_name: php
  6. volumes:
  7. - ./src:/var/www/html
  8. depends_on:
  9. - mysql
  10. ports:
  11. - "9000:9000"
  12. networks:
  13. - laravel

答案2

得分: 0

Update DB_HOST in .env

  1. DB_HOST=mysql

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

  1. 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)

  1. DB_HOST=host.docker.internal
英文:

Short

Update DB_HOST in .env

  1. DB_HOST=mysql

More about that

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

  1. 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)

  1. 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:

确定