Can (or should) 2 docker containers communicate with each other via localhost?

We fake our microservice application, and I ran into some opening issues.

The application is configured as follows:

When the service starts in non-local mode, Consul is used as the Discovery registry. When a service starts in "local" mode, it automatically binds the address for the service (for example, tcp: // localhost: 61001, tcp: // localhost: 61002, etc. Hard-coded addresses)

After docking the application (only for local mode, at the moment), each service is a container (Docker images created using the docker layout. And with the docker machine, if that matters) But one service cannot interact with another service, since they are not on the same machine, and tcp: // localhost: 61001 will obviously not work.

Using docker-compose with links and specifying localhost as an alias (service: localhost) does not work. Is there a way for two containers to "share" with the same local host?

If not, what is the best way to approach this? I thought about using a specific hostname for each service, and then specify the hostname in the links section in the docker-compose file. (But I doubt that this is an elegant solution) Or, perhaps, use the docked version of the Consul and integrate with it?

This post: How to share a local host between two different Docker containers? provided some information on why localhost should not be confused - but I'm still pretty puzzled on what the right approach is here.

Thank!

+4
source share
3 answers

, , tcp://localhost: 61001, , .

, . , tcp://localhost:61001 , localhost , , localhost . , . , , , Docker Compose.

, , , , , URL-, IP- , /etc/hosts ( , , , ). , , , .

, docker-compose.yml :

a:
  expose:
    - "9999"
b:
  links:
    - a

a 0.0.0.0:9999, b a, b tcp://a:9999. b

ping a

ping a b.

, localhost URL- ( , ). ,

tcp://<container_name>:61001

tcp://localhost:61001

, docker-compose.yml.

,

+2

. (rancher, docker swarm, k8s,...) . Orchestrator . , ( ip).

. , . ( A B 1234, B, A BBBB, tcp://BBBB: 1234 A)

, IP- , .

+2

, , .

, ssh .

, , ssh . socat redir , .

socat TCP4-LISTEN:61001,fork TCP4:othercontainer:61001
+1

Source: https://habr.com/ru/post/1683181/


All Articles