Connect to MySQL on localhost from Docker container
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Overview
When working with Docker containers, you often need to connect your services to a MySQL database that resides on your host machine, running either directly or in another container. Configuring Docker to connect to a MySQL server running on `localhost` involves several steps. This article will guide you through the necessary steps to successfully connect a Docker container to a MySQL database on your host machine.
Prerequisites
Before proceeding, ensure you have the following installed on your machine:
- Docker
- MySQL
- Necessary tools to manage Docker containers and images (e.g., Docker CLI)
Network Configuration
Docker containers operate on an isolated internal network separate from your host machine. Therefore, when you attempt to access `localhost` or `127.0.0.1`, it will refer to the container's internal network rather than the host's network.
Docker Bridge Network
By default, Docker uses a bridge network to enable communication between containers. However, this does not inherently allow a connection from a container to a service on the host machine. For a Docker container to access the MySQL instance on your host's `localhost`, you need to establish a network link.
Host IP Address
Instead of using `localhost`, which references the container's context, use the host's IP address. On Linux, the default bridge network adapter uses a special hostname `host.docker.internal` to refer to the host. However, for other operating systems like Windows and macOS:
- Windows & macOS: Use the special alias `host.docker.internal` when specifying the host in your database configuration inside the container.
- Linux: You might need to determine the actual host IP address or set up `host.docker.internal` manually via Docker configurations.
MySQL Configuration
Ensure your MySQL instance is configured to accept connections. Check that:
- It's running on the expected port (default 3306).
- User permissions are set to allow remote connections.
Example MySQL user setup:
- mynet
- Firewall Rules: Check if your system's firewall permits connections on MySQL's default port (3306). Open the port using your OS-specific command or GUI tools.
- MySQL Bind Address: In the MySQL configuration file (usually `my.cnf` or `my.ini`), ensure `bind-address` is set to `0.0.0.0` or your specific host IP.
- SELinux: On Linux systems, SELinux might block the connection. Temporarily set SELinux to permissive mode to test if it’s the source of the problem.
- MySQL Error Logs: Check the MySQL logs for any connection errors. Paths vary, but typically found in `/var/log/mysql/`.
Related reading
- Connect to remote MySQL db from docker container
- Connect to SQL Server database from a docker container
- Connect to wsl2 Ubuntu docker from Windows host
- connecting AWS SAM Local with dynamodb in docker
- Connect to mysql server without sudo
- connecting to cassandra from PHP
- Connecting to many kubernetes services from local machine
- Connecting to Postgresql in a docker container from outside

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.