Communicating between different docker services in docker-compose

旧街凉风 提交于 2019-12-06 02:59:28

As clearly documented in Networking in Compose

Networked service-to-service communication use the CONTAINER_PORT

Thus you should use the container ports to communicate between the containers. http://bob:5000 and http://alice:5000 .

The Alice and Bob that you named in the docker-compose were docker name (docker run --name) not the hostname of the dockers; I would request you to defined "hostname" key in the docker-compose file to define hostname of the dockers; See example below:

   version: "3.0"
   services:
       lab1:
           image: ubuntu:version0
           container_name: lab1
           entrypoint: /bin/bash
           stdin_open: true
           hostname: lab1

       lab2:
          image: ubuntu:version0
          container_name: lab2
          entrypoint: /bin/bash
          stdin_open: true
          hostname: lab2

Once you defined 'hostname' in the docker-compose file then try to ping the containers using hostname; it should succeed first.

Coming next to making use of port; You binded Alice port 5000 to host port 5556 and Bob port 5000 to host port 5557; From host to reach to the specific docker port you need to use the port 5556 or 5557 to reach Alice or Bob containers respectively whereas if you want to reach the port of the container inside container itself then you need to use the actual port that was used by the containers to communicate; that is port 5556 or 5557 to reach Alice or Bob respectively from containers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!