I have a laravel project setup with docker (I currently run it using docker-compose). The project was working fine yesterday but it has stopped functioning partially today.
The most amazing thing is that although migrations
and seeders
work just fine, I can’t fetch any result from the database! I’ve tried clearing caches and every other guide I could find but they didn’t work at all.
My file contents are:
.env
APP_NAME=bookshelf
APP_ENV=local
APP_KEY=base64:/PqZs+/8YfRYtCth0U5LATD5oOXCN/5pdAs5onk1z7U=
APP_DEBUG=true
APP_URL=http://0.0.0.0:8000
DB_CONNECTION=mysql
DB_HOST=0.0.0.0
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret
docker-compose.yml
version: "3.7"
services:
web:
image: $COMPOSE_PROJECT_NAME
container_name: $COMPOSE_PROJECT_NAME-laravel
ports:
- 8000:8000
volumes:
- ./:/app
mysql:
image: mysql:8
container_name: $COMPOSE_PROJECT_NAME-mysql
ports:
- '3306:3306'
environment:
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: secret
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
environment:
PMA_HOST: 'mysql'
ports:
- '127.0.0.1:8080:80'
Dockerfile
FROM php:8.0
RUN apt-get update && apt-get install -y
libzip-dev
&& docker-php-ext-install
pdo_mysql
zip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /app
COPY . /app
RUN composer install
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
As I said, the following commands work totally ok:
php artisan migrate
php artisan db:seed
But when I want to load a page with the following Controller@method
:
public function index()
{
$records = Book::all();
...
which fetches results from books
table, I get the following error in the corresponding page/url/route:
So… I appreciate any help!
3
Answers
I solved it. For some reason, my MySQL
.env
config worked fine yesterday but I had to set theDB_HOST
tomysql
which is the name of the service, created indocker-compose.yml
; so, after changing it:docker-compose.yml
.env
I got the idea from this answer and as I tried it, it worked!
I'm gonna leave this Q&A for anyone else who might have such an ambiguous problem.
EDIT: Strangely enough, after doing so,
migrate
command has stopped working.I am currently oscillating between
DB_HOST=0.0.0.0
formigrations
andseeding
andDB_HOST=mysql
for other purposes throughout the application... If anyone got a solution for this, let me knowIn Laravel you have
config/database.php
where all the setup for the connection is located. You also have a.env
file in the root directory in your project (which everyone uses for timesaving). This contains variables that you can use for the entire project.On a standard L5 project the MySql section of
config/database.php
looks like this:Notice there is no port set!
Although in my .env file I had set DB_PORT=33060. But that value (3306) was never read into the config/database.php.
So don’t be a dumbass like myself and forget to check the database.php file.
FIX
Simply add ‘port’ => env(‘DB_PORT’, 3306), to your config/database.php and set that value in .env like this DB_PORT=3306
If you’re using sail, try this command:
Works with default Docker & Laravel 9 installation from their official docs: https://laravel.com/docs/9.x#laravel-and-docker