Example #1
0
        public static void ApplyContainerDefaults(ApplicationBuilder application, DockerFileServiceBuilder project, ContainerInfo container)
        {
            if (container.ImageName == null && application.Registry?.Hostname == null)
            {
                container.ImageName ??= project.Name.ToLowerInvariant();
            }
            else if (container.ImageName == null && application.Registry?.Hostname != null)
            {
                container.ImageName ??= $"{application.Registry?.Hostname}/{project.Name.ToLowerInvariant()}";
            }

            container.ImageTag ??= "latest";
        }
Example #2
0
        public static async Task BuildContainerImageFromDockerFileAsync(OutputContext output, ApplicationBuilder application, DockerFileServiceBuilder containerService, ContainerInfo container)
        {
            if (output is null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            if (application is null)
            {
                throw new ArgumentNullException(nameof(application));
            }

            if (containerService is null)
            {
                throw new ArgumentNullException(nameof(containerService));
            }

            if (containerService.DockerFile is null)
            {
                throw new ArgumentNullException(nameof(containerService.DockerFile));
            }

            var dockerFileInfo   = new FileInfo(containerService.DockerFile);
            var contextDirectory = containerService.DockerFileContext ?? dockerFileInfo.DirectoryName;
            var dockerFilePath   = Path.Combine(dockerFileInfo.DirectoryName !, "Dockerfile");

            output.WriteDebugLine($"Using existing Dockerfile '{dockerFilePath}'.");

            output.WriteDebugLine("Running 'docker build'.");
            output.WriteCommandLine("docker", $"build \"{contextDirectory}\" -t {container.ImageName}:{container.ImageTag} -f \"{dockerFilePath}\"");
            var capture  = output.Capture();
            var exitCode = await application.ContainerEngine.ExecuteAsync(
                $"build \"{contextDirectory}\" -t {container.ImageName}:{container.ImageTag} -f \"{dockerFilePath}\"",
                new FileInfo(containerService.DockerFile).DirectoryName,
                stdOut : capture.StdOut,
                stdErr : capture.StdErr);

            output.WriteDebugLine($"Done running 'docker build' exit code: {exitCode}");
            if (exitCode != 0)
            {
                throw new CommandException("'docker build' failed.");
            }

            output.WriteInfoLine($"Created Docker Image: '{container.ImageName}:{container.ImageTag}'");
            containerService.Outputs.Add(new DockerImageOutput(container.ImageName !, container.ImageTag !));
        }