createService method

Future<Service?> createService(
  1. String serviceName,
  2. String imageName, {
  3. String? version,
  4. int? replicas,
  5. List<String>? ports,
  6. String? network,
  7. String? hostname,
  8. Map<String, String>? environment,
  9. Map<String, String>? volumes,
  10. String? healthCmd,
  11. Duration? healthInterval,
  12. int? healthRetries,
  13. Duration? healthStartPeriod,
  14. Duration? healthTimeout,
})

Creates a Docker service with serviceName, image and optional version. Note that the Docker Daemon should be in Swarm mode.

Implementation

Future<Service?> createService(
  String serviceName,
  String imageName, {
  String? version,
  int? replicas,
  List<String>? ports,
  String? network,
  String? hostname,
  Map<String, String>? environment,
  Map<String, String>? volumes,
  String? healthCmd,
  Duration? healthInterval,
  int? healthRetries,
  Duration? healthStartPeriod,
  Duration? healthTimeout,
}) async {
  if (isEmptyString(serviceName, trim: true)) {
    return null;
  }

  var containerInfos = buildContainerArgs(
    'create',
    imageName,
    version,
    serviceName,
    ports,
    network,
    hostname,
    environment,
    volumes,
    false,
    healthCmd,
    healthInterval,
    healthRetries,
    healthStartPeriod,
    healthTimeout,
    null,
  );

  var cmdArgs = containerInfos.args!;

  cmdArgs.removeLast();

  if (replicas != null && replicas > 1) {
    cmdArgs.add('--replicas');
    cmdArgs.add('$replicas');
  }

  cmdArgs.add(containerInfos.image!);

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

  var process = await command('service', cmdArgs);
  if (process == null) return null;

  var exitCodeOK = await process.waitExitAndConfirm(0);
  if (!exitCodeOK) return null;

  var id = await getServiceIDByName(containerInfos.containerName);

  return Service(
      this,
      containerInfos.containerName,
      id,
      containerInfos.image,
      containerInfos.ports,
      containerInfos.containerNetwork,
      containerInfos.containerHostname);
}