From 4670eaf6157bcf1df38cf906d0a80da39770e941 Mon Sep 17 00:00:00 2001 From: Matt Thalman Date: Thu, 3 Dec 2020 09:05:17 -0600 Subject: [PATCH] Sample Dockerfiles for Windows Server Core (#2428) --- .../Dockerfile.windowsservercore-x64 | 19 ++++++++++++ .../Dockerfile.windowsservercore-x64-slim | 29 +++++++++++++++++++ .../Dockerfile.windowsservercore-x64 | 17 +++++++++++ .../Dockerfile.windowsservercore-x64-slim | 22 ++++++++++++++ .../SampleImageTests.cs | 4 +-- .../Microsoft.DotNet.Docker.Tests/TestData.cs | 6 ++-- 6 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 samples/aspnetapp/Dockerfile.windowsservercore-x64 create mode 100644 samples/aspnetapp/Dockerfile.windowsservercore-x64-slim create mode 100644 samples/dotnetapp/Dockerfile.windowsservercore-x64 create mode 100644 samples/dotnetapp/Dockerfile.windowsservercore-x64-slim diff --git a/samples/aspnetapp/Dockerfile.windowsservercore-x64 b/samples/aspnetapp/Dockerfile.windowsservercore-x64 new file mode 100644 index 000000000..8eb6d7146 --- /dev/null +++ b/samples/aspnetapp/Dockerfile.windowsservercore-x64 @@ -0,0 +1,19 @@ +# https://hub.docker.com/_/microsoft-dotnet +FROM mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2019 AS build +WORKDIR /source + +# copy csproj and restore as distinct layers +COPY *.sln . +COPY aspnetapp/*.csproj ./aspnetapp/ +RUN dotnet restore -r win-x64 + +# copy everything else and build app +COPY aspnetapp/. ./aspnetapp/ +WORKDIR /source/aspnetapp +RUN dotnet publish -c release -o /app -r win-x64 --self-contained false --no-restore + +# final stage/image +FROM mcr.microsoft.com/dotnet/aspnet:5.0-windowsservercore-ltsc2019 AS runtime +WORKDIR /app +COPY --from=build /app ./ +ENTRYPOINT ["aspnetapp"] diff --git a/samples/aspnetapp/Dockerfile.windowsservercore-x64-slim b/samples/aspnetapp/Dockerfile.windowsservercore-x64-slim new file mode 100644 index 000000000..f8b25eac4 --- /dev/null +++ b/samples/aspnetapp/Dockerfile.windowsservercore-x64-slim @@ -0,0 +1,29 @@ +# escape=` + +# https://hub.docker.com/_/microsoft-dotnet +FROM mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2019 AS build +WORKDIR /source + +# copy csproj and restore as distinct layers +COPY *.sln . +COPY aspnetapp/*.csproj ./aspnetapp/ +RUN dotnet restore -r win-x64 + +# copy everything else and build app +COPY aspnetapp/. ./aspnetapp/ +WORKDIR /source/aspnetapp +RUN dotnet publish -c release -o /app -r win-x64 --self-contained true --no-restore /p:PublishTrimmed=true /p:PublishReadyToRun=true + +# final stage/image +# Uses the ltsc2019 release; 20H2, 2004, 1909, 1809, and ltsc2016 are other choices +FROM mcr.microsoft.com/windows/servercore:ltsc2019 AS runtime +WORKDIR /app +COPY --from=build /app ./ + +ENV ` + # Configure web servers to bind to port 80 when present + ASPNETCORE_URLS=http://+:80 ` + # Enable detection of running in a container + DOTNET_RUNNING_IN_CONTAINER=true + +ENTRYPOINT ["aspnetapp"] diff --git a/samples/dotnetapp/Dockerfile.windowsservercore-x64 b/samples/dotnetapp/Dockerfile.windowsservercore-x64 new file mode 100644 index 000000000..267f4b4a5 --- /dev/null +++ b/samples/dotnetapp/Dockerfile.windowsservercore-x64 @@ -0,0 +1,17 @@ +# https://hub.docker.com/_/microsoft-dotnet +FROM mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2019 AS build +WORKDIR /source + +# copy csproj and restore as distinct layers +COPY *.csproj . +RUN dotnet restore -r win-x64 + +# copy and publish app and libraries +COPY . . +RUN dotnet publish -c release -o /app -r win-x64 --self-contained false --no-restore + +# final stage/image +FROM mcr.microsoft.com/dotnet/runtime:5.0-windowsservercore-ltsc2019 +WORKDIR /app +COPY --from=build /app . +ENTRYPOINT ["dotnetapp"] diff --git a/samples/dotnetapp/Dockerfile.windowsservercore-x64-slim b/samples/dotnetapp/Dockerfile.windowsservercore-x64-slim new file mode 100644 index 000000000..f84a23bc9 --- /dev/null +++ b/samples/dotnetapp/Dockerfile.windowsservercore-x64-slim @@ -0,0 +1,22 @@ +# https://hub.docker.com/_/microsoft-dotnet +FROM mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2019 AS build +WORKDIR /source + +# copy csproj and restore as distinct layers +COPY *.csproj . +RUN dotnet restore -r win-x64 + +# copy and publish app and libraries +COPY . . +RUN dotnet publish -c release -o /app -r win-x64 --self-contained true --no-restore /p:PublishTrimmed=true /p:PublishReadyToRun=true + +# final stage/image +# Uses the ltsc2019 release; 20H2, 2004, 1909, 1809, and ltsc2016 are other choices +FROM mcr.microsoft.com/windows/servercore:ltsc2019 +WORKDIR /app +COPY --from=build /app . + +# Enable detection of running in a container +ENV DOTNET_RUNNING_IN_CONTAINER=true + +ENTRYPOINT ["dotnetapp"] diff --git a/tests/Microsoft.DotNet.Docker.Tests/SampleImageTests.cs b/tests/Microsoft.DotNet.Docker.Tests/SampleImageTests.cs index 9e70324f2..4df2d48e7 100644 --- a/tests/Microsoft.DotNet.Docker.Tests/SampleImageTests.cs +++ b/tests/Microsoft.DotNet.Docker.Tests/SampleImageTests.cs @@ -34,7 +34,7 @@ namespace Microsoft.DotNet.Docker.Tests .Select(imageData => new object[] { imageData }); } - [SkippableTheory("windowsservercore-ltsc2019")] + [Theory] [MemberData(nameof(GetImageData))] public async Task VerifyDotnetSample(SampleImageData imageData) { @@ -49,7 +49,7 @@ namespace Microsoft.DotNet.Docker.Tests }); } - [SkippableTheory("windowsservercore-ltsc2019")] + [Theory] [MemberData(nameof(GetImageData))] public async Task VerifyAspnetSample(SampleImageData imageData) { diff --git a/tests/Microsoft.DotNet.Docker.Tests/TestData.cs b/tests/Microsoft.DotNet.Docker.Tests/TestData.cs index 1d869a577..28d0ccea3 100644 --- a/tests/Microsoft.DotNet.Docker.Tests/TestData.cs +++ b/tests/Microsoft.DotNet.Docker.Tests/TestData.cs @@ -86,8 +86,10 @@ namespace Microsoft.DotNet.Docker.Tests new SampleImageData { OS = OS.NanoServer2004, Arch = Arch.Amd64, IsPublished = true }, new SampleImageData { OS = OS.NanoServer20H2, Arch = Arch.Amd64, IsPublished = true }, - new SampleImageData { OS = OS.NanoServer20H2, Arch = Arch.Amd64, DockerfileSuffix = "nanoserver-x64" }, - new SampleImageData { OS = OS.NanoServer20H2, Arch = Arch.Amd64, DockerfileSuffix = "nanoserver-x64-slim" }, + new SampleImageData { OS = OS.NanoServer20H2, Arch = Arch.Amd64, DockerfileSuffix = "nanoserver-x64" }, + new SampleImageData { OS = OS.NanoServer20H2, Arch = Arch.Amd64, DockerfileSuffix = "nanoserver-x64-slim" }, + new SampleImageData { OS = OS.ServerCoreLtsc2019, Arch = Arch.Amd64, DockerfileSuffix = "windowsservercore-x64" }, + new SampleImageData { OS = OS.ServerCoreLtsc2019, Arch = Arch.Amd64, DockerfileSuffix = "windowsservercore-x64-slim" }, }; public static IEnumerable GetImageData()