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 {
  ports = DockerHost.normalizeMappedPorts(ports);

  var response = await _httpClient.getJSON('create', parameters: {
    'image': imageName,
    if (version != null) 'version': version,
    'name': containerName,
    if (ports != null) 'ports': ports.join(','),
    if (network != null) 'network': network,
    if (hostname != null) 'hostname': hostname,
    if (environment != null) 'environment': encodeQueryString(environment),
    if (volumes != null) 'volumes': encodeQueryString(volumes),
    'cleanContainer': '$cleanContainer',
    if (healthCmd != null) 'healthCmd': healthCmd,
    if (healthInterval != null)
      'healthInterval': '${healthInterval.inMilliseconds}',
    if (healthRetries != null) 'healthRetries': '$healthRetries',
    if (healthStartPeriod != null)
      'healthStartPeriod': '${healthStartPeriod.inMilliseconds}',
    if (healthTimeout != null)
      'healthTimeout': '${healthTimeout.inMilliseconds}',
    if (restart != null) 'restart': restart,
  }) as Map?;

  if (response == null) return null;

  containerName = response['containerName'] as String;
  var id = response['id'] as String?;
  var image = response['image'] as String?;
  var portsList = response['ports'] as List?;
  network = response['network'] as String?;
  hostname = response['hostname'] as String?;

  ports = portsList?.cast<String>().toList();

  return ContainerInfos(containerName, id, image, ports, network, hostname);
}