Connect to local MySQL from Docker Container

Published on Author JFLeave a comment

You’re running a docker web app and it connects fine to the IP address you specified on a shared database server, but you can’t connect to MySQL running on your workstation hosting the container.

How do you connect to your local machine’s instance of MySQL?

Easy.

In this example, we’re doing dev coding on our local machine while running the container.

The container thinks localhost or 127.0.0.1 is itself.

So, simply use “host.docker.internal”, which will map to your local machine’s IP address.

If you’re using a variable in a .env file, it would something like

db_host="host.docker.internal"

Don’t forget to give MySQL permissions to that user to connect from a remote host.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.