Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind multiple docker ports to the one IP address #6

Open
alexandertsukanov opened this issue Feb 9, 2022 · 5 comments
Open

Bind multiple docker ports to the one IP address #6

alexandertsukanov opened this issue Feb 9, 2022 · 5 comments

Comments

@alexandertsukanov
Copy link

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
Copy link
Member

gregnr commented Feb 9, 2022

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
Copy link
Author

@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
Copy link
Author

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
Copy link

Mahoney commented May 28, 2022

  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
Copy link

Mahoney commented May 28, 2022

Mine is broken (see Mahoney/docker-etc-hosts#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....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants