Docker: Multiple networks, one container and ports - docker

I have set up multiple containers in one docker compose file.
I want all containers to share a network. One container needs network_mode: host.
As I understand, adding network_mode: host to one container prevents this container from accessing the other containers which does not have network mode host.
So I tried to define two networks, one with driver bridge (network A) (all containers should be connected to this network) and one with driver macvlan (network B) (only the container that requires network_mode: host should be connected to this network).
In general, everything works as expected. The container which is connected to network B got an ip addres in my local network and can communicate with the other containers connected to network A.
The problem is, that the ports configuration seems to not work. I want to expose a port of the container connected to network A and B to all connected host networks. This doesn't work when I connect the container to network A and B.
Thank you!

Related

Docker-Compose - Give a container an IP on the hosts network and also allow container to container connections and host net connections

I have several containers running in a stack controlled by docker-compose.
Amongst these is one running node-red.
The node-red container needs to be able to search the network for a device that is on the hosts network (192.168.1.0/24).
With the default networking the containers can ping and connect to the host network when they know the IP, unfortunately the node-red implementation only scans the local networks, and it only has the docker bridge network (172...) so it only searches there.
I've tried multiple combinations of the docker network setups to try and get the containers to see the 192 network as local. When I am able to get a 192 network address, the containers can not ping other hosts on the external (to the host machine) network.
TLDR: Docker container needs to be able to see other docker containers and also have an IP address on the hosts network and be able to connect to other services on the hosts network.
Okay, I found a solution that works for me:
In the docker-compose.yml
app:
image: youimage
network_mode: "host"
adding the network_mode: "host" was what I needed

How to access docker container in a custom network from another docker container running with host network

My program is consisting of a network of ROS1 and ROS2 nodes, which are software that work with a publish/subscribe way of communication.
Assume there is 4 nodes running inside a custom network: onboard_network.
Those 4 nodes (ROS1) can only communicate together, therefore we have a bridge node (ROS1 & ROS2) that needs to be sitting on the edge on both onboard_network and host network. The reason why we need the host network is because the host is inside a VPN (Zerotier). Inside the VPN we have also our server (ROS2).
We also need the bride node to work with host network because ROS2 work with some multicast stuff that works only on host mode.
So basically, I want a docker compose file running 4 containers inside an onboard_network & a container running inside the host network. The last container needs to be seen from the containers in the onboard_network and being able to see them too. How could I do it ? Is it even possible ?
If you're running a container on the host network, its network setup is identical to a non-container process running on the host.
A container can't be set to use both host networking and Docker networking.
That means, for your network_mode: host container, it can call other containers using localhost as a hostname and their published ports: (because its network is the host's network). For your bridge-network containers, they can call the host-network container using the special hostname host.docker.internal on MacOS or Windows hosts, or on Linux they need to find some reachable IP address (this is discussed further in From inside of a Docker container, how do I connect to the localhost of the machine?.

Docker: how to access the hosts network with a docker container?

How can I access the hosts network with a docker container? Can I put a container in the hosts network with another IP from the hosts network?
Current situation:
Docker container (default bridge network): 172.17.0.2/16
Host (server): 10.0.0.2/24
Question:
Can I put the docker container on the 10.0.0.0/24 network as a secondary address?
(or) Can I access the hosts network on the container and vica versa?
Reason:
I want to access the hosts network from my container (for example: monitoring server).
I want the container to act as a server accessible from the hosts network on all ports.
Note:
I run several docker containers so a few ports are already forwarded from the host and these should remain so. So an all-port-forward from the hosts IP isn't really a solution here.
Setup on host:
basic docker system
Centos 7
Macvlan networks may be the solution you are looking for.
You could assign multiple MAC/IP addresses on virtual NICs over single physical NIC.
There are some prerequisites for using Macvlan.

Can we have two or more container running on docker at the same time

I have not done any practical with the docker and container, But as per my knowledge.
As per the documents available online I did not get the details about the running two or more containers at the same time.
Docker allows container to map port address of container to the host machine.
Now, the question is can we run multiple container at the same time on docker? if yes then if two containers are mapped to same port number then how does the port is handled in this case?
Also out of curiosity, can two containers on docker communicate with each other?
Yes you can run multiple containers on a single host; docker is designed for exactly that.
You cannot map two containers of different images to the same port number; you get an error response if you try. However, if your containers run the same image (e.g.2 instances of a webapp) you could run them as a service, and have them exposed on the same port. Docker will load-balance the requests. You can read more about services here or follow the Get Started (Part 3, services) here
Yes, the containers on a single host can communicate with each other, by container name. For example if you have one container running MongoDB called mongo, and another one running Node.js called webserver, the webserver container can connect to the database by using the name mongo e.g. db.Connect("mongodb://mongo:27017/testdb").
We can run more one than one Docker at a time in a host but yes we will hit the limitation of binding the same port to the docker; so to resolve this we need to bind different port in the host to docker that is if you are running mongo-db then its default port is 27017 so we can run two mongo-db as -p 27017:27017 for Docker D1 and -p 27018:27017 for Docker D2 and 5000:27017 for docker D3; Like this you can bind different host port to map to 27017 for mongo-db port; Now your question is how to manage this ports from host then I would recommend you to use nginx for port managing in the host machine.
Coming to your next question all dockers are connected to default docker0 bridge network so we can connect to any of the dockers connected to default bridge 'docker0' network; If I am right it will come with ipaddress of 172.x.x.x network. Get inside to the docker and run 'ip addr' to see the ip-address assigned to the dockers and you can test connection by running ping command.
Yes two containers can run same time, they can also communicate with each other also, you can define your own network and they can communicate with each other. if two containers have their private ports, they are their internal ports, one container port does not collide with another container port. if you want to expose the port to host, then you have to publish the port(s).

Communication between docker containers in different networks

If I know a Docker container IP address, I can easily communicate with it from another container, but as long as they are in same network.
My question is, how can I communicate with containers from another network and why can't I access local IP which is on the same machine? I am interested in network explanation why I can access 172.19.0.1 from 172.19.0.2 but I can't access 172.20.0.1 from 172.19.0.2.
What are possible workarounds to making Docker container from one network to communicate with docker container from another network?
You can publish a port and then access that port over localhost (or 0.0.0.0 for troubleshooting).
Other than that you could use an alternative to docker network like linking or other things. But I wouldn't suggest that. If you want two containers to communicate with eachother and not the public just create a new network for those two containers.
You can specify that this network is external and they can join it even from different compose files.

Resources