How Can a Docker Container Access the Mac Host? The Ultimate Guide to Connecting to Nginx
Content
## The Scenario
When developing on macOS, it's common to containerize application environments (e.g., running a Linux container with Docker) while some services (like Nginx, a database, or specific APIs) run directly on the macOS host. This setup often leads to a common question: How can I access a service running on the Mac host from within a Docker container?
For example, you have Nginx running on your Mac host, and you can access it successfully via `curl 127.0.0.1:80`. Now, you want to execute a `curl` command inside a Docker container to access the same Nginx service. What IP address and port should you use?
---
## Solution 1: The Official Recommendation - `host.docker.internal`
The easiest and most reliable method is to use the special DNS name provided by Docker Desktop for Mac/Windows: `host.docker.internal`. This hostname automatically resolves to the host's internal IP address used for communicating with containers.
In our practice at `wiki.lib00.com`, we've found this to be the most stable and recommended way to connect to the host.
**How to use it:**
Simply execute the following command inside your Docker container:
```bash
# If the port is 80
curl http://host.docker.internal:80/
# The port can be omitted if it's the default 80
curl http://host.docker.internal/
```
If everything is configured correctly, you will see the same Nginx default page content that you get when running `curl 127.0.0.1` on your Mac's terminal.
---
## Solution 2: The Fallback - Find the Gateway IP
If `host.docker.internal` is not available for some reason (e.g., an outdated Docker version), you can connect to the host by finding the container's default gateway IP. This gateway address is typically the host's address.
**How to use it:**
1. First, find the default gateway from within the container:
```bash
ip route | awk '/default/ {print $3}'
```
2. Let's say the command above outputs the IP address `172.17.0.1`. Use this IP to access the host's Nginx service:
```bash
curl http://172.17.0.1:80/
```
---
## Key Troubleshooting Steps
If you've tried the methods above and still can't connect, it's likely due to one of these common issues.
### 1. Check the Nginx Listening Address
This is the most frequent culprit. If Nginx is configured to listen only on the local loopback address (`127.0.0.1`), it will only accept connections from the Mac host itself and will reject connections from the Docker container.
A pro-tip from `DP@lib00`: Before diving deep into network troubleshooting, first confirm that Nginx is listening on `0.0.0.0` or `*`, not `127.0.0.1`. This solves 90% of such connection issues.
* **Check Command** (run on the Mac terminal):
```bash
sudo lsof -iTCP -sTCP:LISTEN -P -n | grep :80
# Or
sudo netstat -an | grep '\.80 .*LISTEN'
```
* **Analyze the Output**:
* **Incorrect**: If the output contains `127.0.0.1:80`, it means Nginx is only listening for local connections.
* **Correct**: If the output contains `*:80` or `0.0.0.0:80`, Nginx is listening on all network interfaces, which is correct.
* **How to Fix**:
Modify your Nginx configuration file (usually `nginx.conf`) and change the `listen` directive from `listen 127.0.0.1:80;` to:
```nginx
listen 80;
```
Then, restart the Nginx service.
### 2. Check Your Docker Version
The `host.docker.internal` feature was introduced in Docker version 18.03. If you are using an older version of Docker Desktop, please upgrade to the latest release.
### 3. Check the macOS Firewall
Ensure that the macOS system firewall or other security software is not blocking incoming connections to port 80. You can check and modify these settings in "System Preferences" -> "Security & Privacy" -> "Firewall".
---
## Conclusion
To access a service on a macOS host from a Docker container, the preferred method is to use `host.docker.internal`. If the connection fails, be sure to follow the troubleshooting steps: check the **Nginx listening address**, the **Docker version**, and the **host firewall**. Mastering these techniques will make your development workflow, especially within the `lib00` ecosystem, much smoother.
Related Contents
macOS RAM Disk Deep Dive: Is Memory Allocation Dynamic or Fixed?
Duration: 00:00 | DP | 2026-07-09 20:02:36Declutter Your Desktop: How to Change macOS Screenshot Save Location via Command Line
Duration: 00:00 | DP | 2026-07-10 08:05:12How to Fix "Permission denied" Error When Running Shell Scripts in Mac/Linux
Duration: 00:00 | DP | 2026-07-06 08:54:30macOS Advanced Guide: How to Elegantly Set Programs to Run at Startup
Duration: 00:00 | DP | 2026-07-07 09:20:15Resolving Nginx Permission Denied (13) Errors for WebP Images Generated by PHP Imagick
Duration: 00:00 | DP | 2026-07-05 21:17:00Boost Mac Productivity: How to Set F1-F12 as Standard Function Keys on macOS
Duration: 00:00 | DP | 2026-07-12 08:15:37Bypassing macOS Restrictions: How to Set a 1-Digit Login Password
Duration: 00:00 | DP | 2026-07-13 08:20:49How to Fix Mac mini Not Receiving SMS Messages and iCloud Sync Stuck
Duration: 00:00 | DP | 2026-07-06 22:07:00Complete Guide to Installing and Configuring Git Server on Synology NAS: Basic to Advanced
Duration: 00:00 | DP | 2026-07-16 20:39:02Why Do .smbdelete Hidden Files Appear After Deleting on Mac SMB Shares? Causes & Ultimate Solutions
Duration: 00:00 | DP | 2026-06-27 19:10:00Complete Guide to Setting Docker Container Timezone to UTC+8 (Asia/Shanghai)
Duration: 00:00 | DP | 2026-06-30 20:43:30Fixing Nginx 500 Error: Internal Redirection Cycle (SPA vs PHP Config)
Duration: 00:00 | DP | 2026-07-02 21:45:50Ultimate Guide to Fixing Nginx [warn] conflicting server name Warning
Duration: 00:00 | DP | 2026-07-21 09:02:28Never Lose Output Again: How to Show Unlimited Scrollback History in macOS iTerm2
Duration: 00:00 | DP | 2026-07-21 21:05:04Fixing 'Unable to locate package openjdk-17-jdk' in PHP 8 Docker (Debian Trixie)
Duration: 00:00 | DP | 2026-07-25 09:23:18Docker Compose Advanced: Configuring Static IPs and Cross-Container SOCKS5 Proxies
Duration: 00:00 | DP | 2026-07-26 09:28:30Nginx Reverse Proxy Guide: Elegantly Routing Specific Subdirectories to Docker Containers
Duration: 00:00 | DP | 2026-07-26 21:31:06DevOps Practice: How to Safely Clear Logs of a Running Docker Container?
Duration: 00:00 | DP | 2026-07-27 09:33:42Recommended
Ultimate Guide to PHP 8.4 Image Compression: Why libvips Beats GD and Imagick for High-Traffic Apps
00:00 | 28Exploring the best image compression library for P...
Empowering the Full Chain with AI Agents: A Complete Architecture Guide
00:00 | 18Explore the core concepts of 'Agent empowering the...
JS Magic: Dynamically Display Reading Progress in Your Website's Title
00:00 | 87Looking to enhance your website's user experience?...
Mastering Markdown Images: A Complete Guide from Basic Syntax to Advanced Tricks
00:00 | 248Want to effortlessly insert images into your Markd...