Docker Compose的 Postgres实例突然停止工作

后端开发 2026-07-09

我正在使用Docker Compose来运行一个Postgres实例。到现在为止,一切都正常,尽管我对docker compose文件没有做任何修改。数据库无法连接,健康检查失败。我尝试清理我的镜像和卷。我也尝试升级Docker。

我已经尝试了这些命令:

$ docker inspect --format='{{json .State.Health}}' microblog-lucid-db-1
{"Status":"unhealthy","FailingStreak":10,"Log":[{"Start":"2026-06-03T20:27:01.95169733Z","End":"2026-06-03T20:27:02.021298912Z","ExitCode":-1,"Output":"OCI runtime exec failed: exec failed: unable to start container process: exec: \"pg_isready -d microblog\": executable file not found in $PATH: unknown"},{"Start":"2026-06-03T20:27:12.022492502Z","End":"2026-06-03T20:27:12.082637862Z","ExitCode":-1,"Output":"OCI runtime exec failed: exec failed: unable to start container process: exec: \"pg_isready -d microblog\": executable file not found in $PATH: unknown"},{"Start":"2026-06-03T20:27:22.083658616Z","End":"2026-06-03T20:27:22.196740614Z","ExitCode":-1,"Output":"OCI runtime exec failed: exec failed: unable to start container process: exec: \"pg_isready -d microblog\": executable file not found in $PATH: unknown"},{"Start":"2026-06-03T20:27:32.198814253Z","End":"2026-06-03T20:27:32.258340819Z","ExitCode":-1,"Output":"OCI runtime exec failed: exec failed: unable to start container process: exec: \"pg_isready -d microblog\": executable file not found in $PATH: unknown"},{"Start":"2026-06-03T20:27:42.259947925Z","End":"2026-06-03T20:27:42.317225829Z","ExitCode":-1,"Output":"OCI runtime exec failed: exec failed: unable to start container process: exec: \"pg_isready -d microblog\": executable file not found in $PATH: unknown"}]}
$ docker run --rm --entrypoint sh postgres:18 -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/18/bin
$ docker exec -it microblog-lucid-db-1 sh -c 'pg_isready -d $POSTGRES_DB'
/var/run/postgresql:5432 - accepting connections

我不明白这个错误是什么意思。尝试在Google上搜索 OCI runtime exec failed: exec failed: unable to start container process: execexecutable file not found in $PATH: unknown 并没有出现任何解释正在发生的事情的结果。解决方案要么不是针对compose文件,要么是针对从未起作用过的文件。

我的docker-compose.yml:

services:
  db:
    image: postgres:18
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?}
      POSTGRES_DB: ${POSTGRES_DB:?}
    healthcheck:
      test: ["CMD", "pg_isready -d $POSTGRES_DB"]
      interval: 10s
      timeout: 5s
      retries: 5
    shm_size: 128mb

  redis:
    image: redis
    restart: always
    entrypoint: redis-server --save 20 1 --loglevel warning
    ports:
      - 6379:6379
    volumes:
      - redis_data:/usr/share/redis/data

volumes:
  redis_data:
    driver: local

一个原本能够工作的Docker compose文件,为什么在没有改动的情况下突然停止工作?

解决方案

Compose healthcheck: 有两种形式。若你指定 test: ["CMD", ...],那么你需要把命令拆分为单词;若你指定 test: ["CMD-SHELL", ...],那么Docker会把命令包裹在 sh -c 中。环境变量展开 需要 一个shell,你应该把 CMD 改成 CMD-SHELL。一个裸字符串默认为 CMD-SHELL,那样也更容易。

healthcheck:
  test: pg_isready -d $$POSTGRES_DB

(另外请注意,你需要把美元符号加倍:默认情况下,Compose将使用主机系统的环境来对环境变量引用进行插值,但在这里你可能更希望使用容器的环境。)

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章