Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

define docker baseimage and tag #447

Merged
merged 15 commits into from May 11, 2020
4 changes: 2 additions & 2 deletions src/Microsoft.Tye.Core/ApplicationFactory.cs
Expand Up @@ -96,12 +96,12 @@ public static async Task<ApplicationBuilder> CreateAsync(OutputContext output, F
}
project.Replicas = configService.Replicas ?? 1;

await ProjectReader.ReadProjectDetailsAsync(output, project);

// We don't apply more container defaults here because we might need
// to prompt for the registry name.
project.ContainerInfo = new ContainerInfo() { UseMultiphaseDockerfile = false, };

await ProjectReader.ReadProjectDetailsAsync(output, project);

// Do k8s by default.
project.ManifestInfo = new KubernetesManifestInfo();
}
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.Tye.Core/ProjectReader.cs
Expand Up @@ -268,6 +268,15 @@ private static void EvaluateProject(OutputContext output, ProjectServiceBuilder
project.Frameworks.AddRange(sharedFrameworks.Select(s => new Framework(s)));
output.WriteDebugLine($"Found shared frameworks: {string.Join(", ", sharedFrameworks)}");

// determine container base image
if (project.ContainerInfo == null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See @jkotalik comment : #447 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidfowl I agree and it appears that it is the only entry point. However, @jkotalik is suggesting that there should be a null check, if it is (although it shoudn't be) we must create the instance.

previously I was just setting the values - project.ContainerInfo!.BaseImageName

{
project.ContainerInfo = new ContainerInfo() { UseMultiphaseDockerfile = false, };
}

project.ContainerInfo!.BaseImageName = projectInstance.GetPropertyValue("ContainerBaseImage");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a null check here for project.ContainerInfo is probably better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need the property here, should I create it?
new ContainerInfo() { UseMultiphaseDockerfile = false, };

project.ContainerInfo!.BaseImageTag = projectInstance.GetPropertyValue("ContainerBaseTag");

bool PropertyIsTrue(string property)
{
return projectInstance.GetPropertyValue(property) is string s && !string.IsNullOrEmpty(s) && bool.Parse(s);
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.Tye.Hosting/Model/ProjectRunInfo.cs
Expand Up @@ -24,6 +24,8 @@ public ProjectRunInfo(ProjectServiceBuilder project)
RunCommand = project.RunCommand;
RunArguments = project.RunArguments;
PublishOutputPath = project.PublishDir;
ContainerBaseImage = project.ContainerInfo?.BaseImageName;
ContainerBaseTag = project.ContainerInfo?.BaseImageTag;
IsAspNet = project.IsAspNet;
}

Expand All @@ -47,6 +49,9 @@ public ProjectRunInfo(ProjectServiceBuilder project)
public string RunCommand { get; }
public string RunArguments { get; }

public string? ContainerBaseTag { get; }
public string? ContainerBaseImage { get; }

// This exists for running projects as containers
public List<DockerVolume> VolumeMappings { get; } = new List<DockerVolume>();
}
Expand Down
9 changes: 8 additions & 1 deletion src/Microsoft.Tye.Hosting/TransformProjectsIntoContainers.cs
Expand Up @@ -127,7 +127,14 @@ private static string DetermineContainerImage(ProjectRunInfo project)
{
var baseImage = project.IsAspNet ? "mcr.microsoft.com/dotnet/core/aspnet" : "mcr.microsoft.com/dotnet/core/runtime";

return $"{baseImage}:{project.TargetFrameworkVersion}";
if (!string.IsNullOrEmpty(project.ContainerBaseImage))
spboyer marked this conversation as resolved.
Show resolved Hide resolved
{
baseImage = project.ContainerBaseImage;
}

var imageTag = string.IsNullOrEmpty(project.ContainerBaseTag) ? project.TargetFrameworkVersion : project.ContainerBaseTag;

return $"{baseImage}:{imageTag}";
}

public Task StopAsync(Application application)
Expand Down
24 changes: 24 additions & 0 deletions test/E2ETest/TyeRunTests.cs
Expand Up @@ -246,6 +246,30 @@ public async Task FrontendDockerBackendProject()
});
}

[ConditionalFact]
public async Task DockerBaseImageAndTagTest()
{
using var projectDirectory = CopyTestProjectDirectory(Path.Combine("frontend-backend", "backend"));

var projectFile = new FileInfo(Path.Combine(projectDirectory.DirectoryPath, "backend-baseimage.csproj"));

var outputContext = new OutputContext(_sink, Verbosity.Debug);
var application = await ApplicationFactory.CreateAsync(outputContext, projectFile);

// Transform the backend into a docker image for testing
var project = (ProjectServiceBuilder)application.Services.First(s => s.Name == "backend-baseimage");

// check ContainerInfo values
Assert.True(string.Equals(project.ContainerInfo!.BaseImageName, "mcr.microsoft.com/dotnet/core/sdk"));
Assert.True(string.Equals(project.ContainerInfo!.BaseImageTag, "3.1-buster"));

// check projectInfo values
var projectRunInfo = new ProjectRunInfo(project);

Assert.True(string.Equals(projectRunInfo!.ContainerBaseImage, project.ContainerInfo.BaseImageName));
Assert.True(string.Equals(projectRunInfo!.ContainerBaseTag, project.ContainerInfo.BaseImageTag));
}

[ConditionalFact]
[SkipIfDockerNotRunning]
public async Task DockerNamedVolumeTest()
Expand Down
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Backend</RootNamespace>
</PropertyGroup>

<PropertyGroup>
<ContainerBaseImage>mcr.microsoft.com/dotnet/core/sdk</ContainerBaseImage>
<ContainerBaseTag>3.1-buster</ContainerBaseTag>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(TyeLibrariesPath)\Microsoft.Tye.Extensions.Configuration\Microsoft.Tye.Extensions.Configuration.csproj" />
</ItemGroup>

</Project>