In docker-compose how to create an alias / link to localhost?

后端 未结 3 1708
Happy的楠姐
Happy的楠姐 2021-02-13 15:00

In my docker-compose file there is a need for several containers to know the hostname of a specific container, including this specific container.

Links will not work, si

3条回答
  •  日久生厌
    2021-02-13 15:26

    You should avoid using links. Instead, services on the same Docker network can find each other by using service names as DNS names. Use that to reference the specific container you described, including when it references itself.

    For example, in the following made up Docker Compose file, if someservice was a web server serving on port 80, anotherservice service would be able to connect to it at http://someservice/, because they're on a common network the_net.

    version: '3'
    
    services:
      someservice:
        image: someserviceimage
        networks:
          - the_net
    
      anotherservice:
        image: anotherserviceimage
        networks:
          - the_net
    
    networks:
      the_net:
    

    someservice can also reach itself at http://someservice/.

提交回复
热议问题