computeConfigHash static method

String computeConfigHash(
  1. String image,
  2. String? version,
  3. String? containerName,
  4. List<String>? imageArgs,
  5. DockerRunOptions options,
)

A hash of what defines a reusable container: image, version, containerName, imageArgs and options, except the DockerRunOptions.labelSession label, so any session (including remote clients) finds the same container. See DockerRunOptions.reuse.

Implementation

static String computeConfigHash(
    String image,
    String? version,
    String? containerName,
    List<String>? imageArgs,
    DockerRunOptions options) {
  var labels = options.labels;
  if (labels != null && labels.containsKey(DockerRunOptions.labelSession)) {
    options = DockerRunOptions.fromJson({
      ...options.toJson(),
      'labels': {...labels}..remove(DockerRunOptions.labelSession),
    });
  }

  var key = [
    DockerHost.resolveImage(image, version),
    containerName ?? '',
    ...?imageArgs,
    '--',
    ...options.toArgs(),
  ].join('\u0000');

  // Two FNV-1a 32-bit hashes with different seeds: 64 bits.
  String fnv1a(int hash) {
    for (var c in key.codeUnits) {
      hash = ((hash ^ c) * 0x01000193) & 0xFFFFFFFF;
    }
    return hash.toRadixString(16).padLeft(8, '0');
  }

  return fnv1a(0x811c9dc5) + fnv1a(0x050c5d1f);
}