r/docker • • 10d ago

Route homelab Docker through specific interface

I created my homelab with docker running on Debian that is a VM sitting on Proxmox 9. I have two interfaces connected to the Debian server ens18 (192.168.20.21) and ens19 (192.168.20.23). I want all but 1 docker container (via docker compose) to use ens18 to receive and send traffic on. I only want ens19 to receive traffic for one port, this port is already in use in other docker containers and can't be used there so needed its own interface. I created a bridge (bridge_21) and specified com.docker.network.host_ipv4=192.168.20.21, the other bridge (bridge_23) I didn't specify any options on. I have the bridge_21 specified for the ngnix proxy manager specified below but it still seems to be routing traffic out on bridge_23's ip address. How can I get this to only route data in and out on bridge_21?

#Bridges settings
docker network create \
  --driver bridge \
  -o com.docker.network.host_ipv4=192.168.20.21 \
  bridge_21

#Ngnix Proxy Manager Compose.
services:
  app:
    container_name: ngnixproxymanager
    image: 'jc21/nginx-proxy-manager:latest'
    restart: unless-stopped
    networks:
      - bridge_21
    ports:
      # These ports are in format <host-port>:<container-port>
      - '192.168.20.21:80:80' # Public HTTP Port
      - '192.168.20.21:443:443' # Public HTTPS Port
      - '192.168.20.21:81:81' # Admin Web Port
    volumes:
      - type: volume
        source: ngnixproxymanager_data
        target: /data
        volume:
          subpath: data
      - type: volume
        source: ngnixproxymanager_data
        target: /etc/letsencrypt
        volume:
          subpath: letsencrypt

volumes:
  ngnixproxymanager_data:
    external: true

networks:
  bridge_21:
    external: true

#Weather Station docker compose
services:
  cc0ac-weather:
    build:
      context: https://github.com/akienm/CC0AC-Weather.git
      dockerfile: setup/docker/Dockerfile
    container_name: acurite-weather
    image: cc0ac-weather
    networks:
      - default
      - bridge_23
    #network_mode: bridge
    ports:
      - '192.168.20.23:443:443' # the Access hub's uploads (HTTPS)
      - '192.168.20.21:12345:12345' # dashboard and setup page
      - '192.168.20.21:12346:12346' # console uploads
    volumes:
      - acurite_data:/data
volumes:
  acurite_data:
    external: true
networks:
  bridge_23:
    external: true
3 Upvotes

7 comments sorted by

4

u/JohnsTech_dev 9d ago

I think the real problem is both NICs being in the same /24, not docker. linux picks one route for 192.168.20.0/24 from the main table, so traffic for .23 can still leave via ens18, and both interfaces will answer ARP for each other's IP. host_ipv4 only sets the source IP docker SNATs to, it doesn't choose the interface (also check docker network inspect bridge_21 actually shows it, the option is fairly new).

If you want to keep two NICs you need policy routing, something like:

ip route add 192.168.20.0/24 dev ens19 src 192.168.20.23 table 23
ip route add default via 192.168.20.1 dev ens19 table 23
ip rule add from 192.168.20.23 table 23

plus net.ipv4.conf.all.arp_ignore=1 and arp_announce=2 in sysctl. gateway is a guess, use yours.

Easier route since it's a proxmox VM anyway: drop ens19, add .23 as a second address on ens18, and publish that one port as 192.168.20.23:PORT:PORT. same result, no routing tables to babysit.

1

u/NoDevelopment206 9d ago

One more thing to check. The weather container is also on the default network, so anything it starts itself (like calls out to the internet) will leave from whatever the host's main route picks, not bridge_23. host_ipv4 only changes the SNAT source address, so it won't pin the interface. I'd try the policy routing above first and then confirm with tcpdump on ens19 while the container sends something.

1

u/TemporaryPiccolo1095 9d ago

tes conteneur sont routé via traefik ou autre choses ? personnellement juste pour une app je ferais un nat

1

u/Only-Stable3973 9d ago

docker network create proxy_net, using container level not network level.

# Nginx Proxy Manager

services:
  app:
    container_name: ngnixproxymanager
    image: 'jc21/nginx-proxy-manager:latest'
    restart: unless-stopped
    networks:
      - proxy_net
    ports:
      - '192.168.20.21:80:80'
      - '192.168.20.21:443:443'
      - '192.168.20.21:81:81'
    volumes:
      - type: volume
        source: ngnixproxymanager_data
        target: /data
        volume:
          subpath: data
      - type: volume
        source: ngnixproxymanager_data
        target: /etc/letsencrypt
        volume:
          subpath: letsencrypt

volumes:
  ngnixproxymanager_data:
    external: true

networks:
  proxy_net:
    external: true

# Weather Station Compose

services:
  cc0ac-weather:
    build:
      context: https://github.com/akienm/CC0AC-Weather.git
      dockerfile: setup/docker/Dockerfile
    container_name: acurite-weather
    image: cc0ac-weather
    restart: unless-stopped
    networks:
      - proxy_net
    ports:
      - '192.168.20.23:443:443'     # Access hub uploads (HTTPS)
      - '192.168.20.21:12345:12345' # Dashboard and setup page
      - '192.168.20.21:12346:12346' # Console uploads
    volumes:
      - acurite_data:/data

volumes:
  acurite_data:
    external: true

networks:
  proxy_net:
    external: true

1

u/lxtakc 8d ago

Port bindings only control inbound. Outbound uses the host's routing, so with both NICs in one subnet you need an ip rule sending the bridge_21 subnet out ens18.