Connect to Docker MySQL container from localhost?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To connect to a MySQL container from your host machine, the container must publish the MySQL port to the host. Once that port is mapped, your client connects to 127.0.0.1 or localhost on the mapped port using normal MySQL credentials.
The Essential Docker Flag: Port Publishing
A MySQL container is not automatically reachable from the host. You need to publish the container's port.
The key part is:
- '
-p 3306:3306'
That maps host port 3306 to container port 3306.
After that, a local client can connect to the database through the host port.
The Connection Settings From The Host
If you used the mapping above, typical connection values are:
- host:
127.0.0.1 - port:
3306 - username:
rootor another configured user - password: the one you supplied through the container environment
For example with the MySQL CLI:
Using 127.0.0.1 instead of bare localhost is often clearer because some MySQL clients treat localhost specially and may try a Unix socket instead of TCP.
Using A Different Host Port
If your machine already has something on port 3306, publish the container on another host port.
Now connect with:
This is a very common development setup.
Docker Compose Example
The same idea in Compose looks like this:
Then start it with:
Your local GUI client or CLI can connect through the exposed host port.
Verify The Container Is Ready
A published port does not mean MySQL is already ready to accept connections. Containers often need a little time to initialize.
Check logs:
Check that the port is published:
You want to see the port mapping in the container list before blaming your client settings.
Common Reasons The Connection Fails
If localhost connection fails, the usual causes are:
- the port was never published
- you are connecting to the wrong host port
- MySQL is still starting up
- the password or user is wrong
- another local MySQL instance is already using the same port
Those are much more common than obscure Docker networking problems.
Host Versus Container Networking
This question is specifically about host to container access. That is different from container to container access.
If another container wants to reach MySQL, it usually uses the Docker network name such as db, not localhost.
A useful rule is:
- host machine to container: use published host port
- container to container: use Docker network service name
Mixing those two mental models causes a lot of confusion.
Common Pitfalls
The most common mistake is forgetting -p hostPort:containerPort. Without that, the container may be running correctly but still be unreachable from the host.
Another mistake is using localhost with a client that silently prefers a local Unix socket instead of TCP. 127.0.0.1 avoids that ambiguity.
Developers also forget that MySQL takes time to initialize, especially on the first run with a new data volume.
Finally, if a local MySQL server is already using port 3306, either stop it or publish the container on a different host port.
Summary
- Publish the MySQL container port with
-p, for example-p 3306:3306. - Connect from the host using
127.0.0.1and the published port. - Use a different host port such as
3307if3306is already in use. - Verify the container is running and MySQL has finished starting up.
- Remember that host-to-container and container-to-container networking are different cases.

