Skip to content

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

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

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

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

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

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

Lifecycle Options

Files & Mounts Options

Build Options

Logging Options

Image Options

Networking Options

Advanced Options

Experimental Options

Container Methods

The etcd container exposes the following methods:

ClientEndpoint

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

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.