Docker-compose links vs external_links

佐手、 提交于 2021-02-18 05:17:07

问题


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_links as I want to have core docker-compose and I want to extend it without overriding the core links.

What exactly I have, I am trying to setup logstash which depends on the elasticsearch. Elasticsearch is in the core docker-compose and the logstash is in the depending one. So I had to define the elastic search in the depended docker-compose as a reference as logstash need it as a link. BUT Elasticsearch has already its own links which I do not want to repeat them in the dependent one.

Can I do that with external_link instead of link?

I know that links will make sure that the link is up first before linking, does the external_link will do the same?

Any help is appreciated. Thanks.


回答1:


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




回答2:


I think external_link will do not do the same as links in docker-compose up command.

links waits for container to boot up and get IP address which is used in etc/hosts file, therefore external_link has already IP:hostname values name described in docker-compose file.

Moreover links will be deprecated




回答3:


Here is a link to Docker-Compose project that uses Elasticsearch, Logstash, and Kibana. You will see that I'm using links:

https://github.com/bahaaldine/elasticsearch-paris-accidentology-demo



来源:https://stackoverflow.com/questions/35154441/docker-compose-links-vs-external-links

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