private string writeDockerfile(string dir, string exe, string[] args, Config config) { var dockerfilename = dir + "/Dockerfile"; if (!string.IsNullOrEmpty(config.Dockerfile)) { File.Copy(config.Dockerfile, dockerfilename); return(dockerfilename); } var instructions = new List <Instruction>(); instructions.Add(new Instruction("FROM", "debian:9")); instructions.Add(new Instruction("RUN", " apt-get update && apt-get -qq -y install libunwind8 libicu57 libssl1.0 liblttng-ust0 libcurl3 libuuid1 libkrb5-3 zlib1g")); // TODO: lots of things here are constant, figure out how to cache for perf? instructions.Add(new Instruction("COPY", string.Format("* /exe/", dir))); instructions.Add(new Instruction("CMD", string.Format("/exe/{0} {1}", exe, getArgs(args)))); var df = new Dockerfile(instructions.ToArray(), new Comment[0]); File.WriteAllText(dockerfilename, df.Contents()); return(dockerfilename); }
public void Test() { var dockerfile = new Dockerfile { Name = "debian", Tag = "stable" }; dockerfile.Run("apt-get update && apt-get install -y --force-yes apache2"); dockerfile.Expose(80, 433); dockerfile.AddVolumes("/var/www", "/var/log/apache2", "/etc/apache2"); dockerfile.SetEntrypoint("/usr/sbin/apache2ctl", new[] { "-D", "FOREGROUND" }); Assert.Equal( @"FROM debian:stable RUN apt-get update && apt-get install -y --force-yes apache2 EXPOSE 80 433 VOLUME [""/var/www"",""/var/log/apache2"",""/etc/apache2""] ENTRYPOINT [""/usr/sbin/apache2ctl"",""-D"",""FOREGROUND""]", dockerfile.ToString().Trim()); }
public void Build(string[] args) { var proc = Process.GetCurrentProcess(); var procName = proc.ProcessName; string exe = null; string dir = null; TextWriter o = config.Verbose ? Console.Out : null; TextWriter e = config.Quiet ? Console.Error : null; if (procName == "dotnet") { dir = "bin/release/netcoreapp2.0/debian.8-x64/publish"; Exec("dotnet", "publish -r debian.8-x64 -c release", stdout: o, stderr: e); //var dirInfo = new UnixDirectoryInfo(dir); var files = Directory.GetFiles(dir); foreach (var filePath in files) { var file = new FileInfo(filePath); if (file.Name.EndsWith(".runtimeconfig.json")) { exe = file.Name.Substring(0, file.Name.Length - ".runtimeconfig.json".Length); } } } else { exe = procName; var prog = proc.MainModule.FileName; dir = Directory.GetParent(prog).FullName; } var instructions = new List <Instruction>(); instructions.Add(new Instruction("FROM", "debian:9")); instructions.Add(new Instruction("RUN", " apt-get update && apt-get -qq -y install libunwind8 libicu57 libssl1.0 liblttng-ust0 libcurl3 libuuid1 libkrb5-3 zlib1g")); // TODO: lots of things here are constant, figure out how to cache for perf? instructions.Add(new Instruction("COPY", string.Format("* /exe/", dir))); instructions.Add(new Instruction("CMD", string.Format("/exe/{0} {1}", exe, getArgs(args)))); var df = new Dockerfile(instructions.ToArray(), new Comment[0]); var dockerfilename = dir + "/Dockerfile"; File.WriteAllText(dockerfilename, df.Contents()); var builder = getBuilder(); string imgName = (string.IsNullOrEmpty(config.Repository) ? exe : config.Repository); if (!string.IsNullOrEmpty(config.Version)) { imgName += ":" + config.Version; } if (!builder.Build(dockerfilename, imgName, stdout: o, stderr: e)) { Console.Error.WriteLine("Image build failed."); return; } if (config.Publish) { if (!builder.Push(imgName, stdout: o, stderr: e)) { Console.Error.WriteLine("Image push failed."); return; } } if (runtimeConfig == null) { return; } var exec = getExecutor(); var id = exec.Run(imgName, runtimeConfig); Console.CancelKeyPress += delegate { exec.Cancel(id); }; exec.Logs(id, Console.Out, Console.Error); }
public Image(Dockerfile file) { File = file; }
private string GetReadmeTagName(Dockerfile dockerFile) => dockerFile.Tags.First();
private string GetTagLink(Dockerfile dockerFile) => Normalize(GetReadmeTagName(dockerFile));
public Result <IGraph <IArtifact, Dependency> > Create(IEnumerable <Template> templates) { var graph = new Graph <IArtifact, Dependency>(); var nodeDict = new Dictionary <string, INode <IArtifact> >(); foreach (var template in templates) { foreach (var variant in template.Variants) { var lines = _contentParser.Parse(template.Lines, variant.Variables).ToImmutableList(); var imageId = "unknown"; var tags = new List <string>(); var platform = string.Empty; var references = new List <Reference>(); var components = new List <string>(); var repositories = new List <string>(); var comments = new List <string>(); var dockerfileLines = new List <Line>(); var weight = 0; foreach (var line in lines) { var isMetadata = false; if (line.Type == LineType.Comment) { isMetadata = TrySetByPrefix(line.Text, CommentPrefix, value => comments.Add(value.Trim())) || TrySetByPrefix(line.Text, IdPrefix, value => imageId = value) || TrySetByPrefix(line.Text, TagPrefix, value => tags.Add(value)) || TrySetByPrefix(line.Text, PlatformPrefix, value => platform = value) || TrySetByPrefix(line.Text, BasedOnPrefix, value => { var match = ReferenceRegex.Match(value); if (match.Success) { var weightValue = 0; if (int.TryParse(match.Groups["weight"].Value, out var refWeightValue)) { weightValue = refWeightValue; } references.Add(new Reference(match.Groups["reference"].Value, new Weight(weightValue))); } }) || TrySetByPrefix(line.Text, ComponentsPrefix, value => components.Add(value)) || TrySetByPrefix(line.Text, RepoPrefix, value => repositories.Add(value)) || TrySetByPrefix(line.Text, WeightPrefix, value => { if (int.TryParse(value, out var weightValue)) { weight = weightValue; } }); } if (!isMetadata) { dockerfileLines.Add(line); } } var dockerfile = new Dockerfile(_pathService.Normalize(variant.BuildPath), imageId, platform, tags, components, repositories, comments, references, new Weight(weight), dockerfileLines); if (graph.TryAddNode(new Image(dockerfile), out var dockerImageNode)) { foreach (var tag in tags) { nodeDict[$"{imageId}:{tag}"] = dockerImageNode; } if (graph.TryAddNode(new GeneratedDockerfile(_pathService.Normalize(Path.Combine(dockerfile.Path, "Dockerfile")), dockerfile.Lines), out var dockerfileNode)) { graph.TryAddLink(dockerImageNode, GenerateDependency, dockerfileNode, out _); } } } } var imageNodes = from node in graph.Nodes let image = node.Value as Image where image != null select new { node, image }; // Add references foreach (var from in imageNodes.ToList()) { foreach (var reference in from.image.File.References) { if (nodeDict.TryGetValue(reference.RepoTag, out var toNode)) { graph.TryAddLink(from.node, new Dependency(DependencyType.Build), toNode, out _); } else { if (graph.TryAddNode(reference, out var referenceNode)) { nodeDict[reference.RepoTag] = referenceNode; } graph.TryAddLink(from.node, new Dependency(DependencyType.Pull), referenceNode, out _); } } } return(new Result <IGraph <IArtifact, Dependency> >(graph)); }
public Task BuildImage(Dockerfile dockerFile, IProgress <BuildImageProgress> progress, RepositoryAuthInfo authInfo = null) { throw new NotImplementedException(); }
private static string GetReadmeTagName(Dockerfile dockerFile) => string.Join(" or ", dockerFile.Tags);
private static string GetTagLink(Dockerfile dockerFile) => GetReadmeTagName(dockerFile).Replace(".", string.Empty).Replace(" ", "-");