Reusable Containers (Experimental)

InfluxDB Module

qianmoQqianmoQ· 更新于 2026-10-13· 阅读 11 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

InfluxDB Module

Testcontainers module for InfluxData InfluxDB.

Important note

There are breaking changes in InfluxDB 2.x. For more information refer to the main documentation. You can find more information about the official InfluxDB image on Docker Hub.

InfluxDB 2.x usage example

Running a InfluxDBContainer as a stand-in for InfluxDB in a test:

Create an InfluxDB container

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:2.0.7")
)

The InfluxDB instance will be setup with the following data:

PropertyDefault Value
usernametest-user
passwordtest-password
organizationtest-org
buckettest-bucket
retention0 (infinite)
adminToken-

For more details about the InfluxDB setup, please visit the official InfluxDB documentation.

It is possible to overwrite the default property values. Create a container with InfluxDB admin token:

Create an InfluxDB container with admin token

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:2.0.7")
)
    .withAdminToken(ADMIN_TOKEN)

Or create a container with custom username, password, bucket, organization, and retention time:

Create an InfluxDB container with custom settings

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:2.0.7")
)
    .withUsername(USERNAME)
    .withPassword(PASSWORD)
    .withOrganization(ORG)
    .withBucket(BUCKET)
    .withRetention(RETENTION);

The following code snippet shows how you can create an InfluxDB Java client:

Create an InfluxDB Java client

public static InfluxDBClient createClient(final InfluxDBContainer<?> influxDBContainer) {
    final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions
        .builder()
        .url(influxDBContainer.getUrl())
        .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray())
        .bucket(influxDBContainer.getBucket())
        .org(influxDBContainer.getOrganization())
        .build();
    return InfluxDBClientFactory.create(influxDBClientOptions);
}

Hint

You can find the latest documentation about the InfluxDB 2.x Java client here.

InfluxDB 1.x usage example

Running a InfluxDBContainer as a stand-in for InfluxDB in a test with default env variables:

Create an InfluxDB container

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:1.4.3")
)

The InfluxDB instance will be setup with the following data:

PropertyDefault Value
usernametest-user
passwordtest-password
authEnabledtrue
adminadmin
adminPasswordpassword
database-

It is possible to overwrite the default values. For instance, creating an InfluxDB container with a custom username, password, and database name:

Create an InfluxDB container with custom settings

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:1.4.3")
)
    .withDatabase(DATABASE)
    .withUsername(USER)
    .withPassword(PASSWORD)

In the following example you will find a snippet to create an InfluxDB client using the official Java client:

Create an InfluxDB Java client

public static InfluxDB createInfluxDBWithUrl(final InfluxDBContainer<?> container) {
    InfluxDB influxDB = InfluxDBFactory.connect(
        container.getUrl(),
        container.getUsername(),
        container.getPassword()
    );
    influxDB.setDatabase(container.getDatabase());
    return influxDB;
}

Hint

You can find the latest documentation about the InfluxDB 1.x Java client here.

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

Gradle

testImplementation "org.testcontainers:testcontainers-influxdb:2.0.5"

Maven

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers-influxdb</artifactId>
    <version>2.0.5</version>
    <scope>test</scope>
</dependency>

Hint

Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency.

评论

登录后参与评论

正在加载评论…