You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

基于简易docker-compose配置的Docker网络连接问题求助

Fixing Docker PHP+Apache Access Issues on macOS

Hey there, I get it—switching from Linux to macOS can throw some curveballs with Docker, since the underlying network setup is totally different. Let's walk through why you're seeing this problem and how to fix it.

First, Understand the Key Difference Between Docker on Linux vs. macOS

On Linux, Docker runs directly on your host's network stack, so you can reach container IPs like 172.20.0.2 directly from your host. But on macOS, Docker runs inside a lightweight virtual machine (VM) managed by Docker Desktop. That container IP is only accessible within the VM, not from your Mac's main operating system. That's why pinging it or trying to access it directly fails—your Mac doesn't know how to route traffic to that internal VM IP.

Step-by-Step Troubleshooting & Fixes

1. Verify Your Port Mapping & Container Port

Looking at your docker-compose.yml, you've mapped host port 8080 to container port 8080:

version: '2'
services:
  php:
    build: apache-php
    ports:
      - "8080:8080"
      - "8443:8443"
    volumes:
      - ./apache-php/www:/var/www/html

But here's a common gotcha: default Apache listens on port 80, not 8080. Unless you explicitly changed the port in your Dockerfile (in the apache-php directory), your container's Apache is running on port 80, not 8080. That means your port mapping is pointing to the wrong container port.

Fix this by updating your ports section to map host port 8080 to container port 80:

ports:
  - "8080:80"
  - "8443:443"

2. Test Access Correctly

Once your port mapping is fixed, access the service using localhost:8080 (or 0.0.0.0:8080) on your Mac. Forget trying to use the container's internal IP—it won't work on macOS.

If localhost:8080 still doesn't respond:

  • Check if the port is occupied on your Mac:
    lsof -i :8080
    
    If another process is using 8080, switch to a different port (e.g., 8081:80) in your docker-compose.yml, then restart the container.

3. Confirm the Service is Running Inside the Container

Let's make sure Apache is actually up and listening inside the container:

  • First, get your container ID with docker ps
  • Exec into the container:
    docker exec -it <container-id> /bin/bash
    
  • Inside the container, test if Apache is responding:
    curl localhost:80  # Or localhost:8080 if you did change the port
    

If this returns your PHP page, the service is working—your issue is definitely with port mapping or host network routing.

4. Reset Docker Desktop Network (If All Else Fails)

Sometimes Docker Desktop's network stack gets into a weird state. Fix this by:

  1. Opening Docker Desktop
  2. Going to Settings > Network
  3. Clicking Reset to factory defaults
  4. Restarting Docker Desktop and re-running your docker-compose up command

What Not to Waste Time On

  • Mapping the container IP to /etc/hosts: As mentioned, that IP is only accessible inside the Docker VM, so this won't help on macOS.
  • Removing port configurations: Without port mapping, you have no way to reach the container from your Mac host.

内容的提问来源于stack exchange,提问作者Luke Marks

火山引擎 最新活动