static int Main(string[] args) { var commandLineApplication = new CommandLineApplication(throwOnUnexpectedArg: false); var project = commandLineApplication.Option( "-p |--project <project>", "The path to the project to dockerize. Only required if the multiple projects exist " + "in a directory or the project is not in the current working directory.", CommandOptionType.SingleValue); var configuration = commandLineApplication.Option( "-c |--configuration <configuration>", "The configuration in which to publish the project. " + "Passed directly to 'dotnet publish -c <configuration>'. Defaults to Release.", CommandOptionType.SingleValue); var tag = commandLineApplication.Option( "-t |--tag <tag>", "The desired tag name of the created image. Will be directly passed to " + "docker build -t, see docker build --help for more info. Defaults to the project name.", CommandOptionType.SingleValue); var baseRid = commandLineApplication.Option( "-r |--runtime <RID>", "The RID of the specified Base Docker image. Defaults to \"linux-x64\".", CommandOptionType.SingleValue); var baseImage = commandLineApplication.Option( "-i |--image <image>", "The base docker image used for the generated docker file. If you change this from the default, be sure to" + "update BaseRid if appropriate. Defaults to \"microsoft/dotnet:2.0-runtime\".", CommandOptionType.SingleValue); var username = commandLineApplication.Option( "-u |--user <username>", "Generate a user with name <username> and set it to the current user inside the container. \n" + "It is recommended to run production containers not as root for security.", CommandOptionType.SingleValue); commandLineApplication.HelpOption("-? | -h | --help"); commandLineApplication.OnExecute(() => { var projectName = GetProjectName(Environment.CurrentDirectory, project); var config = new DockerizeConfiguration(projectName, configuration.Value(), tag.Value(), baseRid.Value(), baseImage.Value(), username.Value()); return(Run(config)); }); return(commandLineApplication.Execute(args)); }
static int Run(DockerizeConfiguration config) { var projectDirectory = Environment.CurrentDirectory; var dockerizeBaseDir = Path.Combine(projectDirectory, "bin", "dockerize"); var publishOutDirectory = Path.Combine(dockerizeBaseDir, "publish"); var dockerfilePath = Path.Combine(dockerizeBaseDir, "Dockerfile"); var publish = Command.Create("dotnet", new[] { "publish", "-o", publishOutDirectory, "-r", config.BaseRid, "-c", config.BuildConfiguration }); var publishResult = publish.WorkingDirectory(projectDirectory).ForwardStdErr().ForwardStdOut().Execute(); if (publishResult.ExitCode != 0) { return(publishResult.ExitCode); } var publishOutputDepsJsons = Directory.EnumerateFiles(publishOutDirectory, "*.deps.json").ToList(); if (publishOutputDepsJsons.Count > 1) { throw new GracefulException(string.Format( "Multiple output programs were found in {0}. Please try removing bin obj directories and retrying.", publishOutDirectory)); } if (!publishOutputDepsJsons.Any()) { throw new GracefulException(string.Format( "No output programs were found in {0}. Please examine 'dotnet publish' results and try again.", publishOutDirectory)); } var outputBinaryName = Path.GetFileNameWithoutExtension(publishOutputDepsJsons.Single()).Replace(".deps", ""); var dockerfile = DockerfileTemplate.Generate(config, outputBinaryName); File.WriteAllText(dockerfilePath, dockerfile); var dockerBuild = Command.Create("docker", new[] { "build", "-t", config.GeneratedImageTag, "." }).WorkingDirectory(dockerizeBaseDir).ForwardStdErr() .ForwardStdOut(); var dockerResult = dockerBuild.Execute(); return(dockerResult.ExitCode); }