Docker-compose links vs external_links

后端 未结 3 808
梦谈多话
梦谈多话 2021-02-14 04:32

I believe it is simple question but I still do not get it from Docker-compose documentations. What is the difference between links and external_links?

I like external_li

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-14 05:30

    Use links when you want to link together containers within the same docker-compose.yml. All you need to do is set the link to the service name. Like this:

    ---
    elasticsearch:
      image: elasticsearch:latest
      command: elasticsearch -Des.network.host=0.0.0.0
      ports:
        - "9200:9200"
    
    logstash:
      image: logstash:latest
      command: logstash -f logstash.conf
      ports:
        - "5000:5000"
      links:
        - elasticsearch
    

    If you want to link a container inside of the docker-compose.yml to another container that was not included in the same docker-compose.yml or started in a different manner then you can use external_links and you would set the link to the container's name. Like this:

    ---
    logstash:
      image: logstash:latest
      command: logstash -f logstash.conf
      ports:
        - "5000:5000"
      external_links:
        - my_elasticsearch_container
    

    I would suggest the first way unless your use case for some reason requires that they cannot be in the same docker-compose.yml

提交回复
热议问题