Docker-compose 的健康检查对于 Neo4j Docker 并不起作用。

3

我有一个docker-compose文件,可以启动多个容器。我想避免当前看到的错误信息,使得其他docker容器在neo4j容器运行后才启动。目前,我的配置如下:

version: '3.7'

services:
  neo4j:
    container_name: neo4j 
    hostname: neo4j 
    image: neo4j:3.5
    restart: unless-stopped
    environment:
      - NEO4J_dbms_memory_pagecache_size=2G
      - dbms_connector_bolt_tls__level=OPTIONAL
      - NEO4J_dbms_memory_heap_max__size=3500M
      - NEO4J_AUTH=neo4j/start
    volumes:
      - $HOME/neo4j/data:/data
      - $HOME/neo4j/logs:/logs
      - $HOME/neo4j/import:/import
      - $HOME/neo4j/plugins:/plugins
    ports:
      - 7474:7474
      - 7687:7687
    healthcheck:
      test: ["CMD", "curl", "--fail", "http://localhost:7474/", "||", "exit 1"] 
      interval: 10s
      timeout: 2s
      retries: 10
  myapp:
    container_name: myapp
    hostname: myapp
    image: python:3.7.3-slim
    build: './APP'
    restart: on-failure
    environment:
      - PYTHONUNBUFFERED=1 
    ports:
      - 5000:5000 

healthcheck参数没有任何区别。我也使用了:

curl -i http://127.0.0.1:7474 2>&1 | grep -c -e '200 OK'  

并且...

["CMD-SHELL", "/var/lib/neo4j/bin/neo4j status"]

目前我的应用程序在尝试启动后会退出,直到neo4j正在运行。我该如何让我的应用程序等待neo4j运行?


这是我用于健康检查的代码:https://gist.github.com/fbiville/bf617f29a18b1ecc688bf312915d83f1#file-docker-start-sh-L42。这有帮助吗? - fbiville
2个回答

3
您确实正在寻找 depends_on,但前提是该服务必须处于健康状态。这意味着docker-compose文件必须如下所示:
version: "3.9"
services:
  myapp:
    ...
    depends_on:
      # Make sure the application only starts when the DB is indeed ready
      neo4j:
        condition: service_healthy

  neo4j:
     ...
     healthcheck:
      test: wget http://localhost:7474 || exit 1
      interval: 1s
      timeout: 10s
      retries: 20
      start_period: 3s

问题解决了。谢谢! - Catscratch

0

2
我最初有这个,但它不起作用,也不适用于docker-compose版本3.x,我相信。 - pymat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接