Could not find a valid Docker environment when using Gradle Shared Build Service (`ClassNotFoundException` when loading strategies)
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 25/100
- Issue 類型
- 缺陷
- 描述清晰度
- 需要釐清
- 活躍度
- 停滯
- 技術堆疊
- docker, java, mysql
- 領域
- build-system, databases, devops, testing
研究方向
先從報告中展示的 Gradle buildscript 和 MySQLContainerService 開始,接著使用 --stacktrace 重現 :startMySQLContainer,並檢查 ClassNotFoundException 以及 Docker 環境探索日誌。完成的標準是找出 shared-service classpath 或策略載入為何在不同執行之間有所差異,並確認針對所回報 build sequence 的已記錄或已測試解決方案。
由索引模型根據 Issue 內容生成。
描述
Discussed in https://github.com/testcontainers/testcontainers-java/discussions/9034
Originally posted by debapgithub July 30, 2024
Hi,
I am new to testcontainers and setting up for a gradle service where I wanted to achieve the following in sequence.
When gradle build command will run : ./gradlew clean build
- MySql Testcontainer should start
- DB migrations should apply against the testcontainer DB
- generateJooq task should execute against the testcontainer DB
- Run the integration tests against testcontainer
- At the end of the build process, testcotnainer should stop automatically.
I have tried starting MySql testcontainer as a gradle task and I was able to execute the following tasks properly except one issue due to which I have decided to switch to another approach where I can start the MySql testcontainer as a gradle Shared service so that it will automaticllay manage the testcontainer lifecycle.
Here is the code which will start the testcontainer as a gradle shared service and it worked fine as well. But next day when I tried running the gradle command, I started getting the below error.
Error
* What went wrong:
Execution failed for task ':startMySQLContainer'.
> Failed to create service 'mysqlContainerService'.
> Could not create an instance of type MySQLContainerService.
> Could not find a valid Docker environment. Please see logs and check configuration
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
Gradle Code
import org.testcontainers.containers.MySQLContainer;
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
buildscript {
repositories {
maven {
name repoName
url repoUrl
credentials() {
username = repoUsername
password = repoPassword
}
}
}
dependencies {
classpath "org.testcontainers:testcontainers:${dependencyManagement.importedProperties['testcontainers.version']}"
classpath "org.testcontainers:mysql:${dependencyManagement.importedProperties['testcontainers.version']}"
classpath "com.mysql:mysql-connector-j:${dependencyManagement.importedProperties['mysql.version']}"
}
}
// Here we register service for providing our database during the build.
gradle.sharedServices.registerIfAbsent('mysqlContainerService', MySQLContainerService) { spec ->
spec.parameters.databaseSchemaName.set(project.ext.databaseSchemaName)
}
/**
* Build service for providing database container.
*/
abstract class MySQLContainerService implements BuildService<MySQLContainerService.Params>, AutoCloseable {
interface Params extends BuildServiceParameters {
Property<String> getDatabaseSchemaName()
}
private final MySQLContainer mysqlContainer;
@javax.inject.Inject
MySQLContainerService(Params params) {
// Services are initialized lazily, on first request to them, so we start container immediately.
long startTime = System.currentTimeMillis()
println("DB Schema Name: ${params.getDatabaseSchemaName().get()}")
mysqlContainer = new MySQLContainer(MySQLContainer.IMAGE).withDatabaseName(params.getDatabaseSchemaName().get())
mysqlContainer.start()
long duration = System.currentTimeMillis() - startTime
println("MySQL Testcontainer Started successfully with URL: ${mysqlContainer.getJdbcUrl()}. Total time taken: ${duration} ms")
}
String getJdbcUrl() {
return mysqlContainer.getJdbcUrl()
}
String getUsername() {
return mysqlContainer.getUsername()
}
String getPassword() {
return mysqlContainer.getPassword()
}
String getDriverClassName() {
return mysqlContainer.getDriverClassName()
}
@Override
void close() {
// Ensure to stop container in the end
if (mysqlContainer != null) {
try {
mysqlContainer.stop()
println("MySQL container stopped successfully.")
} catch (Exception e) {
println("Failed to stop MySQL container: ${e.message}")
}
} else {
println("MySQL container is already null (was not started).")
}
}
}
tasks.register('startMySQLContainer') {
doLast {
gradle.sharedServices.getRegistrations().getByName('mysqlContainerService')
}
}
// Ensure the integrationTest uses the same container and stops it after
tasks.named('integrationTest').configure {
dependsOn 'startMySQLContainer'
doFirst {
def service = gradle.sharedServices.getRegistrations().getByName('mysqlContainerService').service.get()
systemProperty 'spring.datasource.url', service.getJdbcUrl()
systemProperty 'spring.datasource.username', service.getUsername()
systemProperty 'spring.datasource.password', service.getPassword()
systemProperty 'spring.datasource.driver-class-name', service.getDriverClassName()
}
doLast {
systemProperties.remove('spring.datasource.url')
systemProperties.remove('spring.datasource.username')
systemProperties.remove('spring.datasource.password')
systemProperties.remove('spring.datasource.driver-class-name')
}
}
Not sure whats going wrong and how it worked first time when I did this code.
Thanks in advance!!
-Deba
- 主要語言
- Java
- 星號
- 8.8k
- 分支
- 2k
- 平均合併
- 5 天 21 小時
- 30 天內合併 PR
- 15
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 提供 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
testcontainers/testcontainers-java 的其他 Issue
-
[Bug]: withStartupTimeout() changes the startup timeout of all containers that use the default wait strategy可能已有人在做 @aymenjamaaoui 於 1 天前認領。 未關閉type/bug
難度 2/5 1-3 小時 新手友好度 64/100
testcontainers/testcontainers-java#12171 ·
維護者通常 1 天內回覆
-
[Enhancement]: Document why singleton containers are required under Spring's test context caching可能已有人在做 @zakaullah075 於 95 天前認領。 未關閉type/enhancement
難度 1/5 1 小時以內 新手友好度 85/100
testcontainers/testcontainers-java#11967 · 1 則留言 ·
維護者通常 1 天內回覆
-
[Bug]: Cannot reuse Selenium BrowserWebDriverContainers under Linux可能已有人在做 @kdelay 於 116 天前認領。 未關閉type/bug
難度 2/5 1-3 小時 新手友好度 78/100
testcontainers/testcontainers-java#11941 ·
維護者通常 1 天內回覆
-
[Bug]: DockerDesktopClientProviderStrategy is always applicable可能已有人在做 @seonwooj0810 於 165 天前認領。 未關閉type/bug
難度 2/5 1-3 小時 新手友好度 74/100
testcontainers/testcontainers-java#11829 · 1 則留言 ·
維護者通常 1 天內回覆
-
[Enhancement]: cleanup document after removing junit4 support可能已有人在做 @SJvaca30 於 173 天前認領。 未關閉type/enhancement
難度 2/5 1-3 小時 新手友好度 65/100
testcontainers/testcontainers-java#11578 ·
維護者通常 1 天內回覆
查看 testcontainers/testcontainers-java 的全部 Issue
相似的 Issue
-
難度 1/5 1 小時以內 新手友好度 68/100
pact-foundation/pact-plugins#125 ·
-
難度 2/5 1-3 小時 新手友好度 66/100
opencollab/flexdock#28 · 1 則留言 ·
-
MVP
難度 2/5 1-3 小時 新手友好度 65/100
OpenConext/OpenConext-access#1145 · 1 則留言 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 85/100
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 75/100
SAP/ai-sdk-java#1206 ·
維護者通常 1 天內回覆