Docker and ASP.NET MVC

Share on:

Get started

  1. choco install docker-desktop
  2. Open Docker desktop verify it is starting
  3. Right click icon in taskbar and: Switch to Windows containers

####Docker cheat sheet

Get Running containers docker container ls

Run a command inside the container docker container exec XX ls (XX är id för containern, kanske funkar med namnet?)

Attach to a running container and run powershell docker exec -ti XX powershell

Create an image docker image build --tag jtjp:v1 --file .\Dockerfile .

Run the image interactively and start powershell docker container run -it TAG powershell

Run the image detached and map port 80 in container to 8123 in host docker container run -d -p 8123:80 TAG

Inspect en running container (get IP, run status and more) docker inspect XX

####Dockerfile Create %projectRoot%\docker\web\Dockerfile

 1# escape=`
 2FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2019 AS build-agent
 3SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
 4
 5# Install web workload:
 6RUN Invoke-WebRequest -UseBasicParsing https://aka.ms/vs/16/release/vs_buildtools.exe -OutFile vs_BuildTools.exe; `
 7    Start-Process vs_BuildTools.exe -ArgumentList  '--add', 'Microsoft.VisualStudio.Workload.WebBuildTools', '--quiet', '--norestart', '--nocache' -NoNewWindow -Wait;
 8
 9SHELL ["cmd", "/S", "/C"]
10RUN @powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
11
12# new shell so choco is available
13SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
14RUN choco install webdeploy -y;
15
16WORKDIR C:\src\JTjP
17COPY JTjP\*.config ./
18RUN nuget restore packages.config -PackagesDirectory ..\packages;
19
20COPY JTjP\ .
21RUN msbuild JTjP.csproj /p:OutDir=C:\out /p:Configuration=Release /p:CreatePackageOnPublish=True /p:DeployOnBuild=True /p:DeployTarget=Package /p:AutoParameterizationWebConfigConnectionStrings=False
22
23FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2016 AS runtime
24SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
25
26WORKDIR c:\webfiles
27
28COPY --from=build-agent /out/_PublishedWebsites/JTjP/ ./
29
30RUN Remove-Website 'Default Web Site'; `
31    New-Website -Name 'jtjp' -Port 80 -PhysicalPath 'c:\webfiles'

####Troubleshooting

######VirtualBox, Hyper-v osv. Docker Desktop uses Hyper-v under the hood, and the Windows feature must be installed. Docker Toolbox is for legacy systems, and works with VirtualBox instead. However Docker Desktop is better for Windows. Hanselman has a solution for switching between Hyper-v and VirtualBox https://www.hanselman.com/blog/SwitchEasilyBetweenVirtualBoxAndHyperVWithABCDEditBootEntryInWindows81.aspx

######No internet access from inside container with Docker Desktop

Trying to restore Nuget, and have no internet access? With multiple network adapters the solution for Win 10 might be to set the interface metric, to the lowest in the system, as Docker/Hyper-v(?) choses the one with the lowest value https://github.com/docker/for-win/issues/2760#issuecomment-430889666

1Get-NetIPInterface -AddressFamily IPv4 | Sort-Object -Property InterfaceMetric -Descending
2
3#from elevated prompt!
4Set-NetIPInterface -InterfaceAlias 'Wi-Fi' -InterfaceMetric 3