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(), "bitnami/etcd:latest")
.
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¶
WithExposedPorts
Since v0.37.0WithEnv
Since v0.29.0WithWaitStrategy
Since v0.20.0WithAdditionalWaitStrategy
Not available until the next release mainWithWaitStrategyAndDeadline
Since v0.20.0WithAdditionalWaitStrategyAndDeadline
Not available until the next release mainWithEntrypoint
Since v0.37.0WithEntrypointArgs
Since v0.37.0WithCmd
Since v0.37.0WithCmdArgs
Since v0.37.0WithLabels
Since v0.37.0
Lifecycle Options¶
WithLifecycleHooks
Not available until the next release mainWithAdditionalLifecycleHooks
Not available until the next release mainWithStartupCommand
Since v0.25.0WithAfterReadyCommand
Since v0.28.0
Files & Mounts Options¶
WithFiles
Since v0.37.0WithMounts
Since v0.37.0WithTmpfs
Since v0.37.0WithImageMount
Since v0.37.0
Build Options¶
WithDockerfile
Since v0.37.0
Logging Options¶
WithLogConsumers
Since v0.28.0WithLogConsumerConfig
Not available until the next release mainWithLogger
Since v0.29.0
Image Options¶
WithAlwaysPull
Not available until the next release mainWithImageSubstitutors
Since v0.26.0WithImagePlatform
Not available until the next release main
Networking Options¶
WithNetwork
Since v0.27.0WithNetworkByName
Not available until the next release mainWithBridgeNetwork
Not available until the next release mainWithNewNetwork
Since v0.27.0
Advanced Options¶
WithHostPortAccess
Since v0.31.0WithConfigModifier
Since v0.20.0WithHostConfigModifier
Since v0.20.0WithEndpointSettingsModifier
Since v0.20.0CustomizeRequest
Since v0.20.0WithName
Not available until the next release mainWithNoStart
Not available until the next release main
Experimental Options¶
WithReuseByName
Since 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.