Docker通过网络组合连接容器

时间:2018-01-21 01:29:26

标签: docker curl elasticsearch networking docker-compose

我正在尝试使用elasticsearch设置docker compose,并能够通过共享网桥将其他容器连接到它。我可以设置两个容器,但是当我试图将它们连接在一起时,我得到“连接被拒绝”。

如果可能的话,我想使用expose代替ports documented herediscussed here,因为我认为这是当前版本的首选方法。我不知道如何设置它。

version: '2' # docker-compose config file format version

services:

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.1.2
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=elasticsearch"
    expose:
      - "9200"
      - "9300"
    volumes:
      - esdata:/usr/share/elasticsearch/data
    networks:
      esnet:


volumes:
  esdata:


networks:
  esnet:
    driver: bridge

似乎工作:

$ docker-compose -f docker-compose3.yml  up
Starting elasticsearch ... done
Attaching to elasticsearch
elasticsearch    | [2018-01-21T01:22:12,059][INFO ][o.e.n.Node               ] [] initializing ...
elasticsearch    | [2018-01-21T01:22:12,128][INFO ][o.e.e.NodeEnvironment    ] [a1sCmMh] using [1] data paths, mounts [[/usr/share/elasticsearch/data (/dev/mapper/ubuntu--vg-root)]], net usable_space [51.9gb], net total_space [101gb], types [ext4]
elasticsearch    | [2018-01-21T01:22:12,129][INFO ][o.e.e.NodeEnvironment    ] [a1sCmMh] heap size [494.9mb], compressed ordinary object pointers [true]
elasticsearch    | [2018-01-21T01:22:12,136][INFO ][o.e.n.Node               ] node name [a1sCmMh] derived from node ID [a1sCmMhEQD-bQXiQqCtSrw]; set [node.name] to override
elasticsearch    | [2018-01-21T01:22:12,136][INFO ][o.e.n.Node               ] version[6.1.2], pid[1], build[5b1fea5/2018-01-10T02:35:59.208Z], OS[Linux/4.13.0-25-generic/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/1.8.0_151/25.151-b12]
elasticsearch    | [2018-01-21T01:22:12,136][INFO ][o.e.n.Node               ] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.cgroups.hierarchy.override=/, -Xms512m, -Xmx512m, -Des.path.home=/usr/share/elasticsearch, -Des.path.conf=/usr/share/elasticsearch/config]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [aggs-matrix-stats]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [analysis-common]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [ingest-common]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [lang-expression]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [lang-mustache]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [lang-painless]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [mapper-extras]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [parent-join]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [percolator]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [reindex]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [repository-url]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [transport-netty4]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded module [tribe]
elasticsearch    | [2018-01-21T01:22:13,137][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded plugin [ingest-geoip]
elasticsearch    | [2018-01-21T01:22:13,138][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded plugin [ingest-user-agent]
elasticsearch    | [2018-01-21T01:22:13,138][INFO ][o.e.p.PluginsService     ] [a1sCmMh] loaded plugin [x-pack]
elasticsearch    | [2018-01-21T01:22:14,873][INFO ][o.e.x.m.j.p.l.CppLogMessageHandler] [controller/120] [Main.cc@128] controller (64 bit): Version 6.1.2 (Build 258b02b7b1f613) Copyright (c) 2018 Elasticsearch BV
elasticsearch    | [2018-01-21T01:22:15,148][INFO ][o.e.d.DiscoveryModule    ] [a1sCmMh] using discovery type [zen]
elasticsearch    | [2018-01-21T01:22:15,559][INFO ][o.e.n.Node               ] initialized
elasticsearch    | [2018-01-21T01:22:15,559][INFO ][o.e.n.Node               ] [a1sCmMh] starting ...
elasticsearch    | [2018-01-21T01:22:15,643][INFO ][o.e.t.TransportService   ] [a1sCmMh] publish_address {172.18.0.2:9300}, bound_addresses {[::]:9300}
elasticsearch    | [2018-01-21T01:22:15,659][INFO ][o.e.b.BootstrapChecks    ] [a1sCmMh] bound or publishing to a non-loopback address, enforcing bootstrap checks
elasticsearch    | [2018-01-21T01:22:18,709][INFO ][o.e.c.s.MasterService    ] [a1sCmMh] zen-disco-elected-as-master ([0] nodes joined), reason: new_master {a1sCmMh}{a1sCmMhEQD-bQXiQqCtSrw}{E5_hsKPTRmCDYR4uZpr1-A}{172.18.0.2}{172.18.0.2:9300}{ml.machine_memory=16785715200, ml.max_open_jobs=20, ml.enabled=true}
elasticsearch    | [2018-01-21T01:22:18,713][INFO ][o.e.c.s.ClusterApplierService] [a1sCmMh] new_master {a1sCmMh}{a1sCmMhEQD-bQXiQqCtSrw}{E5_hsKPTRmCDYR4uZpr1-A}{172.18.0.2}{172.18.0.2:9300}{ml.machine_memory=16785715200, ml.max_open_jobs=20, ml.enabled=true}, reason: apply cluster state (from master [master {a1sCmMh}{a1sCmMhEQD-bQXiQqCtSrw}{E5_hsKPTRmCDYR4uZpr1-A}{172.18.0.2}{172.18.0.2:9300}{ml.machine_memory=16785715200, ml.max_open_jobs=20, ml.enabled=true} committed version [1] source [zen-disco-elected-as-master ([0] nodes joined)]])
elasticsearch    | [2018-01-21T01:22:18,741][INFO ][o.e.x.s.t.n.SecurityNetty4HttpServerTransport] [a1sCmMh] publish_address {172.18.0.2:9200}, bound_addresses {[::]:9200}
elasticsearch    | [2018-01-21T01:22:18,742][INFO ][o.e.n.Node               ] [a1sCmMh] started
elasticsearch    | [2018-01-21T01:22:19,034][INFO ][o.e.l.LicenseService     ] [a1sCmMh] license [3c9a3962-dbc9-4fd9-9826-7569974c8ffd] mode [basic] - valid
elasticsearch    | [2018-01-21T01:22:19,039][INFO ][o.e.g.GatewayService     ] [a1sCmMh] recovered [1] indices into cluster_state
elasticsearch    | [2018-01-21T01:22:19,256][INFO ][o.e.c.r.a.AllocationService] [a1sCmMh] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[.monitoring-es-6-2018.01.21][0]] ...]).

但在另一个终端窗口中运行:

$ docker run --rm --network docker_esnet byrnedo/alpine-curl  -XGET 'localhost:9200/_all'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (7) Failed to connect to localhost port 9200: Connection refused

1 个答案:

答案 0 :(得分:0)

你可以试试这样的东西吗?

版本:

服务:

elasticsearch:
  image: docker.elastic.co/elasticsearch/elasticsearch:6.1.2
  container_name: elasticsearch
  environment:
    - cluster.name=docker-cluster
    - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    - "discovery.zen.ping.unicast.hosts=elasticsearch"
  ports:
    - "9200"
    - "9300"
  volumes:
    - esdata:/usr/share/elasticsearch/data

卷:     esdata:

编辑:

确保dockerfile中有这样的行:

EXPOSE 9200 9300