docker-mac-net-connect icon indicating copy to clipboard operation
docker-mac-net-connect copied to clipboard

Bind multiple docker ports to the one IP address

Open alexandertsukanov opened this issue 2 years ago • 5 comments

Hi, @gregnr ! I returned here with another question. Is it possible to bind multiple ports to one IP address to reach internal docker containers e.g. Docker GATEWAY? For example, I have mysql container which uses 3306 port for all incoming connections, when mysql image restarts it changes its IP address from one to another, and I need to reconfigure my DB UI client to reconnect to the new one and this is not comfortable to me. Do you by any chance know some workarounds in such case?

Thank you very much. Sincerely, Alex

alexandertsukanov avatar Feb 09 '22 09:02 alexandertsukanov

Hey @alexandertsukanov, here are some workarounds:

  1. If you don't care about connecting to the container's IP directly, you can just use regular port binding which binds the container's port to a port on your localhost, ie:

    $ docker run -d -p 8080:80 nginx
    $ curl -I localhost:8080
    ...
    

    If you use this approach, you don't need docker-mac-net-connect at all.

  2. Set a static IP on the container so that it doesn't change. Eg. docker-compose:

    version: "3.8"
    
    networks:
      my-net:
        name: my-net
        driver: bridge
        ipam:
          driver: default
          config:
            - subnet: 172.30.0.0/24
              gateway: 172.30.0.1
    
    services:
      nginx-example:
        image: nginx
        networks:
          my-net:
            ipv4_address: 172.30.0.2
    

    This should solve your problem, just note that in general static IP are not usually best practice. In a development environment you are probably fine, but one of the benefits of containers is to treat them as ephemeral.

  3. Some sort of DNS service that auto maps domains to container IPs (I haven't found a good solution that does this yet). I'm actually working on a solution for this right now. Essentially you will be able to do something like:

    $ docker run -d --name nginx-example nginx
    $ curl -I nginx-example.container.docker.internal
    ...
    

    Let me know if this is something you would be interested in.

    Currently it is implemented as a CoreDNS extension, so you would need to point your macOS host DNS to this service in order for it to work. Alternatively we could consider some sort of /etc/hosts file manipulation.

gregnr avatar Feb 09 '22 21:02 gregnr

@gregnr sorry I forgot to clarify -p option doesn't work for me. I need to access container directly by IP. (The reason is my app starts child docker image without -p forwarding). The 3th option looks really cool.

alexandertsukanov avatar Feb 11 '22 05:02 alexandertsukanov

Hey, @gregnr! Is any ETA on this feature? May I can do something to help? I see the project mostly has a code-base in Go, I am more proficient in Java, but can try to dive into GO.

alexandertsukanov avatar May 28 '22 05:05 alexandertsukanov

  1. Some sort of DNS service that auto maps domains to container IPs (I haven't found a good solution that does this yet). I'm actually working on a solution for this right now. Essentially you will be able to do something like:
    $ docker run -d --name nginx-example nginx
    $ curl -I nginx-example.container.docker.internal
    ...
    
    Let me know if this is something you would be interested in. Currently it is implemented as a CoreDNS extension, so you would need to point your macOS host DNS to this service in order for it to work. Alternatively we could consider some sort of /etc/hosts file manipulation.

I had a crack at this with https://github.com/Mahoney/docker-etc-hosts - there's probably loads of issues with it, grateful for your thoughts on the approach.

Mahoney avatar May 28 '22 09:05 Mahoney

Mine is broken (see https://github.com/Mahoney/docker-etc-hosts/issues/2), but there are other options which run a DNS proxy inside docker that will resolve container names to docker ip addresses. You then need to make that DNS proxy the host's DNS server:

https://github.com/aacebedo/dnsdock https://github.com/mageddo/dns-proxy-server

I think there may be others....

Mahoney avatar May 28 '22 13:05 Mahoney