Keywords: Docker | MySQL | Container_Connectivity | docker-compose | Network_Configuration
Abstract: This paper comprehensively examines three core methods for connecting Docker containerized applications to locally hosted MySQL databases: utilizing the host.docker.internal special domain, connecting through Docker network gateway IP addresses, and employing host network mode. The article provides detailed analysis of each approach's implementation principles, configuration procedures, applicable scenarios, and potential limitations, accompanied by complete docker-compose.yml configuration examples and network debugging commands. Addressing production deployment requirements, special emphasis is placed on network stability, security configurations, and cross-platform compatibility considerations, offering comprehensive technical guidance for developers maintaining local database services during containerization migration.
Technical Context and Problem Analysis
During modern application containerization deployment, development teams frequently encounter a typical scenario: using Docker Compose to manage complete application stacks (including application containers and database containers) during development and testing phases, but when migrating to production environments, they wish to retain existing local MySQL database services rather than using containerized database instances. This requirement stems from multiple factors: production databases typically have optimized configurations, contain important historical data, or need integration with existing monitoring and backup systems.
Approach 1: Using the host.docker.internal Special Domain
This is the most straightforward solution with good cross-platform compatibility. host.docker.internal is a special DNS name provided by Docker that, when resolved inside containers, points to the host machine (the machine running the Docker engine). This mechanism enables applications within containers to access services running on the host as if they were external services.
Configuration example:
version: '3'
services:
web-app:
build:
context: .
dockerfile: web-app/Dockerfile
ports:
- 8080:8080
environment:
- DB_HOST=host.docker.internal
- DB_PORT=3306
# Additional configuration required for Linux systems
extra_hosts:
- "host.docker.internal:host-gateway"
Key implementation details:
- In Docker Desktop environments for macOS and Windows,
host.docker.internalis available by default - In Linux environments, explicit addition is required via the
--add-host host.docker.internal:host-gatewayparameter or docker-compose'sextra_hostsconfiguration - Application configurations should set database connection addresses to
host.docker.internal:3306
Approach 2: Connecting Through Docker Network Gateway IP
This method is based on Docker network architecture principles. When services are created using docker-compose, a separate bridge network is created by default, where the host machine has a gateway IP address within this network, allowing containers to access host services through this IP.
Steps to determine the gateway IP:
- After starting the container, obtain the container ID or name
- Execute the command:
docker inspect <container-id> | grep Gateway - Extract the IPv4 gateway address from the output, typically
172.18.0.1or similar format
Configuration example:
version: '3'
services:
web-app:
build:
context: .
dockerfile: web-app/Dockerfile
ports:
- 8080:8080
environment:
- DB_HOST=172.18.0.1
- DB_PORT=3306
Important considerations:
- The gateway IP remains stable throughout the docker-compose network lifecycle
- Executing
docker-compose downdeletes the network, and the gateway IP may change upon recreation - Ensure MySQL configuration has
bind-addressset to0.0.0.0to listen on all network interfaces
Approach 3: Using Host Network Mode
This is the most comprehensive solution, allowing containers to share the host machine's network namespace, enabling containers to directly use the host's network stack. In this mode, localhost inside the container directly points to the host.
Configuration example:
version: '3'
services:
web-app:
build:
context: .
dockerfile: web-app/Dockerfile
network_mode: "host"
# Note: port mappings may be unnecessary or behave differently in host mode
environment:
- DB_HOST=localhost
- DB_PORT=3306
Technical characteristics:
- Container applications can directly connect to MySQL using
localhost:3306 - Optimal network performance with no additional NAT overhead
- Port conflict risk: container applications and host services share the same port space
- Security considerations: complete loss of container network isolation
MySQL Server Configuration Requirements
Regardless of the connection approach chosen, proper MySQL server configuration is essential:
# In my.cnf or my.ini configuration files
[mysqld]
bind-address = 0.0.0.0
# Or comment out the bind-address line
Security recommendations:
- In production environments with public IP addresses, restrict access to specific IP ranges
- Use firewall rules to limit access to port 3306
- Consider using SSH tunnels or VPN for encrypted connections
Solution Comparison and Selection Guidelines
<table border="1"> <tr><th>Approach</th><th>Advantages</th><th>Disadvantages</th><th>Applicable Scenarios</th></tr> <tr><td>host.docker.internal</td><td>Good cross-platform compatibility, simple configuration</td><td>Additional configuration required for Linux</td><td>Development and testing environments, multi-platform deployment</td></tr> <tr><td>Gateway IP Connection</td><td>Clear network structure, stable performance</td><td>IP may change, requires dynamic acquisition</td><td>Production environments, fixed network topology</td></tr> <tr><td>Host Network Mode</td><td>Optimal performance, simplest configuration</td><td>Lower security, complex port management</td><td>Local development, performance-sensitive applications</td></tr>Production Environment Deployment Considerations
In actual production deployments, beyond technical implementation, the following factors must be considered:
- Connection Stability: Ensure network configurations remain effective after container restarts
- Failover Strategies: Develop handling strategies for database connection failures
- Monitoring Integration: Ensure container applications and local database monitoring systems can work collaboratively
- Backup Strategies: Coordinate backup time windows between containerized applications and local databases
By appropriately selecting and configuring the aforementioned approaches, development teams can enjoy the convenience of containerized deployment while fully utilizing existing database infrastructure, achieving smooth production environment migration.