run method

  1. @override
Future<DockerRunner> run(
  1. String image, {
  2. String? version,
  3. List<String>? imageArgs,
  4. String? containerName,
  5. List<String>? ports,
  6. String? network,
  7. String? hostname,
  8. Map<String, String>? environment,
  9. Map<String, String>? volumes,
  10. bool cleanContainer = true,
  11. String? healthCmd,
  12. Duration? healthInterval,
  13. int? healthRetries,
  14. Duration? healthStartPeriod,
  15. Duration? healthTimeout,
  16. String? restart,
  17. bool? outputAsLines = true,
  18. int? outputLimit,
  19. OutputReadyFunction? stdoutReadyFunction,
  20. OutputReadyFunction? stderrReadyFunction,
  21. OutputReadyType? outputReadyType,
})
override

Runs a Docker containers with image and optional version.

Implementation

@override
Future<DockerRunner> run(
  String image, {
  String? version,
  List<String>? imageArgs,
  String? containerName,
  List<String>? ports,
  String? network,
  String? hostname,
  Map<String, String>? environment,
  Map<String, String>? volumes,
  bool cleanContainer = true,
  String? healthCmd,
  Duration? healthInterval,
  int? healthRetries,
  Duration? healthStartPeriod,
  Duration? healthTimeout,
  String? restart,
  bool? outputAsLines = true,
  int? outputLimit,
  OutputReadyFunction? stdoutReadyFunction,
  OutputReadyFunction? stderrReadyFunction,
  OutputReadyType? outputReadyType,
}) async {
  outputAsLines ??= true;

  outputReadyType ??= DockerHost.resolveOutputReadyType(
      stdoutReadyFunction, stderrReadyFunction);

  stdoutReadyFunction ??= (outputStream, data) => true;
  stderrReadyFunction ??= (outputStream, data) => true;

  var instanceID = DockerProcess.incrementInstanceID();

  if (isEmptyString(containerName, trim: true)) {
    containerName = 'docker_commander-$session-$instanceID';
  }

  var containerInfos = buildContainerArgs(
    'run',
    image,
    version,
    containerName!,
    ports,
    network,
    hostname,
    environment,
    volumes,
    cleanContainer,
    healthCmd,
    healthInterval,
    healthRetries,
    healthStartPeriod,
    healthTimeout,
    restart,
    addCIDFile: true,
  );

  var cmdArgs = containerInfos.args!;

  if (imageArgs != null) {
    cmdArgs.addAll(imageArgs);
  }

  _log.info('run[CMD]>\t$dockerBinaryPath ${cmdArgs.join(' ')}');

  var process = await Process.start(dockerBinaryPath!, cmdArgs);

  var containerNetwork = containerInfos.containerNetwork;

  var runner = DockerRunnerLocal(
      this,
      instanceID,
      containerInfos.containerName,
      containerInfos.image,
      process,
      containerInfos.idFile,
      containerInfos.ports,
      containerNetwork,
      containerInfos.containerHostname,
      outputAsLines,
      outputLimit,
      stdoutReadyFunction,
      stderrReadyFunction,
      outputReadyType);

  _runners[instanceID] = runner;
  _processes[instanceID] = runner;

  var ok = await _initializeAndWaitReady(runner, () async {
    if (containerNetwork != null) {
      await _configureContainerNetwork(containerNetwork, runner);
    }
  });

  if (ok) {
    _log.info('Runner[$ok]: $runner');
  }

  return runner;
}