You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

docker-compose启动Flyway容器卡在Attaching to flyway问题求助

docker-compose编排Flyway+PostgreSQL+Python服务栈故障排查

故障现象

  • 执行docker-compose up -d时终端提示postgres、flyway、clover三个容器均已成功创建,但flyway容器无任何日志输出
  • 单独启动postgres和clover服务后,再执行docker-compose up flyway,终端输出postgres is up-to-date、Creating flyway ... done后就持续停留在Attaching to flyway阶段,无后续执行响应
  • 前期排查曾怀疑故障与flyway服务的volumes挂载配置相关,多次调整- ./flyway/sql:/flyway/sql的挂载路径组合均未解决问题

相关配置

Dockerfile

FROM python:3.10.1-slim

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY ./app .
COPY flyway/ /flyway/

COPY init.sql /docker-entrypoint-initdb.d/

docker-compose.yml

version: '3.8'

services:
  clover:
    container_name: clover
    build: .
    command: uvicorn app.main:app --reload --host 0.0.0.0
    volumes:
      - .:/app/
    ports:
      - "8008:8000"
    depends_on:
      - postgres
      - flyway

  postgres:
    image: postgres:14.1-alpine
    container_name: postgres
    restart: always
    volumes:
      - pg_data:/var/lib/postgresql
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - "5433:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=clover

  flyway:
    image: flyway/flyway
    container_name: flyway
    command: -url=jdbc:postgresql://clover:5433/ -schemas=polk_housing -user=postgres -password=postgres -connectRetries=60 migrate
    volumes:
      - ./flyway/sql:/flyway/sql
    depends_on:
      - postgres

volumes:
    pg_data:

命令执行输出

docker-compose up -d 输出

Creating postgres ... done
Creating flyway   ... done
Creating clover   ... done

单独启动flyway服务输出

$ docker-compose up flyway
postgres is up-to-date
Creating flyway ... done
Attaching to flyway

故障根因

本次故障和volumes挂载配置无关,核心问题是flyway服务的JDBC连接参数配置错误:

  1. 服务地址填写错误:docker-compose内部网络通过服务名做域名解析,PostgreSQL服务的服务名为postgres,配置中误写为Python应用的服务名clover,flyway根本无法定位到数据库服务
  2. 端口配置错误:5433是PostgreSQL映射到宿主机的端口,容器内部网络访问PostgreSQL直接使用容器默认监听的5432端口即可,5433端口在容器内网不可达
  3. 连接串未指定目标数据库:JDBC URL末尾没有填写要连接的数据库名,即使地址端口正确也无法连接到提前创建的clover数据库
  4. 配置中添加了connectRetries=60参数,flyway启动后会拿着错误的连接参数持续重试60次,重试间隔内无明显错误输出,最终表现为进程卡在Attaching to flyway阶段无响应。

另外Dockerfile中把flyway目录复制到clover应用镜像内属于冗余配置,flyway通过独立容器运行,该操作不影响服务功能但会增大clover镜像体积,可按需删除。

修复方案

修改docker-compose.yml中flyway服务的command配置,修正JDBC连接串为正确的内网地址、端口和目标数据库:

flyway:
  image: flyway/flyway
  container_name: flyway
  # 修正后的连接串:服务名postgres、内部端口5432、目标库clover
  command: -url=jdbc:postgresql://postgres:5432/clover -schemas=polk_housing -user=postgres -password=postgres -connectRetries=60 migrate
  volumes:
    - ./flyway/sql:/flyway/sql
  depends_on:
    - postgres

修改完成后执行docker-compose up -d重新创建服务即可。如果需要排查flyway运行细节,可执行docker logs -f flyway查看实时运行日志。

补充说明:如果迁移时提示polk_housing schema不存在,无需手动创建,flyway会自动创建配置中指定的schema。

内容的提问来源于stack exchange,提问作者Leustad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 16:31:06