Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laradock doesn't work with mysql

I cannot understand how to get Laradock to work correctly with the mysql db.

I have followed laradock docs and installed everything, spin up containers using

docker-compose up -d nginx mysql

I have the multiple project version layout like such

project
    +laradock-spa

The php side seems to work, I can get the laravel welcome page up, however getting the DB connected is causing me issues.

Firstly, where should I be running php artisan commands like php artisan migrate ? Should that be run from my machine within the project folder, or from within the docker container ?

When I run it from my project folder, it works, and I can go into the mysql container and see the initial db tables, like migration, and user.

BUT, I cannot get an initial POST to the db to work within Postman - I get error SQLSTATE[HY000] [2002] No such file or directory (SQL: insert into users (...

So I thought maybe I'm supposed to be running the migrate command from within the workspace container, so I bash into the workspace, but from here the php artisan migrate command errors with [Illuminate\Database\QueryException] SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schem a.tables where table_schema = spa and table_name = migrations)

As I said, I can successfully get into the mysql container, and login to the DB using root password, and after running first migrations, I can see the tables inside there.

docker ps shows all containers up.

Can someone explain how it works and how to troubleshoot this ? I'm not sure where I should be running which commands, and how the containers talk to each other. But basically, how can I get the mysql db to work in the laravel project?

Btw, I have local mysql running on the PC too, maybe thats causing some conflict/confusion?

I'm running all this on Win10.

like image 268
yoyoma Avatar asked Dec 08 '22 16:12

yoyoma


1 Answers

when you're using Laradock, you need to think about containers as if they were separate servers/computers within one shared network. Each rectangle at the picture below represents one server which has it's own IP address, own Linux system etc. Therefore to connect to MySQL from another container, you need to know an IP address or hostname of this container.

Luckily Laradock provides a little bit of magic under the hood to ease this, and you can use MySQL hostname instead of providing an IP address in your config. If you want to enable Redis, all you have to do is, start Redis with docker-compose up and provide Redis hostname in Laravel config.

If it comes to troubleshooting, docker-compose ps is the best way to check what is going on. The rest is an understanding of a multi-container concept.

Beware you can't connect to your containers from your host PC without exposing containers port in docker-compose.yml. Containers IPs are not visible for a host, as they belong to a virtual network, not the real one where your computer is connected. Default ports are already exposed in docker-compose.yml so you can access DB at IP 127.0.0.1 and port 3306, same with Nginx at port 80 and so on.

Hope this is a bit more clear now :)

enter image description here

like image 107
lchachurski Avatar answered Jan 04 '23 19:01

lchachurski