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)
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?
Advertisement
Answer
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