Communication between containers

I have started two containers and linked them with “–link”. But, do I need to add EXPOSE in each other’s Dockerfile to let them communicate?

I have two services that are deployed on two containers. Only a one ports need to be exposed for external use, but the two containers require communicate with each other with a wide range of port. And they may change over time.

So I wonder if there’s any way to let the containers communicate with each other freely.

look at my post :slight_smile:docker communication

Thanks, but how does a customized network help me with port exposure? Do you imply that customized network does not have port communication restriction?

Thanks~

Okey so maybe it was not clear enough. So between container since they are in the same NAT, they can communicate with each other. The expose part in the dockerfile if I understand that correctly is to communicate outside the container itself, so even in the NAT. But you can try it by take a docker container in the github and change the dockerfile by eliminating the 22 port for ssh and see if inside the nat itself it does work or not. So the expose part for me is to communicate outside the container. To communicate outside the NAT so to everybody is the publish part of the command docker run -p 22:22 . There is normally a clear difference between EXPOSE and PUBLISH.
Assuming that I’ve correctly understand the working protocols of docker.
but of course I don’t what you want to create but I think the philosophy here is to restrict the expose part and even more the publish part to the strict bare minimum…

I see. If all the containers are in one network, they can discover each
other. But if you create the containers without specifying a network, they
will be in their own network. That’s why I found I have expose all the
ports to let them to talk.

well they won’t be able to talk unless you put them in the same network apparently. And the expose ports are in the case where they are on the same network. Since by default, or maybe it has changed, they are on their own network they can’t discuss with each other, or they can but only if you have your box or router who has the loopback activated which is not the cas on ubiquiti routers for example. Or another case, is that you configure your host with a loopback option but not sure that docker accept that. Anyway if they are not on the same network they cant speak with each other, that what’s you have to know. And expose port are a requirement to speak with the rest of a same network.