diff --git a/src/LclDckr/Commands/Run/RunArguments.cs b/src/LclDckr/Commands/Run/RunArguments.cs new file mode 100644 index 0000000..a906acc --- /dev/null +++ b/src/LclDckr/Commands/Run/RunArguments.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using System.Text; + +namespace LclDckr.Commands.Run +{ + public class RunArguments + { + public bool Interactive { get; set; } + + public string Name { get; set; } + + public string HostName { get; set; } + + public IDictionary EnvironmentArgs = new Dictionary(); + + public IList Volumes = new List(); + + public string ToArgString() + { + var args = new StringBuilder("-d"); + + if (Interactive) + { + args.Append("i"); + } + + if (Name != null) + { + args.Append($" --name {Name}"); + } + + if (HostName != null) + { + args.Append($" --hostname {HostName}"); + } + + foreach (var environmentArg in EnvironmentArgs) + { + args.Append($" -e {environmentArg.Key}={environmentArg.Value}"); + } + + foreach (var volume in Volumes) + { + args.Append($" -v {volume}"); + } + + return args.ToString(); + } + } +} diff --git a/src/LclDckr/DockerClient.cs b/src/LclDckr/DockerClient.cs index a66ce78..af88925 100644 --- a/src/LclDckr/DockerClient.cs +++ b/src/LclDckr/DockerClient.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using LclDckr.Commands.Ps; using LclDckr.Commands.Ps.Filters; +using LclDckr.Commands.Run; namespace LclDckr { @@ -54,12 +55,25 @@ public string Build(string path = ".", string filePath = null) /// The long uuid of the created container public string RunImage(string imageName, string name, string hostName = null, bool interactive = false) { - var hostArg = hostName != null ? $"--hostname {hostName}" : ""; - var interactiveArg = interactive ? "i" : ""; + var arguments = new RunArguments + { + Name = name, + HostName = hostName, + Interactive = interactive + }; - var args = $"run -d{interactiveArg} --name {name} {hostArg} {imageName}"; + return RunImage(imageName, arguments); + } - var process = GetDockerProcess(args); + /// + /// Runs the specified image in a new container + /// + /// + /// + /// + public string RunImage(string imageName, RunArguments args) + { + var process = GetDockerProcess($"run {args.ToArgString()} {imageName}"); process.Start(); process.WaitForExit(); process.ThrowForError();