0

I'm trying to connect the laravel app inside docker with MySQL on my host machine.

On my host machine MySQL is running is connected with following credentials.

Host OS MySQL

MySQL version: 8.0.27

Host: 127.0.0.1

Port: 3307

Password: No password

Devilbox .env file:

HOST_PORT_MYSQL=3306

Laravel .env file

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=hanger

DB_USERNAME=root

DB_PASSWORD=

OS / ENVIRONMENT:

Host operating system and version: MacOS Monterey 12.5.1

Docker desktop version: 4.12.0 (85629)

Docker desktop engine: Engine: 20.10.17

Docker desktop compose version: v2.10.2

If i run docker-compose up -d php httpd mysql everything work fine i'm able to connect to database via devilbox phpmyadmin.

As per the documentation https://devilbox.readthedocs.io/en/latest/corporate-usage/use-external-databases.html#database-on-host-os i ran this command docker-compose up -d php httpd bind but i unable to connect to the database from inside the docker to host OS MySQL.

I want to connect the laravel app in docker with MySQL on my host machine.

Wcan
  • 840
  • 1
  • 10
  • 31
  • 2
    If you are using docker desktop I think that binds `docker.internal.host` as the DNS entry to connect to the host so you can try `DB_HOST=docker.internal.host` otherwise you need to check your network adapters to find out what the IP of the host in the docker adapter is. I don't know how to do that on Mac – apokryfos Sep 10 '22 at 19:40
  • Personal recommendation, if you want laravel, just use [Laravel Sail](https://laravel.com/docs/9.x/sail) not `Devilbox` – matiaslauriti Sep 10 '22 at 20:21
  • @apokryfos thankyou for the comment, I've tried ```host.docker.internal``` as mentioned in the documentation but its not working. changed port from 3306 to 3307 as well. but still nothing. – Wcan Sep 10 '22 at 21:11
  • @matiaslauriti thankyou for the recommendation, i understand that sail is the better option but larave sail is not avaiable in laravel 5.6, i've different project in different versions of laravel, also some projects are in vanilla php. – Wcan Sep 10 '22 at 21:12
  • @Wcan For older versions of Laravel, [Laradock](https://laradock.io/) could be an option worth giving a try. – steven7mwesigwa Sep 10 '22 at 21:39
  • @steven7mwesigwa yes i'm already looking for other options including laradock, would it be possible to use host OS MySQL on laradock ? – Wcan Sep 10 '22 at 21:45
  • @Wcan why do you need or want to use a Host OS MySQL instead of a Docker container on the same `docker-compose` file? – matiaslauriti Sep 10 '22 at 23:00
  • @matiaslauriti I actually have so many different projects running on different php versions (php5.6, php7.2, php7.3, php8.0) and different mysql/mariadb versoins, it is difficult for me to setup every database seperatly. – Wcan Sep 10 '22 at 23:40
  • @matiaslauriti also if i'm working on one project and docker-compose 2nd project i've to change the database. – Wcan Sep 10 '22 at 23:41
  • I'm new to docker i don't know what should i do, should i use stack like devilbox and keep all my projects under same stack or should i create seperate environment for each project. – Wcan Sep 10 '22 at 23:43
  • well, each project should have its own database, or be as similar (if 100% better) to what you have in production (versions) but I do understand your frustration, it is very difficult at the beginning, so I recommend you to look for tutorials about Docker, it is 99% used for this type of things, so I would learn it as it is not exactly difficult, so you understand how to do anything in there – matiaslauriti Sep 10 '22 at 23:55
  • 1
    It's suggested online that on Mac the terminal command `ifconfig` might work and in there you might see the docker network adapter listed along with what IP is assigned to the host so you can try using that. – apokryfos Sep 11 '22 at 06:31

0 Answers0