Service discovery between containers

Last Updated: Feb 13, 2018

Container Service provides multiple methods of service discovery for the services and containers in the cluster. The service can be discovered by using the container name, link, or hostname.

Container name

Container Service can be accessed by using the container IP address or the name of another container in the network. In the example described in Container network interconnection, you can access the container test_network-test1_1 by using its container name in the container test_network-test2_1.

If the container_name is not specified in the orchestration file, the default container name is {project-name}_{service-name}_{container-index}. After connecting to the web terminal of a container, you can access a container of another service by using the container name to test the network interconnection.

Link

Container Service supports the link between services in an orchestration template. The link between services can link the containers of a service to the containers of another service. In containers, you can access the dependent containers by using the alias of the linked service. When the IP address of the dependent container changes, the alias resolved IP address will be dynamically updated. For a specific example, see the WordPress orchestration in Container Service sample orchestration. The web service in WordPress links the db:mysql service to the containers as follows. Then, the containers can access the containers of the db service by using the MySQL domain name.

  1. links:
  2. - 'db:mysql'

Hostname

If the hostname configuration is defined in the orchestration template service, the container can be accessed by using this hostname in the cluster.

For example,

  1. testhostname:
  2. image: busybox
  3. hostname: xxserver
  4. command: sleep 100000
  5. tty: true

In the cluster, you can resolve and access the container of this service by using xxserver. For more information, see the orchestration example in Container network interconnection. If this service contains several containers, accessing the containers by using this hostname allows Server Load Balancer to take effect.

If the service does not configure the hostname, Container Service will use the container name as the internal hostname of the container. If an application in the container needs to know the container name for service registration, such as Eureka Client, register an accessible address to Eureka Server. The process in the container can obtain the container name for service registration and enable other service callers to access each other by using the container name.

Thank you! We've received your feedback.