From inside a Docker container, how do I connect to an IP address on the host network? - docker

I am using Docker Windows with WSL2.
From some answers I understand that connecting to an IP address exposed to the host network can be done from within a Docker container without any particular setup, but I cannot do it. Note that I do not want to access localhost, but a specific IP address (and port) of another device exposed to the host network.
I read a couple reasons on what could be the issue:
Missing firewall exception to allow communication between the external IP and the IP of the container.
I may have to use network_mode: "host", which however only seems to work in Linux
Many thanks.

Related

Can two docker both set host network mode?

I know docker host network mode, which will let docker share the same network with host machine. It will not need NAT and you can visit the docker by the host ip adress.
My question is if start two docker both with host network mode, what will happened? I found that their IP addresses are the same, will their networks conflict?
Setting host networking generally disables Docker networking. It's almost never necessary, unless you have a program that can't be configured to listen on a fixed port or you have a program that listens on thousands of ports.
Since it disables Docker networking, containers that use host networking have direct access to the host network devices. If they set up network listeners, these share a port space with other host-network containers and non-container processes. You cannot remap ports, limit a port to being visible only to specific interfaces, or directly communicate with other containers if you have the host network. Containers don't have their own private IP address or port space in host-network mode.
Nothing stops you from starting multiple containers with host networking (in the same way you can start multiple non-container servers on the host directly), but if they try to listen to the same port on the same (host) interface(s), one of them will fail and you'll have to do application-specific reconfiguration to fix it.

Access Docker container via DNS name from corporate LAN

I'm looking for a way to access containers that are running on server in our company lan by domain names. By far I only managed to access them by IPs
So the setup is. Docker (for windows) is running on server srv1.ourdomain.com (Windows Server 2019), network for container is configured with l2bridge driver, container's dns name, as specifiedn in run command, is cont1. It is accessible by dns name on the docker host (srv1) and by IP from my machine.
What can I do to access the container by dns name cont1.ourdomain.com from my local machine located in the same lan?
I tried to use proxy (traefik) but it cant rewrite urls in the content, so web applications running inside the container are failing. Bacause of this I can't host multiple web application behind that proxy.
I know that it is possible to map container's port to host port and then it will be accessible from lan through the host name and host port, but applications I'm running are requiring many ports to be mapped (like 8 ports for each container) and with those containers being short-lived developer's environments it will be a hell to find a port pool when running new container.
So again if I can access container and its' ports by IP, is there a way to do the same by DNS name?
UPD1. Container host is a virtual server running on vmware. I tried to follow those recommendations and configure promiscuous mode. Thise doesn't help with dns though.
UPD2. I tried transparent network as well. For some reason DHCP can never assign propper IP and container ends up with autoconfigured ip from 168.x.x.x subnet.
You could create a transparent network and make the container discoverable on the network just like host. However, using host ports is what's recommended.
Did you try PathStrip or PathPrefixStrip with Traefik? That should let you rewrite the URLs for the backend.

Docker containers that are not running on localhost

For regular docker containers (say the hello world example), after you run it, it is accessible thought localhost, where you can make a request it through your browser.
But sometimes it seems to access a container you need a special IP address. I'm wondering what's this behavior of docker container networking called and where is it defined/documented.
Let's say my local ip address is 10.0.75.1 (got from Network properties in Windows settings named, vEthernet (DockerNAT)). But in order to connect to a container running I had to use ip address 10.0.75.2. Why is this?
If try to inspect existing docker networks using docker network [cmd], the containers seem to be on different subnets, for example '172.17.0.0/16'

How to access docker container from mac machine using ip addr or a domain name

I am using Docker desktop, I have a couple of docker containers running using docker-compose and port forwarding. I can access the containers from my mac using localhost. On the second container, I am exposing on different ports. I can see ip addresses are associated to both containers by using docker inspect, but I cannot access using the ip address.
I would like access the container from my local mac by
dns domain
ip address
Any help appreciated.
Thanks
You cannot directly connect to the container-private IP addresses on MacOS. You also can't connect to them using a VM-based Docker implementation like Docker Toolbox or Kubernetes' minikube, or from a different host. Looking up and using these IP addresses, or trying to manually set them, usually isn't a best practice.
Instead you can use the docker run -p option to publish a port from your container to the host. Programs running directly on the host can access the container using localhost as a host name and the published port number. This works on all platforms; on VM-based solutions use the VM's IP address instead of localhost; from a different host, use the Docker host's DNS name or IP address.

Network accessible IP for each docker container

I would like to deploy multiple applications via docker. Some of them are using the same port.
An alternative port mapping (Port 80->5080) is not an option, so my way to handle the problem is a network bridge which should allow me to assign an ip address from my internal network to each container.
The answer from this post does not work for me
Assign LAN IP address to Docker container different from host's IP address
i am able to assign an ip to the docker container, but it also gets the host ip address so i can not map ports.

Resources