Browse Source

Added support for volume mapping docker containers (#156)

- This also works when you run projects as containers.
davidfowl/dependencies
David Fowler 7 years ago
committed by GitHub
parent
commit
fb5243bcf2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/Microsoft.Tye.Hosting/DockerRunner.cs
  2. 5
      src/Microsoft.Tye.Hosting/Model/ProjectRunInfo.cs
  3. 6
      src/Microsoft.Tye.Hosting/TransformProjectsIntoContainers.cs
  4. 18
      src/tye/ConfigModel/ConfigApplication.cs
  5. 2
      src/tye/ConfigModel/ConfigService.cs
  6. 12
      src/tye/ConfigModel/ConfigVolume.cs
  7. 1
      src/tye/InitHost.cs
  8. 2
      src/tye/tye.csproj

3
src/Microsoft.Tye.Hosting/DockerRunner.cs

@ -129,7 +129,8 @@ namespace Microsoft.Tye.Hosting
foreach (var pair in docker.VolumeMappings) foreach (var pair in docker.VolumeMappings)
{ {
volumes += $"-v {pair.Key}:{pair.Value} "; var sourcePath = Path.GetFullPath(Path.Combine(application.ContextDirectory, pair.Key.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)));
volumes += $"-v {sourcePath}:{pair.Value} ";
} }
var command = $"run -d {workingDirectory} {volumes} {environmentArguments} {portString} --name {replica} --restart=unless-stopped {docker.Image} {docker.Args ?? ""}"; var command = $"run -d {workingDirectory} {volumes} {environmentArguments} {portString} --name {replica} --restart=unless-stopped {docker.Image} {docker.Args ?? ""}";

5
src/Microsoft.Tye.Hosting/Model/ProjectRunInfo.cs

@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace Microsoft.Tye.Hosting.Model namespace Microsoft.Tye.Hosting.Model
{ {
public class ProjectRunInfo : RunInfo public class ProjectRunInfo : RunInfo
@ -16,5 +18,8 @@ namespace Microsoft.Tye.Hosting.Model
public string? Args { get; } public string? Args { get; }
public bool Build { get; } public bool Build { get; }
public string Project { get; } public string Project { get; }
// This exists for running projects as containers
public Dictionary<string, string> VolumeMappings { get; } = new Dictionary<string, string>();
} }
} }

6
src/Microsoft.Tye.Hosting/TransformProjectsIntoContainers.cs

@ -75,6 +75,12 @@ namespace Microsoft.Tye.Hosting
}; };
dockerRunInfo.VolumeMappings[Path.GetDirectoryName(service.Status.ProjectFilePath)!] = "/app"; dockerRunInfo.VolumeMappings[Path.GetDirectoryName(service.Status.ProjectFilePath)!] = "/app";
// Make volume mapping works when running as a container
foreach (var mapping in project.VolumeMappings)
{
dockerRunInfo.VolumeMappings[mapping.Key] = mapping.Value;
}
// Change the project into a container info // Change the project into a container info
serviceDescription.RunInfo = dockerRunInfo; serviceDescription.RunInfo = dockerRunInfo;
} }

18
src/tye/ConfigModel/ConfigApplication.cs

@ -34,7 +34,14 @@ namespace Microsoft.Tye.ConfigModel
} }
else if (service.DockerImage is object) else if (service.DockerImage is object)
{ {
runInfo = new DockerRunInfo(service.DockerImage, service.Args); var dockerRunInfo = new DockerRunInfo(service.DockerImage, service.Args);
foreach (var mapping in service.Volumes)
{
dockerRunInfo.VolumeMappings[mapping.Source!] = mapping.Target!;
}
runInfo = dockerRunInfo;
} }
else if (service.Executable is object) else if (service.Executable is object)
{ {
@ -42,7 +49,14 @@ namespace Microsoft.Tye.ConfigModel
} }
else if (service.Project is object) else if (service.Project is object)
{ {
runInfo = new ProjectRunInfo(service.Project, service.Args, service.Build ?? true); var projectInfo = new ProjectRunInfo(service.Project, service.Args, service.Build ?? true);
foreach (var mapping in service.Volumes)
{
projectInfo.VolumeMappings[mapping.Source!] = mapping.Target!;
}
runInfo = projectInfo;
} }
else else
{ {

2
src/tye/ConfigModel/ConfigService.cs

@ -21,7 +21,7 @@ namespace Microsoft.Tye.ConfigModel
public string? Args { get; set; } public string? Args { get; set; }
public int? Replicas { get; set; } public int? Replicas { get; set; }
public List<ConfigServiceBinding> Bindings { get; set; } = new List<ConfigServiceBinding>(); public List<ConfigServiceBinding> Bindings { get; set; } = new List<ConfigServiceBinding>();
public List<ConfigVolume> Volumes { get; set; } = new List<ConfigVolume>();
[YamlMember(Alias = "env")] [YamlMember(Alias = "env")]
public List<ConfigConfigurationSource> Configuration { get; set; } = new List<ConfigConfigurationSource>(); public List<ConfigConfigurationSource> Configuration { get; set; } = new List<ConfigConfigurationSource>();
} }

12
src/tye/ConfigModel/ConfigVolume.cs

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.Tye.ConfigModel
{
public class ConfigVolume
{
public string? Source { get; set; }
public string? Target { get; set; }
}
}

1
src/tye/InitHost.cs

@ -75,6 +75,7 @@ services:
{ {
service.Bindings = null!; service.Bindings = null!;
service.Configuration = null!; service.Configuration = null!;
service.Volumes = null!;
service.Project = service.Project!.Substring(directory.FullName.Length).TrimStart('/'); service.Project = service.Project!.Substring(directory.FullName.Length).TrimStart('/');
} }

2
src/tye/tye.csproj

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Tye</RootNamespace> <RootNamespace>Microsoft.Tye</RootNamespace>
<AssemblyName>tye</AssemblyName> <AssemblyName>tye</AssemblyName>
<PackageId>Microsoft.Tye</PackageId> <PackageId>Microsoft.Tye</PackageId>
<ToolCommandName>tye</ToolCommandName> <ToolCommandName>tye</ToolCommandName>

Loading…
Cancel
Save