How to connect locally hosted MySQL database with the docker container
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Connecting a Dockerized application to a MySQL server running on your host machine is a frequent development setup, especially when containerizing only the app layer first. The main challenge is that localhost inside a container refers to the container itself, not your host. A reliable connection setup requires correct host address, MySQL bind settings, and user privileges.
Understand the Networking Boundary
When your app runs in a container, network namespaces are isolated. So this will fail from inside container:
- host set to
127.0.0.1 - host set to
localhost
You must point to an address reachable from container that routes to host machine.
Common host targets:
- Docker Desktop on macOS and Windows:
host.docker.internal - Linux: host gateway IP, often bridge gateway such as
172.17.0.1or explicit host-gateway mapping
Configure MySQL to Accept External Connections
By default, MySQL may bind only to loopback interface. Check bind-address in MySQL config and set it to an interface reachable from Docker bridge if needed.
Example in mysqld.cnf:
After changing config, restart MySQL service.
Security note:
- do this only in controlled development environments unless firewall and access controls are configured properly
Create a MySQL User for Container Access
Do not use broad root access from app containers. Create a dedicated user with limited privileges.
For stricter setup, replace % with expected Docker subnet host pattern.
Example App Container Connection with docker run
Inside your app, build connection string from these environment variables.
Python example:
Linux-Specific Host Gateway Mapping
On Linux, host.docker.internal may not resolve by default. Add explicit host gateway mapping.
This provides a consistent hostname pattern across operating systems.
Docker Compose Example
Compose keeps local development config repeatable across teammates.
Troubleshooting Checklist
If connection fails, check each layer:
- MySQL service is running on host
- MySQL listens on expected interface and port
- firewall allows traffic from Docker bridge network
- app container resolves host name correctly
- database credentials and user host permissions are correct
Useful checks from container:
If TCP port is reachable but auth fails, issue is likely user privileges or password.
Security and Development Hygiene
Even in development, avoid storing plaintext credentials directly in source files. Use environment files excluded from version control or local secrets managers.
Also avoid exposing MySQL broadly unless necessary. Restrict host firewall to local development subnets where possible.
Common Pitfalls
- Using
localhostin container expecting it to reach host MySQL. - Forgetting to adjust MySQL bind address from loopback-only configuration.
- Granting user only for
localhostand expecting remote container access. - Assuming
host.docker.internalalways resolves on Linux without host-gateway mapping. - Debugging app code first when issue is actually network path or MySQL privileges.
Summary
- Containers cannot reach host services through
localhost; use host-reachable address. - Configure MySQL bind and user permissions to allow container-originated connections.
- Use
host.docker.internalwhere available, with host-gateway mapping on Linux. - Keep configuration in environment variables for repeatable local setup.
- Troubleshoot systematically across network, service, and credential layers.
Related reading
- How to connect MySQL running on Kubernetes
- How to connect to local clickhouse db form container?
- how to connect to localhost9092 from docker container using docker-compose and not using docker bridge
- How to continue a Docker container which has exited
- How to connect mongodb clients to local Meteor MongoDB
- How to connect to Cassandra inside a Pylons app?
- How to copy a file to a Docker container before starting/running it?
- How to copy Docker images from one host to another without using a repository

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.