rootless-containers / slirp4netns

User-mode networking for unprivileged network namespaces
GNU General Public License v2.0
715 stars 82 forks source link

Access a service from the same ip:port in both a container and the host/lan (docker rootless) #332

Open y7a8c9 opened 7 months ago

y7a8c9 commented 7 months ago

Hello,

This is my first post on GitHub. I hope I've provided enough information, and that my issue is clearly articulated.

I'm working on a Docker rootless project and facing an issue with accessing a service from both another container and my host using the same IP and port. While I've managed to access it using my host's global (LAN) IP address, this isn't very convenient.

I'd prefer to access it through a local IP address.

Here's an example of my setup:

version: "3"

services:
  nginx:
    image: nginx
    ports:
      - 8080:80
    networks:
      cust_network:
        ipv4_address: 172.27.0.101

  cmdline:
    image: busybox
    networks:
      - cust_network

networks:
  cust_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.27.0.0/24

From my host, I can access the service using:

And from the 'cmdline' container in the same network, I can access it using:

What I want is to access it through an address such as 172.17.0.1:8080 or 172.23.0.1:8080.

Is this possible? If so, how can it be achieved? Alternatively, are there any tricks to accomplish something similar?

Thank you for your help!

AkihiroSuda commented 7 months ago

172.17.0.1:8080 (the default bridge network for Docker)

In the case of Rootless Docker, this is not really accessible from the host. But if you have a bridge for Rootful Docker, and let Rootless Docker bind on 0.0.0.0, the port is bound to the bridge's IP as well.

What I want is to access it through an address such as 172.17.0.1:8080 or 172.23.0.1:8080.

So, you can't use 172.17.0.1. 172.27.0.101 is the right IP to use. Not sure what is 172.23.0.1.