将docker-compose.yml文件拆分,并使用external_links选项连接容器
首先/先苦在这里
这是在将docker-compose文件进行拆分时所做的。
这次我们将从Kibana与Elasticsearch进行通信。
环境
-
- docker-compoase
-
- elasticsearch
- kibana
docker-compose.yml在分割之前的版本
version: '2'
services:
elasticsearch:
container_name: esc
image: esi:1.0.0
build: ./es
volumes:
- ./data/es:/usr/share/elasticsearch/data
ports:
- 9200:9200
expose:
- 9300
kibana:
container_name: kibanac
image: kibanai:1.0.0
build: ./kibana
links:
- elasticsearch
ports:
- 5601:5601
使用links来拆分这个docker-compose.yml文件。
docker-compose.yml 分割后的文件
目录结构如下所示。
└── elasticsearch_external_links
└── es_d
├── docker-compose.yml
├── Dockerfile
└── config
└── elasticsearch.yml
└── kibana_d
├── docker-compose.yml
├── Dockerfile
└── config
└── kibana.yml
http.host: 0.0.0.0
### x-pack functions
xpack.security.enabled: false
# 無償利用は1クラスタまで
xpack.monitoring.enabled: true
xpack.graph.enabled: false
xpack.watcher.enabled: false
version: '2'
services:
elasticsearch:
build: .
container_name: es_c_el
image: es_i_el:1.0.0
volumes:
- ./data/es:/usr/share/elasticsearch/data
ports:
- 9200:9200
expose:
- 9300
FROM docker.elastic.co/elasticsearch/elasticsearch:5.5.3
COPY ./config/elasticsearch.yml /usr/share/elasticsearch/config/elasticsearch.yml
# kuromojiをインストール
RUN elasticsearch-plugin install analysis-kuromoji
---
# Default Kibana configuration from kibana-docker.
server.name: kibana
server.host: "0"
elasticsearch.url: http://elasticsearch:9200
elasticsearch.username: elastic
elasticsearch.password: changeme
xpack.monitoring.ui.container.elasticsearch.enabled: true
version: '2'
services:
kibana:
build: .
container_name: kibana_c_el
image: kibana_i_el:1.0.0
external_links:
- elasticsearch
ports:
- 5601:5601
networks:
- default
- es1_default
networks:
es1_default:
external:
name: es_d_default
重点是目录名直接作为网络名称的部分。
FROM docker.elastic.co/kibana/kibana:5.5.3
COPY ./config/kibana.yml /opt/kibana/config/kibana.yml
进行
启动 Elasticsearch
$ cd es_d
$ docker-compose up -d --build
启动Kibana
$ cd kibana_d
$ docker-compose up -d --build
查询Docker网络
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
aaaaaaaaaaaa es_d_default bridge local
bbbbbbbbbbbb kibana_d_default bridge local
Elasticsearch的本地地址是http://localhost:9200/,而Kibana的本地地址是http://localhost:5601/。
分割前的 github 示例
分割后的 github 示例
这样就可以了