compose icon indicating copy to clipboard operation
compose copied to clipboard

Unable to bind port when service is connected to more than one network

Open hiro5id opened this issue 3 years ago • 2 comments

Experiencing issue that I get errors when binding a port to a container, but the error only happens when my container is connected to multiple networks.

This is the error that I get:

Creating network "test-network-error_network1" with driver "bridge"
Creating network "test-network-error_network2" with driver "macvlan"
Creating nginx ...
Creating nginx ... error

ERROR: for nginx  Cannot start service nginx: driver failed programming external connectivity on endpoint nginx (a2508bfa343dea80cebcb5ef60af037999e9eb62ac36f451188be8a026b8475f): Error starting userland proxy: listen tcp4 172.5.1.112:443: bind: cannot assign requested address

ERROR: for nginx  Cannot start service nginx: driver failed programming external connectivity on endpoint nginx (a2508bfa343dea80cebcb5ef60af037999e9eb62ac36f451188be8a026b8475f): Error starting userland proxy: listen tcp4 172.5.1.112:443: bind: cannot assign requested address
ERROR: Encountered errors while bringing up the project.

Here is an example compose file that can reproduce the issue:

Example:

version: '3.8'

services:
  nginx:
    image: nginx:1.19.5
    container_name: nginx
    ports:
      - '172.5.1.112:443:443'
    networks:
      network1:
        aliases:
          - nextcloud
      network2:
        ipv4_address: 172.5.1.12


networks:
  network1:
    driver: bridge
    attachable: true
  network2:
    driver: macvlan
    #enable_ipv6: true
    driver_opts:
      parent: eno1 # I got this value by running this on the host: ip address | grep --before=2 <host-ip-address>
    ipam:
      config:
      - subnet: 172.5.1.0/24

If I remove ports definition from the nginx service, everything works fine. But that also means that i'm unable to make nginx service accessible on the 172.5.0 network.

also if I leave the ports definition, but remove network1 from the nginx service, then it also works.

It just doesn't work with all of them together.

hiro5id avatar Feb 08 '22 19:02 hiro5id

Hallo, I'm experiencing the same exact problem. Here is my docker-compose.yaml:

version: "3.9"

services:
  mssql:
    image: mssql_server:2019-latest
    networks:
      pywoodnet:
        ipv4_address: 172.37.1.3

    # Port-binding is ignored when
    # a network with a custom ipv4_address
    # have been specified (in this case, Sql server
    # responds only through the port 1433. 
    # The port 1344 is LISTENING - tested with
    # netstat -ano | grep :1344 - UNTIL the docker-compose
    # is up and running)
    ports:
      - "1344:1433"

    volumes:
      - /DATA:/var/opt/mssql
      - /DATA/data:/var/opt/mssql/data
      - /DATA/backup:/var/opt/mssql/backup
      - /DATA/log:/var/opt/mssql/log
      - /DATA/secrets:/var/opt/mssql/secrets

    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=$mySuperSecretPassword123!

networks:
  pywoodnet:
    ipam:
      driver: default
      config:
      - subnet: 172.37.0.0/16

ricciarellif avatar Jul 14 '22 14:07 ricciarellif

@hiro5id are you able to setup an equivalent network connectivity using plain docker network create and docker run --network .. commands? I need to admit it's unclear to me if this is just feasible (and as such, what compose is doing wrong or should handle this)

ndeloof avatar May 11 '23 15:05 ndeloof

closing as unclear and obsolete. If you encounter a comparable issue, please create a fresh new one with details on your environment

ndeloof avatar Oct 23 '24 06:10 ndeloof