如何在docker-compose文件中使用两个健康检查,而python应用程序依赖于这两个健康检查?

pieyvz9o  于 2022-11-02  发布在  Docker
关注(0)|答案(1)|浏览(153)

我有两个postgres容器,一个是mdhillon/postgis,另一个是postgrest/postgrest。python应用程序依赖于这两个postgres容器的健康检查。请帮助
在码头完成后在码头

Creating compose_postgis_1 ... done
Creating compose_postgrest_1 ... done

Error for app Container <postgrest_container_id> is unhealthy. And the terminal exits

正在显示Docker-compose.yml文件

services:
  postgis:
    image: mdillon/postgis
    volumes:
      - ./data:/var/lib/postgresql/data:cached
    ports:
      - 5432:5432
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5
  postgrest:
    image: postgrest/postgrest
    volumes:
      - ./data:/var/lib/postgresql/data:cached
    environment:
      PGRST_DB_URI: postgres://${PGRST_DB_ANON_ROLE}:@postgis:5432/postgres
      PGRST_DB_SCHEMA: ${PGRST_DB_SCHEMA}
      PGRST_DB_ANON_ROLE: ${PGRST_DB_ANON_ROLE}
      PGRST_DB_POOL: ${PGRST_DB_POOL}  
    ports:
      - 3000:3000
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5
  app:
    image: newapp
    command: python main.py
    ports:
      - 5000:5000
    depends_on:
      postgis:
        condition: service_healthy
      postgrest:
        condition: service_healthy
b1zrtrql

b1zrtrql1#

如果你使用的是官方的Postgres Docker镜像,这里有一个选项可以让你在一个特定的端口上运行postgres。你需要添加ENV变量PGPORT来让postgres Docker容器在一个不同的端口上运行。试试下面的一个...

services:
  postgis:
    image: mdillon/postgis
    volumes:
      - ./data:/var/lib/postgresql/data:cached
    ports:
      - 5432:5432
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5
  postgrest:
    image: postgrest/postgrest
    volumes:
      - ./data:/var/lib/postgresql/data:cached
    environment:
      PGRST_DB_URI: postgres://${PGRST_DB_ANON_ROLE}:@postgis:5432/postgres
      PGRST_DB_SCHEMA: ${PGRST_DB_SCHEMA}
      PGRST_DB_ANON_ROLE: ${PGRST_DB_ANON_ROLE}
      PGRST_DB_POOL: ${PGRST_DB_POOL}
      PGPORT: 3000
    ports:
      - 3000:3000
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 5s
      retries: 5
  app:
    image: newapp
    command: python main.py
    ports:
      - 5000:5000
    depends_on:
      postgis:
        condition: service_healthy
      postgrest:
        condition: service_healthy

默认情况下,Postgres容器在Docker网络内的端口5432上运行。由于您没有更改Postgres容器的端口,因此两个容器都尝试在Docker网络内的同一个端口上运行,因此,一个容器将运行,另一个将不运行。您可以查看Docker容器的日志以更好地了解。
因此,将PGPORT env var添加到容器以在diff端口上运行Postgres将解决您的问题...

相关问题