createService method
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,
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);
}