build function

Image build({
  1. required String pathToDockerFile,
  2. required String imageName,
  3. required String version,
  4. bool clean = false,
  5. bool pull = false,
  6. List<String> buildArgs = const <String>[],
  7. String? repository,
  8. String? workingDirectory,
  9. bool showProgress = true,
  10. bool buildx = false,
})

Builds a docker image from the docker file at pathToDockerFile Giving it a name of imageName:version If you set the optional clean argument to true then the build will be run with the --no-cache flag set.

You can pass additional docker build args in the buildArgs argument. The args should be passed in the form 'arg=value'

If passed, the workingDirectory is used when running the docker build command. This is important as it affects what files the docker build command will add to its context. If not passed then the current working directory will be used.

Pass showProgress == false to only show errors.

Implementation

///
/// If passed, the [workingDirectory] is used when running the
/// docker build command. This is important as it affects what
/// files the docker build command will add to its context.
/// If not passed then the current working directory will be used.
///
/// Pass [showProgress] == false to only show errors.
Image build(
    {required String pathToDockerFile,
    required String imageName,
    required String version,
    bool clean = false,
    bool pull = false,
    List<String> buildArgs = const <String>[],
    String? repository,
    String? workingDirectory,
    bool showProgress = true,
    bool buildx = false}) {
  var cleanArg = '';
  if (clean) {
    cleanArg = ' --no-cache';
  }
  var pullArg = '';
  if (pull) {
    pullArg = ' --pull';
  }

  workingDirectory ??= pwd;

  final tag =
      tagName(repository: repository, imageName: imageName, version: version);

  final buildArgList = StringBuffer();
  if (buildArgs.isNotEmpty) {
    for (final arg in buildArgs) {
      buildArgList.write('--build-arg $arg ');
    }
  }
  final progress = showProgress ? Progress.print() : Progress.printStdErr();

  final buildxFlag = buildx ? 'buildx' : '';
  'docker $buildxFlag build $pullArg $buildArgList $cleanArg -t $tag'
          ' -f $pathToDockerFile .'
      .start(workingDirectory: workingDirectory, progress: progress);

  return Image.fromName(tag);
}