In many applications, there are more than one containers running. There are different communications involved
- Containers communicating with host machine
- Containers communicating with each other
- containers communicating with internet
Communication with host machine
By default, Docker containers cannot access the host machine’s network since they are segregated from it. Using the hostname “host.docker.internal” is one method of allowing containers to access the network of the host system. A unique address called host.docker.internal is converted by Docker to the IP address of the server running the container.
The “localhost” inside of the Container refers to the Container environment, not to the local host machine which is running the Container.
Communication with other containers
When two containers are connected to the same network, they can speak to one another using their container names.
sample:
docker run -network my-network --name app1 image1
docker run -network my-network --name app2 image2
Both app1 and app2 would be on the same network i.e “my-network”. They can communicate with each using their names i.e app1 and app2.
Communication with internet
This will work out of the box, no extra configuration is required.
References