Olá Pessoal,

Neste post irei mostrar como instalar uma aplicação em .NET rodando dentro de um Windows Container, tudo isso usando docker e powershell.

Primeiro, se você ainda não instalou o docker no seu windows server, assista esse vídeo, para instalar windows server 2016 ou esse para instalar no windows server 2019


Segundo, segue abaixo o dockerfile utilizado com o passo-a-passo, para criacao da nossa imagem.
Neste exemplo nos iremos utilizar o Windows nano server versão 1809 e o .NET Core 3.1


# https://hub.docker.com/u/leandroscar
FROM leandroscar/dotnetcore-sdk-3.1-windows-nanoserver1809 AS build
WORKDIR /source

# copy csproj and restore as distinct layers / copiando csproj e restaurando distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore -r win-x64

# copy everything else and build app / Copiando os arquivos e criando o 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 / Imagem final
FROM leandroscar/windows-nanoserver1809 AS runtime
WORKDIR /app
COPY --from=build /app ./

# Configure web servers to bind to port 80 when present / configurando servidor web para porta 80
ENV ASPNETCORE_URLS=http://+:80
# aspnetcore application as entrypoint / aplicacao aspnetcore como entrypoint
ENTRYPOINT ["aspnetapp"]


Agora com o docker instalado e que já vimos como esta estruturado o nosso docker file.
O próximo passo, será executar os comandos abaixo, para baixar o dockerfile supracitado e salvar no caminho “C:/ws/dotnetcoreapp” , Você pode alterar o caminho do script se necessário, alterando o valor da variável $folder.



Invoke-WebRequest https://github.com/leandroscardua/Windows-Container/raw/master/dockerfile/dotnetcoreapp/dotnetcoreapp.zip -UseBasicParsing -OutFile ( New-Item -Path “C:\ws\dotnetcoreapp\dotnetcoreapp.zip” -Force) 
cd C:\ws\dotnetcoreapp\
Expand-Archive -Path dotnetcoreapp.zip -DestinationPath C:\ws\dotnetcoreapp\


Após a execução do script anterior, nos podemos executar o comando abaixo para criar a imagem, usando a tag local:dotnetcoreapp 


docker build -t local:dotnetcoreapp .


Segue o output do comando em partes:

PS C:/ws/dotnetcoreapp> docker build -t local:dotnetcoreapp .

Sending build context to Docker daemon 5.374MB
Step 1/13 : FROM leandroscar/dotnetcore-sdk-3.1-windows-nanoserver1809 AS build
—> 2c5f79b7fa38
Step 2/13 : WORKDIR /source
—> Running in c57b5dca8412
Removing intermediate container c57b5dca8412
—> 0c010793a790
Step 3/13 : COPY *.sln .
—> 1c429369b357
Step 4/13 : COPY aspnetapp/*.csproj ./aspnetapp/
—> 1573836ed0a2
Step 5/13 : RUN dotnet restore -r win-x64
—> Running in 83d6f2c1fdd3
Determining projects to restore…
Restored C:\source\aspnetapp\aspnetapp.csproj (in 26.15 sec).
Removing intermediate container 83d6f2c1fdd3
—> 9465fddc9159
Step 6/13 : COPY aspnetapp/. ./aspnetapp/
—> 8d86b30e50c8
Step 7/13 : WORKDIR /source/aspnetapp
—> Running in 8509484bf16c
Removing intermediate container 8509484bf16c
—> 7aa57c6b21c8
Step 8/13 : RUN dotnet publish -c release -o /app -r win-x64 –self-contained true –no-restore /p:PublishTrimmed=true /p:PublishReadyToRun=true
—> Running in 3e739ba5d248
Microsoft (R) Build Engine version 16.7.0-preview-20360-03+188921e2f for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

aspnetapp -> C:\source\aspnetapp\bin\release\netcoreapp3.1\win-x64\aspnetapp.dll
aspnetapp -> C:\source\aspnetapp\bin\release\netcoreapp3.1\win-x64\aspnetapp.Views.dll
Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
Some ReadyToRun compilations emitted warnings, indicating potential missing dependencies. Missing dependencies could potentially cause runtime failures. To show the warnings, set the PublishReadyToRunShowWarnings property to true.
aspnetapp -> C:\app\
Removing intermediate container 3e739ba5d248
—> 10f581dff22c
Step 9/13 : FROM leandroscar/windows-nanoserver1809 AS runtime
—> 9cf03161d226
Step 10/13 : WORKDIR /app
—> Running in 1a1dc9763200
Removing intermediate container 1a1dc9763200
—> 5b098799dbce
Step 11/13 : COPY –from=build /app ./
—> 112221465065
Step 12/13 : ENV ASPNETCORE_URLS=http://+:80
—> Running in 554567595c83
Removing intermediate container 554567595c83
—> d3174128e07c
Step 13/13 : ENTRYPOINT [“aspnetapp”]
—> Running in ed6e2546ee8a
Removing intermediate container ed6e2546ee8a
—> 4ae3a7ff5f1e
Successfully built 4ae3a7ff5f1e
Successfully tagged local:dotnetcoreapp

 

 

Valeu pessoal, até a próxima.

 

Deixe uma resposta