KeycloakContainer class

Keycloak in a container in development mode, with an admin user and optionally a realm imported from a JSON export, ready for the security tests: the issuer URI of the realm is what an application's @ServiceConnection() receives.

As a @ServiceConnection() it sets spring.security.oauth2.resourceserver.jwt.issuer-uri (a resource server validating Keycloak's tokens) and spring.security.oauth2.client.provider.<id>.issuer-uri (a client logging in through it; withProviderId names the provider, keycloak by default), both http://host:port/realms/<realm>. The realm is the one of the imported file, else master.

@Container()
@ServiceConnection()
static final keycloak = KeycloakContainer('quay.io/keycloak/keycloak:26.8')
    .withRealmImport('../example/keycloak/neon-realm.json');

Testcontainers (Java): KeycloakContainer of the community module dasniko/testcontainers-keycloak (withRealmImportFile, getAuthServerUrl()).

Inheritance
Implemented types

Constructors

KeycloakContainer([String image = 'quay.io/keycloak/keycloak:26.8'])
Keycloak from image (quay.io/keycloak/keycloak:26.8 unless given), started with start-dev, with the admin user admin/admin.

Properties

adminPassword → String
The admin user's password.
no setter
adminUsername → String
The admin user.
no setter
authServerUrl → String
The server's address, http://host:port.
no setter
connectionProperties → Map<String, String>
The properties that point an application at the started container, such as spring.datasource.url. Read after the container started: the values hold its host and mapped ports.
no setteroverride
containerId → String
The container's id; throws before start.
no setterinherited
docker → DockerClient
The client of the daemon the container runs on.
no setterinherited
exposedPorts → List<int>
The container ports published with withExposedPorts.
no setterinherited
hashCode → int
The hash code for this object.
no setterinherited
host → String
The host the published ports are reached on: 127.0.0.1 for a local daemon.
no setterinherited
image → String
The image the container is created from.
finalinherited
isStarted → bool
Whether start succeeded and stop was not called since.
no setterinherited
issuerUri → String
The realm's issuer URI, http://host:port/realms/<realm>: the iss of its tokens.
no setter
realm → String
The imported realm, or master.
no setter
remaining → Duration
How long the strategy may still take; it is the container's startup timeout, less the time already spent.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
waitStrategy → WaitStrategy
The wait strategy, for modules that extend it.
no setterinherited

Methods

baseUrl(int port, {String scheme = 'http'}) → String
scheme://host:port of the container's port: http unless scheme says otherwise.
inherited
execInContainer(List<String> command) → Future<DockerExecResult>
Runs command in the started container and waits for it.
inherited
getLogs() → Future<String>
Everything the container printed so far, standard output and error together.
inherited
inspect() → Future<DockerContainerState>
The container's state, as the daemon reports it now.
inherited
logs() → Stream<DockerLogLine>
The container's output from its start, following it until it stops.
inherited
mappedPort(int port) → int
The port on host that the container's port is published on; the port must be listed in withExposedPorts, and the container started.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
poll(String description, Future<bool> check(), {Duration interval = const Duration(milliseconds: 100)}) → Future<void>
Calls check every interval until it returns true.
inherited
start() → Future<void>
Pulls the image if it is missing, creates the container, copies the files in, starts it and waits until it is ready.
inherited
stop() → Future<void>
Removes the container (killing it when it still runs). A container that is not started, or was found again through withReuse, is left as it is.
inherited
toString() → String
A string representation of this object.
override
waitingFor(WaitStrategy strategy) → GenericContainer
Decides when the started container counts as ready: see Wait. By default Wait.forListeningPort().
inherited
withAdminCredentials(String username, String password) → KeycloakContainer
Sets the admin user created at the first start (KC_BOOTSTRAP_ADMIN_USERNAME, KC_BOOTSTRAP_ADMIN_PASSWORD).
withCommand(List<String> command) → GenericContainer
Replaces the image's default command (CMD) with command.
inherited
withCopyContent(Object content, String containerPath, {int mode = 420}) → GenericContainer
Creates the file containerPath with content (a String or bytes) in the container before it starts.
inherited
withCopyFile(String hostPath, String containerPath, {int mode = 420}) → GenericContainer
Copies the file at hostPath to containerPath in the container before it starts. A relative hostPath starts at the working directory (the package, under dart test); the file is read at start. mode is the permission (0644 by default, as 0x1A4).
inherited
withEntrypoint(List<String> entrypoint) → GenericContainer
Replaces the image's entry point with entrypoint.
inherited
withEnv(String name, String value) → GenericContainer
Sets the environment variable name.
inherited
withEnvironment(Map<String, String> variables) → GenericContainer
Sets several environment variables.
inherited
withExposedPorts(List<int> ports) → GenericContainer
Publishes the container's TCP ports on random free ports of the host; mappedPort says which.
inherited
withExtraHost(String host, String address) → GenericContainer
Adds host with address to the container's hosts file; the address host-gateway is the machine the daemon runs on (withExtraHost('host.docker.internal', 'host-gateway') makes that name work on Linux as it does on Docker Desktop).
inherited
withHealthCheck(List<String> test, {Duration interval = const Duration(seconds: 1), Duration timeout = const Duration(seconds: 3), int retries = 10}) → GenericContainer
Gives the container a health check, for Wait.forHealthcheck() and for images that define none: test is the command in exec form (['CMD-SHELL', 'pg_isready -U test']), run every interval; after retries failures the container is unhealthy.
inherited
withLabel(String name, String value) → GenericContainer
Adds the label name=value to the container.
inherited
withNetwork(Network network) → GenericContainer
Puts the container on network, where the other containers of it reach this one by its withNetworkAliases names and the ports it listens on. The network is created when the first container on it starts.
inherited
withNetworkAliases(List<String> aliases) → GenericContainer
The names the other containers of its withNetwork network reach this container by.
inherited
withProviderId(String id) → KeycloakContainer
Names the OAuth2 client provider that connectionProperties sets the issuer URI of (spring.security.oauth2.client.provider.<id>).
withRealmImport(String hostPath) → KeycloakContainer
Imports the realm exported to the JSON file at hostPath when Keycloak starts (--import-realm); the container is ready when that realm answers, and realm is its name.
withReuse(bool reuse) → GenericContainer
Keeps the container alive after the tests, for the next run to use again (off by default, and ignored when the CI environment variable is set).
inherited
withStartupTimeout(Duration timeout) → GenericContainer
How long the wait strategy may take (60 seconds by default); the image pull is not counted.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Constants

port → const int
Keycloak's HTTP port in the container.