chipmk / docker-mac-net-connect

Connect directly to Docker-for-Mac containers via IP address 🐳 💻
MIT License
402 stars 41 forks source link

Bind multiple docker ports to the one IP address #6

Open alexandertsukanov opened 2 years ago

alexandertsukanov commented 2 years ago

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

gregnr commented 2 years ago

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.

alexandertsukanov commented 2 years ago

@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 commented 2 years ago

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.

Mahoney commented 2 years ago
  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 commented 2 years ago

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....