Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

diazfarindra's avatar

connecting MySQL server from outside docker container with Laravel inside docker container

hello, I'm just learning laravel sail, and found something I don't understand.

i have mysql installed in wsl2 ubuntu, and everything is configured properly, mysql in wsl can connect to tableplus which is based on my windows.

but i can't connect it with my laravel which is in docker container. i have tried every solution on the internet, but i don't find any similar with my case.

is there a way to connect laravel in the docker container to mysql server in wsl?

is laravel in contanier really isolated?

thanks in advance

0 likes
4 replies
fideloper's avatar
Level 11

First, make sure MySQL is running in the container. It's possible you're connecting to a MySQL running outside of sail.

Run sail ps to see if the mysql container is running, or if it's exited. If it's exited, it likely means MySQL is running somewhere else already, and Sail's MySQL container can't bind to (listen for connections on) the same port.

Second, Assuming you haven't edited the docker-compose.yml file that Sail makes, then the mysql container is run in a service named mysql. The hostname to use from within the Laravel application (the .env file) should match the service name of the database container. That's likely mysql:

DB_HOST=mysql

Assuming your docker-compose.yml looks like this screenshot: https://d.pr/i/sIDfpw

Lastly Be sure if you're running artisan commands to run sail artisan foo instead of just php artisan foo. You'll want to run artisan commands from within the Laravel container, not from outside of it.

1 like
diazfarindra's avatar

@fideloper sorry for the late response, thanks for the answer, my mistake turned on both MySQL service in docker and in WSL. now everything is good

but is there a way to use MySQL that is outside the docker container?

MySQL port in the docker container is 3306, and MySQL port that runs under WSL is 3307. I've not edited anything in docker-compose.yml yet.

fideloper's avatar

@diazfarindra Run sail ps to see if the port forwarding is setup

For example, on my machine, I see this:

$ docker ps # You can run this instead of `sail ps`
CONTAINER ID   IMAGE       COMMAND                  CREATED        STATUS      PORTS                                                  NAMES
a47868bff8a5   mysql:8.0   "docker-entrypoint.s…"   2 months ago   Up 7 days   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   TO--mysql--8.0--3306

This shows me that my mysl container is forwarding port 3306 into port 3306 within the container (so outside of the container, I can connect to it using hostname/port 127.0.0.1:3306).

If you see something like 0.0.0.0:33060->3306, then you'd use 127.0.0.1:33060 to connect to MySQL from outside of the container.

I'm not sure if Windows/WSL makes that more complicated or not, I haven't used Docker on Windows before.

Please or to participate in this conversation.