createLabels function

Map<String, String> createLabels(
  1. String image,
  2. Map<String, String>? labels
)

Returns the standard set of Docker labels for a container or network.

Merges the caller-supplied labels map with the four built-in testcontainers labels:

Label key Value
org.testcontainers 'true'
org.testcontainers.version tcVersion
org.testcontainers.lang 'dart'
org.testcontainers.session-id sessionId (omitted for ryuk)

Parameters:

  • image — the image name being used. When image matches the configured ryuk image, the session-id label is omitted so that ryuk itself is not tracked by another ryuk instance.
  • labels — optional caller-supplied labels. May be null (treated as empty). Must not contain any key that starts with org.testcontainers — doing so throws an ArgumentError.

Throws ArgumentError if any key in labels starts with the reserved org.testcontainers namespace.

Implementation

Map<String, String> createLabels(
  String image,
  Map<String, String>? labels,
) {
  final effective = labels ?? const <String, String>{};
  for (final k in effective.keys) {
    if (k.startsWith(testcontainersNamespace)) {
      throw ArgumentError(
        'The org.testcontainers namespace is reserved for internal use',
      );
    }
  }
  return {
    ...effective,
    labelLang: labelLangValue,
    labelTestcontainers: 'true',
    labelVersion: tcVersion,
    if (image != testcontainersConfig.ryukImage) labelSessionId: sessionId,
  };
}