How can I access Docker container services using an IP address on the host machine?

  • os: Win11 Home Edition
  • image: nginx:latest
  • host ip address: 192.168.2.3

When I execute the command docker run -d -p 8080:80 nginx:latest, I am able to access the website through http://localhost:8080 and http://127.0.0.1:8080, but not through 192.168.2.3:8080:80. Additionally, when I run docker run -d -p 192.168.2.3:8080:80 nginx:latest, I still cannot access it through 192.168.2.3:8080:80 and 192.168.1.2:8080

Than it is a firewall issue and you need to allow incomming requests to that ip address on that port on Windows.

Maybe Configure firewall rules with group policy | Microsoft Learn

EDIT

Do you try from another machine or from the same machine?

Running the front-end project on this machine allows access to 192.168.2.3 , and the firewall is also disabled. Previously, when running Docker on a Linux server, I did not encounter this issue, and I really have no way to resolve it.

What is the front-end project? How do you run it? Is that running in a Docker container?

In the development environment, my project runs at 192.168.2.3:8888 (which my colleagues can access). After compiling, I deploy it using Docker, mapping the port to 8080. I want my colleagues to be able to access the project at 192.168.2.3:8080. However, even though I’ve used the commands docker run -d -p 8080:80 nginx:latest and docker run -d -p 192.168.2.3:8080:80 nginx:latest to deploy the container, my colleagues cannot access my project at 192.168.2.3:8080, even with the firewall turned off, while I can access it through localhost:8080 and 127.0.0.1:8080. So, I’m wondering, does the Windows system not support local network access? Because I had no issues deploying on a Linux system, or are there any additional considerations I should be aware of when using Windows?

Set localhostForwarding=true in the . wslconfig.

It really depends on whether you use Docker Desktop or installed Docker-CE in a WSL2 distribution directly.

Docker Desktop: If you publish a port, the windows host can access it via localhost or the host ip. Docker
Docker-CE in WSL2: if you publish a port, the windows can access it via localhost, if localhostForwarding=true is configured in .wslconfig or missing, as its the default behavior.

Recent versions of WSL2 allows setting the networking mode to mirrored using networkingMode=mirrored in .wslconfig, which will bind any port from inside the wsl2 distribution to the host ip as well. This setting has no effect with Docker Desktop.