---
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
连接不同docker-compose.yml中的容器,需要使用external_links参数,设置目标容器的名字。
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
建议把应用放在同一个docker-compose.yml文件中
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、51CTO网站相关资料:
容器定义在同一个docker-compose.yml文件中的情况与上边一样,来看在不同yml文件中的情况:
我们还是使用nginx镜像来模拟这样的一个情景:假设我们需要将两个使用Docker Compose管理的nignx容器( test1 和 test2 )链接起来,使得 test2 能够访问 test1 中提供的服务,这里我们以能ping通为准。
首先,我们定义容器 test1 的 docker-compose.yml 文件内容为:
version: "3"
services:
test2:
image: nginx
container_name: test1
networks:
- default
- app_net
networks:
app_net:
external: true
容器 test2 内容与 test1 基本一样,只是多了一个 external_links ,需要特别说明的是: 最近发布的Docker版本已经不需要使用external_links来链接容器,容器的DNS服务可以正确的作出判断 ,因此如果你你需要兼容较老版本的Docker的话,那么容器 test2 的 docker-compose.yml文件内容为