是否可以在 docker-compose 中强制启动容器之间的延迟?

时间:2021-04-25 12:50:42

标签: docker elasticsearch docker-compose

我正在关注此 docker tutorial,并且在使用 docker compose 时遇到了麻烦。 问题是当我运行 docker-compose up 时,Web 容器无法连接到 es,在几次失败的重试后,它放弃并死亡(Web 容器然后自行退出)。

然而,从日志来看,这似乎是因为 es 容器只是花时间开始 - 如果我在此之后重复 docker-compose up(一旦 es 已经启动并运行), Web 容器工作正常。

所以我想知道,有没有办法从 docker-compose.yml 文件中指定启动容器之间的时间延迟?

web容器最初连接es失败的终端输出如下:

C:\work\docker_learning\FoodTrucks> docker-compose up Docker Compose 现在位于 Docker CLI 中,请尝试 docker compose up

开始 es ... 完成 启动 foodtrucks_web_1 ... 完成 附加到 es, foodtrucks_web_1 es | OpenJDK 64 位服务器 VM 警告:选项 UseConcMarkSweepGC 在 9.0 版中已弃用,并且可能会在未来版本中删除。 es | [2021-04-25T12:39:43,255][INFO][o.e.n.Node] [] 正在初始化 ... es | [2021-04-25T12:39:43,411][INFO][oeeNodeEnvironment] [_ywWy9B] 使用 1 数据路径,挂载 [[/usr/share/elasticsearch/data (/dev/sdc)]],净可用空间 [230.9gb],净总空间 [250.9gb],类型 [ext4] es | [2021-04-25T12:39:43,412][INFO][o.e.e.NodeEnvironment] [_ywWy9B] 堆大小 [990.7mb],压缩普通对象指针 [true] es | [2021-04-25T12:39:43,458][INFO][o.e.n.Node] [_ywWy9B] 来自节点 ID [_ywWy9B8QZSIn81FhEchNA] 的节点名称;将 [node.name] 设置为覆盖 es | [2021-04-25T12:39:43,470][INFO][oenNode] [_ywWy9B] 版本[6.3.2],pid1,build[default/tar/053779d/2018-07-20T05:20 :23.451332Z]、操作系统[Linux/4.19.128-microsoft-standard/amd64]、JVM[“Oracle Corporation”/OpenJDK 64-Bit Server VM/10.0.2/10.0.2+13] es | [2021-04-25T12:39:43,471][INFO][oenNode] [_ywWy9B] JVM 参数 [-Xms1g、-Xmx1g、-XX:+UseConcMarkSweepGC、-XX:CMSInitiatingOccupancyFraction=75、--Xms1g、-Xmx1g、-XX:CMSInitiatingOccupancyFraction=75、- 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, -Djava.io.tmpdir=/tmp/elasticsearch.4LDlDF7A, -XX :+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data, -XX:ErrorFile=logs/hs_err_pid%p.log, -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,标签:filecount=32,filesize=64m, -Djava.locale.providers=COMPAT, -XX:UseAVX=2, -Des.cgroups.hierarchy.override=/, -Des.path.home=/usr/share/elasticsearch , -Des.path.conf=/usr/share/elasticsearch/config, -Des.distribution.flavor=default, -Des.distribution.type=tar] es | [2021-04-25T12:39:47,178][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [aggs-matrix-stats] es | [2021-04-25T12:39:47,180][INFO][o.e.p.PluginsService][_ywWy9B]加载模块[分析通用] es | [2021-04-25T12:39:47,180][INFO][o.e.p.PluginsService][_ywWy9B]加载模块[ingest-common] es | [2021-04-25T12:39:47,180][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [lang-expression] es | [2021-04-25T12:39:47,180][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [lang-mustache] es | [2021-04-25T12:39:47,181][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [lang-painless] es | [2021-04-25T12:39:47,182][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [mapper-extras] es | [2021-04-25T12:39:47,184][INFO][o.e.p.PluginsService][_ywWy9B]加载模块[parent-join] es | [2021-04-25T12:39:47,189][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [percolator] es | [2021-04-25T12:39:47,189][INFO][o.e.p.PluginsService][_ywWy9B]加载模块[rank-eval] es | [2021-04-25T12:39:47,189][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [reindex] es | [2021-04-25T12:39:47,190][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [repository-url] es | [2021-04-25T12:39:47,190][INFO][o.e.p.PluginsService][_ywWy9B]加载模块[transport-netty4] es | [2021-04-25T12:39:47,190][INFO][o.e.p.PluginsService][_ywWy9B]加载模块[部落] es | [2021-04-25T12:39:47,190][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-core] es | [2021-04-25T12:39:47,190][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-deprecation] es | [2021-04-25T12:39:47,190][INFO][o.e.p.PluginsService][_ywWy9B]加载模块[x-pack-graph] es | [2021-04-25T12:39:47,190][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-logstash] es | [2021-04-25T12:39:47,191][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-ml] es | [2021-04-25T12:39:47,191][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-monitoring] es | [2021-04-25T12:39:47,191][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-rollup] es | [2021-04-25T12:39:47,191][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-security] es | [2021-04-25T12:39:47,191][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-sql] es | [2021-04-25T12:39:47,191][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-upgrade] es | [2021-04-25T12:39:47,191][INFO][o.e.p.PluginsService] [_ywWy9B] 加载模块 [x-pack-watcher] es | [2021-04-25T12:39:47,192][INFO][o.e.p.PluginsService] [_ywWy9B] 加载插件 [ingest-geoip] es | [2021-04-25T12:39:47,192][INFO][o.e.p.PluginsService] [_ywWy9B] 加载插件 [ingest-user-agent] es | [2021-04-25T12:39:52,773][INFO][o.e.x.s.a.s.FileRolesStore] [_ywWy9B] 从文件 [/usr/share/elasticsearch/config/roles.yml] 解析 [0] 个角色 es | [2021-04-25T12:39:55,243][INFO][o.e.d.DiscoveryModule] [_ywWy9B] 使用发现类型 [单节点] web_1 | /usr/local/lib/python3.6/dist-packages/requests/init.py:91: RequestsDependencyWarning: urllib3 (1.26.4) 或 chardet (3.0.4) 不匹配支持版本! web_1 |请求依赖警告) web_1 |无法连接到 ES。 5 秒后重试... web_1 |无法连接到 ES。 5 秒后重试... web_1 |重试次数不足。保释... es | [2021-04-25T12:39:57,622][INFO][o.e.n.Node] [_ywWy9B] 初始化 es | [2021-04-25T12:39:57,623][INFO][o.e.n.Node] [_ywWy9B] 开始... es | [2021-04-25T12:39:57,925][INFO][o.e.t.TransportService] [_ywWy9B] publish_address {172.26.0.2:9300}, bound_addresses {0.0.0.0:9300} es | [2021-04-25T12:39:58,004][WARN][o.e.b.BootstrapChecks] [_ywWy9B] 最大虚拟内存区域 vm.max_map_count [65530] 太低,至少增加到 [262144] es | [2021-04-25T12:39:58,116][INFO][o.e.x.s.t.n.SecurityNetty4HttpServerTransport] [_ywWy9B] publish_address {172.26.0.2:9200}, bound_addresses {0.0.0.0}:9200 es | [2021-04-25T12:39:58,117][INFO][o.e.n.Node] [_ywWy9B] 开始 es | [2021-04-25T12:39:59,070][WARN][o.e.x.s.a.s.m.NativeRoleMappingStore] [_ywWy9B] 无法清除领域 [[]] 的缓存 es | [2021-04-25T12:39:59,147][INFO][o.e.l.LicenseService] [_ywWy9B] 许可证 [8feb64bb-90d9-4dd7-8c89-a65273596aaa] 模式 [基本] - 有效 es | [2021-04-25T12:39:59,173][INFO][o.e.g.GatewayService] [_ywWy9B] 将 1 索引恢复到 cluster_state es | [2021-04-25T12:39:59,954][INFO][oecraAllocationService] [_ywWy9B] 集群健康状态从 [RED] 变为 [YELLOW](原因:[shards started [sfdata] ...] )。 `

1 个答案:

答案 0 :(得分:1)

尝试在docker-compose文件中添加depends_on和健康检查

services:
    service_1:
       ...
       depends_on: 
          db:
             condition: service_healthy

    service_2:
       healthcheck:
          ...

它将强制执行容器启动顺序

同样来自文档的示例

version: "2.4"
services:
  web:
    build: .
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
  redis:
    image: redis
  db:
    image: postgres
    healthcheck:
      test: "exit 0"

https://docs.docker.com/compose/compose-file/compose-file-v2/#depends_on

更新

试试这个

es:
  ...
  healthcheck:
    test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
    interval: 30s
    timeout: 30s
    retries: 3
相关问题