如何使用golang.testcontainers.org在Go代码中启动ArangoDB测试容器并解决启动超时问题?
The "context deadline exceeded" error you're hitting is almost certainly because the ArangoDB container requires specific environment variables to skip interactive setup—without these, the container gets stuck waiting for you to set an initial password, so Testcontainers never sees the "Waiting for connections" log line you're waiting for.
Here's how to fix your ContainerRequest with the right environment variables:
Option 1: Disable Authentication (Great for Testing)
If you don't need auth in your test environment, set the ARANGO_NO_AUTH variable to 1 to skip the password setup entirely:
arangoContainerRequest := testcontainers.ContainerRequest{ Image: "arangodb/arangodb:3.7.5", Name: "arango", ExposedPorts: []string{"8529/tcp"}, Env: map[string]string{ "ARANGO_NO_AUTH": "1", // Disable authentication for testing }, WaitingFor: wait.ForLog("Waiting for connections").WithStartupTimeout(time.Minute * 15), }
Option 2: Set a Predefined Password
If you need authentication enabled, use ARANGO_ROOT_PASSWORD to set a default password during startup:
arangoContainerRequest := testcontainers.ContainerRequest{ Image: "arangodb/arangodb:3.7.5", Name: "arango", ExposedPorts: []string{"8529/tcp"}, Env: map[string]string{ "ARANGO_ROOT_PASSWORD": "testpassword", // Set a known root password }, WaitingFor: wait.ForLog("Waiting for connections").WithStartupTimeout(time.Minute * 15), }
Full Working Example
Make sure you're also properly initializing your context (you might have this already, but it's worth including for completeness):
package main import ( "context" "time" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" ) func main() { ctx := context.Background() arangoContainerRequest := testcontainers.ContainerRequest{ Image: "arangodb/arangodb:3.7.5", Name: "arango", ExposedPorts: []string{"8529/tcp"}, Env: map[string]string{ "ARANGO_NO_AUTH": "1", // Use this or ARANGO_ROOT_PASSWORD }, WaitingFor: wait.ForLog("Waiting for connections").WithStartupTimeout(time.Minute * 15), } arangoContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ContainerRequest: arangoContainerRequest, Started: true, }) if err != nil { panic(err) // Handle error properly in real code } defer arangoContainer.Terminate(ctx) // Get the mapped port to connect to ArangoDB port, err := arangoContainer.MappedPort(ctx, "8529/tcp") if err != nil { panic(err) } println("ArangoDB running on port:", port.Port()) }
Additional Debug Tips
- If you still hit timeouts, check the container logs manually to see where it's getting stuck. You can do this by removing the
defer arangoContainer.Terminate(ctx)line temporarily, then runningdocker logs arangoafter the timeout occurs. - For more reliable waiting, you could use
wait.ForHTTP("/_api/version")instead of waiting for a log line—this verifies the API is actually reachable:WaitingFor: wait.ForHTTP("/_api/version").WithPort("8529/tcp").WithStartupTimeout(time.Minute * 15),
内容的提问来源于stack exchange,提问作者Aditya Vashist

