Skip to content

Commit

Permalink
Added support for environment args and volumes in run command. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickcrowne authored Dec 6, 2016
1 parent f7629ca commit 356e7db
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
50 changes: 50 additions & 0 deletions src/LclDckr/Commands/Run/RunArguments.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> EnvironmentArgs = new Dictionary<string, string>();

public IList<string> Volumes = new List<string>();

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();
}
}
}
22 changes: 18 additions & 4 deletions src/LclDckr/DockerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.RegularExpressions;
using LclDckr.Commands.Ps;
using LclDckr.Commands.Ps.Filters;
using LclDckr.Commands.Run;

namespace LclDckr
{
Expand Down Expand Up @@ -54,12 +55,25 @@ public string Build(string path = ".", string filePath = null)
/// <returns>The long uuid of the created container</returns>
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);
/// <summary>
/// Runs the specified image in a new container
/// </summary>
/// <param name="imageName"></param>
/// <param name="args"></param>
/// <returns></returns>
public string RunImage(string imageName, RunArguments args)
{
var process = GetDockerProcess($"run {args.ToArgString()} {imageName}");
process.Start();
process.WaitForExit();
process.ThrowForError();
Expand Down

0 comments on commit 356e7db

Please sign in to comment.