Quarkus开发模式下PostgreSQL容器无法随服务关闭问题求助
解决Quarkus DevServices PostgreSQL容器无法随应用关闭的问题
问题背景
使用Quarkus 2.16.7.Final,数据源配置为PostgreSQL 12.4,DevServices自定义配置如下:
datasource: db-kind: postgresql devservices: image-name: artifactory.{"image-name"}/postgres:12.4 port: 5000
执行mvn quarkus:dev启动应用后,关闭/终止服务器时PostgreSQL容器持续运行,需手动停止;但Keycloak与Ryuk容器可正常随服务关闭,设置testcontainers.reuse.enable=false无效。
解决方案
1. 移除固定端口配置
指定固定端口port: 5000会导致Testcontainers无法将容器与Quarkus应用的生命周期绑定,Ryuk进程无法识别该容器为DevServices管理的实例,进而不会自动清理。建议删除该配置,让DevServices自动分配端口:
datasource: db-kind: postgresql devservices: image-name: artifactory.{"image-name"}/postgres:12.4
2. 强制启用容器清理
添加quarkus.datasource.devservices.remove-containers=true配置,强制DevServices在应用终止时销毁关联容器,覆盖默认的复用逻辑:
datasource: db-kind: postgresql devservices: image-name: artifactory.{"image-name"}/postgres:12.4 remove-containers: true
3. 检查Docker权限(Linux环境)
若运行在Linux系统下,Ryuk进程需要足够的Docker操作权限才能管理容器。可通过docker ps验证当前用户是否有权限操作Docker,若没有权限,可尝试使用sudo mvn quarkus:dev启动应用,验证是否为权限导致的清理失败。
4. 临时自动清理脚本(Workaround)
如果上述方案无法立即生效,可在pom.xml中添加Maven插件,启动前自动停止遗留的PostgreSQL容器:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>stop-postgres-container</id> <phase>pre-clean</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>docker</executable> <arguments> <argument>stop</argument> <argument>$(docker ps -q --filter ancestor=artifactory.{"image-name"}/postgres:12.4)</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build>
执行mvn clean quarkus:dev时,会自动停止目标PostgreSQL容器。
内容的提问来源于stack exchange,提问作者Kanstantsin Voitau
相关产品推荐
相关产品推荐

