Connecting 3 docker containers where one container is both a server and client

Hi,

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 :slight_smile:

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

docker run --network=e_p_net -p 8101 containerE

Do the next step in growing/learning as a Docker developer and upgrade to compose (doc) :smile:

And why do you think it should be able to?

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…

Maybe if you format your posts next time as described in the following guide that helps: How to format your forum posts

Your commands are still strange. Like this:

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.

My apologies, I’m quite new to this so I apologise if I’m missing something obvious.

Here is what I’m trying to do in each command:

Container C should be connected to network p_c_net and be able to take inputs from Container P.

docker run --network=p_c_net -p 6051 --name p_c_net containerC

Container P should be connected to both network p_c_net and e_p_net to that it can receive information from Container E and C.

docker run --network=e_p_net --network=p_c_net containerP

Finally container E should be connected to network e_p_net only and be able to send and receive informantion from container P.

docker run --network=e_p_net -p 8101 containerE

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.

  1. 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?
  2. 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.

Thank you,
Ishika

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.

1 Like