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

如何使用golang.testcontainers.org在Go代码中启动ArangoDB测试容器并解决启动超时问题?

Fixing Timeout When Starting ArangoDB with Testcontainers in Go

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 running docker logs arango after 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 23:42:48