Search code examples
asp.netdockervisual-studiocontainers

Correct way to containerize ASP.NET Core Web API Application


I've written a minimal API using Visual Studio's 'ASP.NET Core Web API' project template. It's pretty simple and interfaces with an SQL Server db.

I want to containerize the application using Docker, so I can deploy the app to a cloud machine.

Currently, I'm using Visual Studio's 'Docker' build option (the green play button). When I build it this way it creates and runs the docker container and everything works as expected, the Swagger UI can be accessed using localhost:port. The problem is when I try to run the container without using Visual Studio (i.e. calling docker run manually), nothing functions as expected. I cannot access the Swagger UI or any other part of the app via localhost.

Note that this is while using (what I think is) the exact same docker run command that Visual Studio is using, as I copied the command from the visual studio 'Container Tools' output.

The command I'm using is

docker run -dt -v "C:\Users\[User]\vsdbg\vs2017u5:/remote_debugger:rw" -v "C:\Users\[User]\AppData\Roaming\Microsoft\UserSecrets:/root/.microsoft/usersecrets:ro" -v "C:\Users\[User]\AppData\Roaming\Microsoft\UserSecrets:/home/app/.microsoft/usersecrets:ro" -v "C:\Users\[User]\AppData\Roaming\ASP.NET\Https:/root/.aspnet/https:ro" -v "C:\Users\[User]\AppData\Roaming\ASP.NET\Https:/home/app/.aspnet/https:ro" -v "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Sdks\Microsoft.Docker.Sdk\tools\TokenService.Proxy\linux-x64\net6.0:/TokenService.Proxy:ro" -v "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Sdks\Microsoft.Docker.Sdk\tools\HotReloadProxy\linux-x64\net6.0:/HotReloadProxy:ro" -v "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\HotReload:/HotReloadAgent:ro" -v "C:\Users\[User]\source\repos\KPFIrmwareApiApp\KPFirmwareAPI:/app" -v "C:\Users\[User]\source\repos\KPFIrmwareApiApp:/src/" -v "C:\Users\[User]\.nuget\packages\:/.nuget/fallbackpackages2" -v "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages:/.nuget/fallbackpackages" -e "ASPNETCORE_LOGGING__CONSOLE__DISABLECOLORS=true" -e "ASPNETCORE_ENVIRONMENT=Development" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -e "NUGET_PACKAGES=/.nuget/fallbackpackages2" -e "NUGET_FALLBACK_PACKAGES=/.nuget/fallbackpackages;/.nuget/fallbackpackages2" -P --name KPFirmwareAPI --entrypoint tail kpfirmwareapi:dev -f /dev/null 

Thanks for the help!


Solution

  • When you are running in Visual Studio (especially in the Debug configuration) it does a lot of optimalizations to speed up debugging. You can read about them in the docs

    For your particular case you'll want to simplify to:
    docker run -dt -v "C:\Users\[User]\AppData\Roaming\Microsoft\UserSecrets:/root/.microsoft/usersecrets:ro" -v "C:\Users\[User]\AppData\Roaming\Microsoft\UserSecrets:/home/app/.microsoft/usersecrets:ro" -v "C:\Users\[User]\AppData\Roaming\ASP.NET\Https:/root/.aspnet/https:ro" -v "C:\Users\[User]\AppData\Roaming\ASP.NET\Https:/home/app/.aspnet/https:ro" -e "ASPNETCORE_ENVIRONMENT=Development" -P --name KPFirmwareAPI kpfirmwareapi

    The volumes and enviorment variables i left in the command allow you to take advantage of the certifcate the tools made for you for local testing. If you don't need SSL you can remove those.

    Note: You'll also need to build the full image (kpfirmwareapi:dev is for debugging only) which you can do in VS by right clicking on the Dockerfile and selecting "Build Docker Image":
    Dockerfile->Build Docker Image