Network accessible IP for each docker container - docker

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.

Related

How docker network works when setting container IP address different from the docker assigned one

Background
I'm currently running OpenWrt inside a docker container. I created a macvlan network with subnet 172.19.0.0/16, and the OpenWrt container which connects to the macvlan network.
The docker automatically assigned one Ip address 172.19.0.2 when creating the openwrt container with Mac address 02:42:ac:13:00:02 or mac1 for short. When I login the openwrt container, and run ip addr, I get the following output, where it has IP address 192.168.50.123 bound to mac1.
Problem
The inconsistency of IP addresses makes me extremely confused, because normally I'd access the container with the docker assigned IP. In this case, the assigned IP 172.19.0.2 is un-ping-able. I can access the container through 192.168.50.123 which is a completely different network from the macvlan network this container connects to. I also edited the openwrt ip address through in /etc/config/network and no matter what ip address I choose, the I can only connect to the container through that IP address instead of the one docker assigned.
My initial thought on this is that, macvlan is all about l2 layer and Mac address, thus IP addresses don't play roles, if so why specify --subnet in the first place when creating a macvlan network as docker doc says. I'm new to docker, and don't have much experience in networking, hope any of you can help me explain this,please

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

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.

Docker containerization. accessing container from other machine using container name instead of host IP

my client and server hosted in a Linux host using docker as container. From other machine of same network able to connect using host IP address but I am not able to connect using container name or container IP address. I want to call a API of my server side application using container name or IP not using host machine IP
You cannot.
The (Docker) container runtime(s) are entirely distinct; there's no shared state that would permit the container on one host to be able to enumerate the container names on another host.
The easiest way for you to connect a client container on one host with a server container on another host is publish the server containers' ports to the host and to leverage the (shared) networking between the client and server and use either the server host's name or IP address to access it from the client.

If docker container ip and external network ip is same, then which one will get respond if will do telnet? any idea

If docker container IP and external network IP is the same, then which one will get respond if it will do telnet?
I know below configuration is the worst configuration, but I want to know the behaviour.
Give you one example
My application is running in localhost which is talking to the database inside the docker container.
Custom IP we provided - (IP : 10.0.0.1, PORT :5432)
Another database running outside the container, let say both container (IP and port) and host (IP and port) are the same.
HOST IP : 10.0.0.1, HOST PORT :5432
Which one will connect by application host/container-database or both the database?
or
If will do the telnet 10.0.0.1 5432? which one will respond and why?
Explain in Diagram
I don't think that's possible, even if you have the same IP (somehow) for the container and the host, you won't be able to map the container port 5432 to the host port 5432, because there's already an application (host dB) running on that port.
Consider a scenario where you are using the host network for the container as well, probably by using the --network host. This way your container IP will be the same as the host IP. The container will be using the 5432 port of the host to run the dB. Now, if you try to start the dB on the host using the same port, you should get an error that port is already being used.

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.

Resources