I am building an API framework where I have 3 docker containers. The first is a client (E) that sends request to the second container (P). I am able to connect these 2 over a network I defined (code below) and get them to communicate. Now I would like to add the 3rd container (C) which is a server for container P, so only those 2 need to be able to communicate and C need to return the message to P before P can finish its request and return the message to E.
I have having some issues when I add the 2nd network where container P is not able to connect with container C. The docker container use python sockets to connect under the hood. Any suggestions would be great (Iām new to docker)! Thank you
docker network create e_p_net
docker network create p_c_net
docker run --network=p_c_net -p 6051 --name p_c_net containerC
docker run --network=e_p_net --network=p_c_net containerP
Since the code in Container P has a socket connection to container C and should be able to receive the messages if we publish the ports and allow them to share a network would be my assumptions. When the 3 scripts are running without docker they are able to communicate with each other use their IP addresses and ports so I assumed we would be able to create similar functionality if the network and port sharing is set up correctly. Thanks!
Oh, sorry, I looked at the commands for long to make sure I donāt miss anything and I still missed you actually connected containerP to two networks. I was pretty sure I saw only oneā¦
docker run --network=p_c_net -p 6051 --name p_c_net containerC
I canāt decide whether you wanted to add a second network or the second āp_c_netā string was left their by accident. But then the image name is missing.
Either way, unless you show how you try to make the connection, you can only get guesses.
You only shared how you started the containers. We know nothing about how you exactly try to connect. Thatās the important part. What host, port you are using. Please, share everything relevant to the connection.
Sorry about the slow reply - I spent some time doing more detailed reading.
Before I give more details I want to make sure at a high level what Iām trying to do is correct.
If I want to connect 3 docker containers but only 1 of them needs to talk to 2 should they all be on the same docker network? Or is it best to make 2 docker networks and attached containers to those based on which containers communicate?
The containers communicate as 2 client server pairs where the server for one is a client for another. They use python TCP sockets so require an IP address and port to send messages back and forth. My understanding is that this requires us to create a custom bridge network and expose corresponding port to allow this - please correct me if Iām wrong.
If A talks to B and A talks to C, you can create two Docker networks AB and AC. You can also create a single Docker network ABC. It mostly depends on your security requirements: if B and C should never be able to connect.
When containers are in a Docker network, default is bridge mode, then you donāt need to expose ports, they are all reachable inside.
You only need to expose ports when a service should be reachable from outside.