Why Port Mapping and Virtual Hosts Don't Always Play Nice in Apache Docker
Running multiple PHP applications within a single Apache container is a common architectural pattern, often used to reduce infrastructure costs or simplify deployment pipelines. However, attempting to expose these applications on different ports via standard Apache virtual hosts frequently leads to the dreaded "Connection Refused" error, even when the container itself is running and accessible on port 80.
The core issue stems from how Docker handles network traffic and how the Apache server inside the container interprets incoming requests. In your specific scenario, you have mapped host port 80 to container port 80, and host port 8060 to container port 8060. While the Dockerfile correctly exposes these ports, the Apache configuration logic is fundamentally misaligned with how the reverse proxy or direct connection expects the traffic to land.
When you access http://192.168.99.100, your host machine's DNS resolver or /etc/hosts likely maps that IP to the container running on port 80. Apache receives the request on its internal port 80 and serves content from /var/www/html. This works because the mapping is direct: Host 80 -> Container 80.
However, when you attempt to access http://192.168.99.100:8060, the browser sends an HTTP request specifically targeting TCP port 8060 on your host. Docker successfully forwards this traffic into the container, landing it on internal port 8060. The problem arises because your Apache configuration defines a VirtualHost listening on *:60.
Apache does not automatically listen on every port unless explicitly configured to do so. By default, a standard Apache installation inside a container often only listens on port 80. When the request arrives at internal port 8060, the Apache process has no listener bound to that specific port address pair, resulting in the operating system returning a TCP RST (reset), which translates to "Connection Refused."
To resolve this without spawning a second Apache process, you must configure Apache to listen on both ports within the same instance. You can achieve this by modifying your VirtualHost blocks to include an Listen directive or, more commonly in containerized environments, by relying on port 80 and using rewrite rules or proxy directives if you need multiple sites on a single port.
However, since you require distinct ports for distinct root domains (one on 80, one on 8060), the cleanest approach is to ensure Apache listens on both. You can modify your configuration file inside the container to include:
Listen *:80
Listen *:8060
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:8060>
ServerName api.example.com
DocumentRoot /var/www/html/api
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
After making these changes, you must rebuild the container or copy the new configuration into the running instance using docker cp and restart Apache within the container (/usr/sbin/apache2ctl -k graceful). Note that in your Dockerfile, you are mounting a volume directly to /var/www/html. Ensure that the directory structure for the second application is correctly placed inside the container's DocumentRoot path relative to how the virtual host expects it.
If you intend to serve multiple distinct root domains on different ports without changing the port mapping strategy, this configuration ensures Apache accepts connections on both interfaces. If you prefer to keep the port mappings as they are but want to avoid listening on 8060 directly, you would need a reverse proxy like Nginx or Traefik sitting outside the container to route traffic based on headers or subdomains, though that introduces additional complexity and latency.
For production environments where strict isolation is required, consider running separate containers for each application using Docker Compose networking. This eliminates the need for complex multi-site configurations within a single web server process and simplifies health checks and logging. If you are building this stack for a Laravel project specifically, remember that while Apache handles the routing, managing multiple versions or environments often benefits from the separation of concerns provided by distinct containers rather than cramming them into one monolithic image.
