3

I've following coder-compose configuration:

version: '2'
services:
    nginx:
        build: ./nginx
        links:
            - tomcat1:tomcat1
            - tomcat2:tomcat2
            - tomcat3:tomcat3
        ports:
            - "80:80"

    tomcat1:
        build: ./tomcat
        ports:
            - "8080"

    tomcat2:
        build: ./tomcat
        ports:
            - "8080"

    tomcat3:
        build: ./tomcat
        ports:
            - "8080"

So, the question is, how to get access to the host network from the linked container(s):tomcat1, tomcat2, tomcat3. Here is the diagram:

enter image description here

Update Seems, my diagram doesn't help much. Nginx is a load balancer, Tomcat 1-3 are application nodes. Deployed web. app needs to get access to internet resource.

Eugene
  • 520
  • 3
  • 8

1 Answers1

0

Internet access is by default active on all containers (in bridge mode). All you need to check is if the http(s)_proxy variables are set if you are behind a proxy.

If your question if how to access docker host from a container (and not the reverse: access a container from the local docker host), then you would need to inspect the routing table of a container: see "From inside of a Docker container, how do I connect to the localhost of the machine?"

export DOCKER_HOST_IP=$(route -n | awk '/UG[ \t]/{print $2}')

There is a recent (June 2016) effort to add a adding a dockerhost as an entry in /etc/hosts of all running containers: issue 23177.
Update March 2020: this issue has been closed, and redirect to PR 40007: "Support host.docker.internal in dockerd on Linux"

This PR allows containers to connect to Linux hosts by appending a special string "host-gateway" to --add-host e.g. "--add-host=host.docker.internal:host-gateway" which adds host.docker.internal DNS entry in /etc/hosts and maps it to host-gateway-ip

This PR also add a daemon flag call host-gateway-ip which defaults to the default bridge IP

Docker Desktop will need to set this field to the Host Proxy IP so DNS requests for host.docker.internal can be routed to VPNkit

This will be in Docker for Linux (and Docker Desktop, which runs the Linux daemon, although inside a lightweight VM).

Difference between this and the current implementation on Docker Desktop is that;

  • the current Docker Desktop implementation is in a part of the code-base that's proprietary (i.e., part of how Docker Desktop is configured internally) this code could be used by the Docker Desktop team in future as well (to be discussed)
  • this PR does not set up the "magic" host.docker.internal automatically on every container, but it can be used to run a container that needs this host by adding docker run --add-host host.docker.internal:host-gateway
  • (to be discussed); setting that "magic" domain automatically on containers that are started could be implemented by adding an option for this in the ~/.docker/config.json CLI configuration file.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250