Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
From inside of a Docker container, how do I connect to the localhost of the machine
When running applications in Docker containers, you often need to connect from the container to services running on the host machine's localhost. For example, if you have an Nginx container that needs to access a MySQL database running on your host's localhost (127.0.0.1), the default container networking won't allow this connection since the container has its own isolated network namespace.
This article explains different methods to establish connectivity between Docker containers and host services, with solutions varying by operating system and networking requirements.
Method 1 − Host Network Mode (Linux)
The simplest solution on Linux is using the --network="host" option with the Docker run command. This makes the container share the host's network stack directly.
docker run -it --network=host ubuntu:latest
With host networking, the container's localhost (127.0.0.1) points directly to the host machine. You can verify this by comparing IP addresses:
# On host machine ip addr show eth0 # Inside container with host networking docker run -it --network=host ubuntu:latest ip addr show eth0
Important: Any ports exposed by the container are directly accessible on the host without port mapping. This bypasses Docker's port isolation.
Method 2 − Special Hostnames (Windows/Mac)
Docker Desktop for Windows and Mac provides special hostnames to access the host:
Docker Desktop (Current Versions)
# Use this hostname in your container host.docker.internal
Example MySQL connection from container:
mysql -uroot -h host.docker.internal -p
Docker Desktop (Legacy Mac)
# Older Mac versions docker.for.mac.localhost # or docker.for.mac.host.internal
Method 3 − Bridge Network with Host IP
For bridge networking, configure your host service to listen on the Docker bridge network IP address. Find the Docker bridge IP:
ip route | grep docker0 # Typically shows 172.17.0.1
Configure your service (e.g., MySQL) to bind to this address by modifying the configuration file:
# MySQL config example bind-address = 172.17.0.1
Then connect from the container using the host's Docker bridge IP:
mysql -uroot -h 172.17.0.1 -p
Method 4 − Custom Host Entry (Linux)
On Linux, you can add a custom host entry to make host.docker.internal work like on Windows/Mac:
docker run --add-host host.docker.internal:host-gateway my-container
This allows using the same connection strings across different platforms.
Comparison
| Method | Platform | Pros | Cons |
|---|---|---|---|
| Host Network | Linux | Simple, direct access | No network isolation |
| host.docker.internal | Windows/Mac | Cross-platform, clean | Docker Desktop only |
| Bridge IP | All platforms | Maintains isolation | Requires service reconfiguration |
| Custom host entry | Linux | Consistent syntax | Manual configuration needed |
Conclusion
Connecting from Docker containers to host services depends on your platform and networking requirements. Use --network=host on Linux for simplicity, host.docker.internal on Docker Desktop, or configure bridge networking for production environments. Choose the method that best balances ease of use with network security for your specific use case.
