0

Images: myprojects, listener

Containers: A, B, C

enter image description here

in myproject there are bunch of project that communicate using telnet and other protocols and I need two of those project to run. So I can't just run them in host because when one of the projects is running, the other project will results in error of "Can't bind ... already in used". hence I need to run to container of myprojects.

A and B will with no issue with the below command:

sudo docker run -itd --name A --privileged myprojects:1.0 bash run.sh

and also I can see the ports with lsof -i in A and B.

in other image, in listener there is a project which will Read/Write to those port which I'm written. The real problem is if I used --network=host to creating containers from images, some port will be used by A need to be available for inner-projects in B, but there are not available and it doesn't run.

What I want is, I want to map those ports on A and B to those ports on C and then the project in A and B are just map these ports and doesn't have any conflict with each other ports, so the inner-projects will run successfully and the project in C can listen to those 6 ports.

P.S: It's important to mention that, the C container will communicate with a device on ip:192.168.10.2.

Example

as David Maze pointed out, here is an example of how I run the containers:

sudo docker run -itd --name A --privileged myprojects:1.0
sudo docker run --itd --name B --privileged myprojects:1.0
sudo docker run --itd --name C --privileged listener:1.0

I can NOT use --network=host because if I do that, the inner-projects in A and B will have be conflict in shared ports, like localhost: 1111 -> localhost: 1212 and etc. Also I need to connect a device on IP:192.168.10.2 to C and I can't even do that. I tried sudo docker run -itd --name C --privileged --ip 192.168.10.2 listener:1.0 and it's runs fine and I can even get results from ping 192.168.10.2, but the device does not recognized while in the host, it can be recognized! I think that ping 192.168.10.2 give me results from container not the ping on the host (or I don't know, because I'm a newbie in docker world...).

  • Can you [edit] the question to include a [mcve] describing your issue in code? `docker run --network=host` is contrary to your need to run each container in an isolated network environment, and shouldn't normally be necessary; similarly you should almost never need `--privileged`. Importantly, within each container, `localhost` is the current container and not one of the other containers, and you might need to configure the endpoints differently in a container environment. – David Maze Jun 01 '23 at 11:31
  • @DavidMaze I edited. I need privileged because of the sudo and also admin privilege on usb. I can change all of ports and IPs in the picture, but I need the bridge between dockers, so that's where I'm stock... – Alireza Nikpay Jun 03 '23 at 05:44
  • Those containers are using an obsolete "default bridge networking" mode; for the current Docker networking environment you need a `docker run --net` option with a `docker network create` network. Also see [How to communicate between Docker containers via "hostname"](https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname). – David Maze Jun 05 '23 at 11:13
  • @DavidMaze as I understand creating a network and attach those containers doesn't help me much, because inner-communication on **A** and **B** are in the same ip and ports and can't run them in a same network (if I can run those two in a same network, I really don't need docker!). Am I missing something about network? – Alireza Nikpay Jun 06 '23 at 04:56
  • Inside Docker, each container has its own IP address, and you can run multiple containers that listen on the same port without conflicts. Conversely, since each container has its own IP address, each container separately thinks `localhost` is itself. – David Maze Jun 06 '23 at 09:50

0 Answers0