etcd¶
Since v0.34.0
Introduction¶
The Testcontainers module for etcd.
Adding this module to your project dependencies¶
Please run the following command to add the etcd module to your Go dependencies:
go get github.com/testcontainers/testcontainers-go/modules/etcd
Usage example¶
ctx := context.Background()
etcdContainer, err := etcd.Run(ctx, "gcr.io/etcd-development/etcd:v3.5.14")
defer func() {
    if err := testcontainers.TerminateContainer(etcdContainer); err != nil {
        log.Printf("failed to terminate container: %s", err)
    }
}()
if err != nil {
    log.Printf("failed to start container: %s", err)
    return
}
Module Reference¶
Run function¶
- Since v0.34.0
The etcd module exposes one entrypoint function to create the etcd container, and this function receives three parameters:
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*EtcdContainer, error)
- context.Context, the Go context.
- string, the Docker image to use.
- testcontainers.ContainerCustomizer, a variadic argument for passing options.
Image¶
Use the second argument in the Run function to set a valid Docker image.
In example: Run(context.Background(), "gcr.io/etcd-development/etcd:v3.5.14").
Container Options¶
When starting the etcd container, you can pass options in a variadic way to configure it.
WithAdditionalArgs¶
- Since v0.34.0
You can pass additional arguments to the etcd container by using the WithAdditionalArgs option. The arguments are passed to the CMD of the etcd container.
WithDataDir¶
- Since v0.34.0
You can set the data directory for the etcd container by using the WithDataDir boolean option. The data directory where the etcd data is stored is /data.etcd.
WithNodes¶
- Since v0.34.0
You can set the number of nodes for the etcd cluster by using the WithNodes option, passing the node names for each of the nodes. Single-node clusters are not allowed,
for that reason the functional option receives three string arguments: the first node, the second node, and a variadic argument for the rest of the nodes.
The module starts a container for each node, having the first node a reference to the other nodes. E.g. WithNodes("etcd-1", "etcd-2"), WithNodes("etcd-1", "etcd-2", "etcd-3") and so on.
The module creates a Docker network for the etcd cluster, and the nodes are connected to this network, so that they can communicate with each other through the network.
WithClusterToken¶
- Since v0.34.0
Sets the cluster token for the etcd cluster. The cluster token is used to identify the etcd cluster. The default value is mys3cr3ttok3n.
The etcd container holds a reference to the cluster token, so you can use it with e.g. ctr.ClusterToken.
The following options are exposed by the testcontainers package.
Basic Options¶
- WithExposedPortsSince v0.37.0
- WithEnvSince v0.29.0
- WithWaitStrategySince v0.20.0
- WithAdditionalWaitStrategySince v0.38.0
- WithWaitStrategyAndDeadlineSince v0.20.0
- WithAdditionalWaitStrategyAndDeadlineSince v0.38.0
- WithEntrypointSince v0.37.0
- WithEntrypointArgsSince v0.37.0
- WithCmdSince v0.37.0
- WithCmdArgsSince v0.37.0
- WithLabelsSince v0.37.0
Lifecycle Options¶
- WithLifecycleHooksSince v0.38.0
- WithAdditionalLifecycleHooksSince v0.38.0
- WithStartupCommandSince v0.25.0
- WithAfterReadyCommandSince v0.28.0
Files & Mounts Options¶
- WithFilesSince v0.37.0
- WithMountsSince v0.37.0
- WithTmpfsSince v0.37.0
- WithImageMountSince v0.37.0
Build Options¶
- WithDockerfileSince v0.37.0
Logging Options¶
- WithLogConsumersSince v0.28.0
- WithLogConsumerConfigSince v0.38.0
- WithLoggerSince v0.29.0
Image Options¶
- WithAlwaysPullSince v0.38.0
- WithImageSubstitutorsSince v0.26.0
- WithImagePlatformSince v0.38.0
Networking Options¶
- WithNetworkSince v0.27.0
- WithNetworkByNameSince v0.38.0
- WithBridgeNetworkSince v0.38.0
- WithNewNetworkSince v0.27.0
Advanced Options¶
- WithHostPortAccessSince v0.31.0
- WithConfigModifierSince v0.20.0
- WithHostConfigModifierSince v0.20.0
- WithEndpointSettingsModifierSince v0.20.0
- CustomizeRequestSince v0.20.0
- WithNameSince v0.38.0
- WithNoStartSince v0.38.0
- WithProviderSince v0.39.0
Experimental Options¶
- WithReuseByNameSince v0.37.0
Container Methods¶
The etcd container exposes the following methods:
ClientEndpoint¶
- Since v0.34.0
Returns the client endpoint for the etcd container and an error, if any. In the case of a cluster, it returns the client endpoint for the first node.
PeerEndpoint¶
- Since v0.34.0
Returns the peer endpoint for the etcd container and an error, if any. In the case of a cluster, it returns the peer endpoint for the first node.