Home > Net >  host.docker.internal vs container name when referencing ip
host.docker.internal vs container name when referencing ip

Time:12-11

When defining services in a docker-compose file, when do I use host.docker.internal for the host's ip and when do I need to use the container's name?

CodePudding user response:

All possible communication flows are illustrated here :

enter image description here

(1) : non-containerized process communicates with a container

(2) : container communicates with another container

(3) : the opposite direction of (1)


(1): container must forward port to host, so the non-containerized process can access it

services:
   c1:
    ...
     ports:
       - hostport:containerport

(2): container c1 just use the service name (container name - c2) to communicate with c2

(3): container c2 must Use host.docker.internal to communicate with host

CodePudding user response:

If you want to connect from a container to a service on the host, use host.docker.internal. Be aware, that this special DNS name is only available on Windows and macOS.

Use the container name for networking between containers.

  • Related