4

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.

yoyoma
  • 3,336
  • 6
  • 27
  • 42
  • 3
    It turns out the .env needs `DB_HOST=mysql` in order for this to work. Still confused but i'll leave this here in case it helps anyone else – yoyoma Oct 15 '17 at 05:28
  • Yeah, that worked for me as well. Before that, the value was `DB_HOST=127.0.0.1` so we have to change that. That localhost assumes MySQL is installed in that same `workspace` container, but in reality, MySQL is installed in another container. Therefore, the `DB_HOST` is an external container. – Pathros Sep 30 '20 at 17:05
  • DB_HOST=, so, in that case it is DB_HOST=mysql, or the name as it is used/written in docker file – tipograf ieromonah Jul 09 '21 at 19:11

2 Answers2

10

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

lchachurski
  • 1,770
  • 16
  • 21
  • Informative, but didn't explain to @yoyoma how to solve his problem (modifying the .env was the right answer). Also, Laravel comes out of the box with MySQL port 3306 exposed to the outside, so you don't have to do it yourself. – vicenteherrera Jun 26 '19 at 15:04
  • There were actually 5 questions asked, my answer offered a fishing rod instead of a fish. Regarding Laravel, it can't expose any ports as it is a framework only. If you meant Laradock, then it was mentioned at the end of my answer, cheers! – lchachurski Jun 26 '19 at 15:17
  • Sorry, meant Laradock, not Laravel. In the case of Laradock, it has a .env property with MYSQL_PORT=3306 and docker-compose.yml property of ports: "${MYSQL_PORT}:3306", so what you told is already taken care of. – vicenteherrera Jun 27 '19 at 14:10
4

I just added the MySQL client to the workspace container so you should be able to run your migrations in there by setting

WORKSPACE_INSTALL_MYSQL_CLIENT=true

in your .env file and running

 docker-compose build workspace

Then you can use

docker-compose exec workspace bash

and do your migrations or whatever MySQL magic your heart desires.

You'll also need to set the mysql host to mysql in your database.php config.

And make sure to edit the .env file of the project and add DB_HOST=mysql.

Mike P. Sinn
  • 126
  • 1
  • 1
  • 4
  • You should edit this answer and add the fact that the first step is to edit the .env file of the project and add DB_HOST=mysql – vicenteherrera Jun 26 '19 at 15:05
  • Any ideas or approach how to execute mysqldump from another container? For example, when there's a package that automates backups but it assumes mysqldump is in the same host. [Question here](https://stackoverflow.com/q/64070267/1883256). – Pathros Sep 27 '20 at 13:58
  • 1
    @Pathros If you follow the instructions in this answer, you should get `mysqldump` in `workspace`. – Adrien Oct 14 '20 at 10:39