When your dev container is running but not accessible via the public IP of the host machine, it usually means there is a networking or configuration issue. Here’s a step-by-step guide to debug and fix this:
1. Check Container Port Mapping
Make sure the container ports are correctly mapped to the host ports.
-
If you are using Docker CLI, check with:
docker ps
Look for the PORTS column, it should show something like:
0.0.0.0:8080->80/tcp
This means port 80 inside the container is mapped to port 8080 on the host.
-
If you are using a devcontainer.json or Docker Compose, verify the port mappings are correctly set, e.g.:
"forwardPorts": [8080]
or in docker-compose.yml:
ports:
- "8080:80"
2. Check if the Service is Listening on the Correct Interface
Inside the container, the service should listen on 0.0.0.0 (all interfaces), not just localhost or 127.0.0.1.
-
Exec into the container:
docker exec -it <container_id> /bin/sh
-
Check listening ports:
netstat -tuln
-
If the service listens only on 127.0.0.1, it won’t be accessible from outside the container.
3. Check Host Firewall and Security Groups
-
On the host machine, check if firewall rules are blocking the port:
-
For ufw (Ubuntu):
sudo ufw status
Allow the port if needed:
sudo ufw allow 8080/tcp
-
For firewalld (CentOS/RHEL):
sudo firewall-cmd --list-all
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
-
If your host is in a cloud environment (AWS, Azure, GCP), check the security group or network firewall rules to allow inbound traffic on the port.
4. Check Host Network Binding
-
Sometimes Docker binds ports only to localhost (127.0.0.1) by default, making it inaccessible externally.
-
Run:
sudo netstat -tuln | grep 8080
You should see something like:
0.0.0.0:8080
If it shows 127.0.0.1:8080, Docker is only listening on localhost.
-
To fix this, explicitly bind to all interfaces in your Docker run command or compose file:
docker run -p 0.0.0.0:8080:80 ...
5. Test Connectivity Locally and Remotely
-
From the host machine, test:
curl http://localhost:8080
-
From another machine on the same network, test:
curl http://<host-public-ip>:8080
-
If local works but remote doesn’t, it’s likely a firewall or network issue.
6. Check Dev Container Configuration
If you are using VS Code dev containers, check your devcontainer.json:
-
Make sure forwardPorts is set correctly.
-
Check if runArgs or overrideCommand is affecting port exposure.
-
Example snippet:
{
"name": "My Dev Container",
"image": "my-image",
"forwardPorts": [8080]
}
Summary Checklist
| Step | Command / Action |
|---|
| Check container port mapping | docker ps |
| Check service listening inside | docker exec -it <container> netstat -tuln |
| Check host firewall | sudo ufw status or firewall-cmd --list-all |
| Check Docker port binding | `netstat -tuln |
| Test connectivity locally | curl http://localhost:<port> |
| Test connectivity remotely | curl http://<host-ip>:<port> |
| Verify devcontainer.json | Check forwardPorts and runArgs |
If you provide more details (e.g., your devcontainer.json, Docker run command, or error messages), I can help you debug further!