createContainer method

  1. @override
Future<ContainerInfos?> createContainer(
  1. String containerName,
  2. String imageName, {
  3. String? version,
  4. List<String>? ports,
  5. String? network,
  6. String? hostname,
  7. Map<String, String>? environment,
  8. Map<String, String>? volumes,
  9. bool cleanContainer = false,
  10. String? healthCmd,
  11. Duration? healthInterval,
  12. int? healthRetries,
  13. Duration? healthStartPeriod,
  14. Duration? healthTimeout,
  15. String? restart,
})
override

Creates a Docker containers with image and optional version.

Implementation

@override
Future<ContainerInfos?> createContainer(
  String containerName,
  String imageName, {
  String? version,
  List<String>? ports,
  String? network,
  String? hostname,
  Map<String, String>? environment,
  Map<String, String>? volumes,
  bool cleanContainer = false,
  String? healthCmd,
  Duration? healthInterval,
  int? healthRetries,
  Duration? healthStartPeriod,
  Duration? healthTimeout,
  String? restart,
}) async {
  if (isEmptyString(containerName, trim: true)) {
    return null;
  }

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

  var cmdArgs = containerInfos.args!;

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

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

  if (exitCode != 0) return null;

  containerInfos.id = await _readContainerID(
      containerInfos.containerName, containerInfos.idFile, process);

  return exitCode == 0 ? containerInfos : null;
}