FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
(...)
ENV ASPNETCORE_URLS="http://*:5000"
ENV ASPNETCORE_ENVIRONMENT=Development
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
ENV vars and ARG are scoped per build-stage: you defined these variables for your base layer of container but you are running your app from runtime layer where these ENV are not set.
(actually, using staged build is great and you are doing it almost right!)
Thats why kestrel is trying to bind to "default" (127.0.0.1 - and thats localhost/loopback address 'inside your container' not 'your localhost' nor 'virtual machine localhost' - you want Kestrel to bind to 0.0.0.0:port)
(Please note that you can bind Kestrel to standart port :80 (http) and/or :443 (https) and then remap port on docker run with -p {any}:80, for example -p 8080:80 or -p 8000:80 or -p 123456:80. With this, you will save a little headache to write everywhere 'my container is exposing :5000')
You can test setting correct runtime ENV variables with
docker container run -itd -p 8000:5000 imageid -e ASPNETCORE_URLS="http://*:5000" ASPNETCORE_ENVIRONMENT=Development
Also, you are exposing :5000, trying to set _URLS to :5000, re-mapping it to port 8000 (with -p 8000:5000) but then you are trying to access it again thru http://localhost:5000/swagger/index.html - but thats the port INSIDE your container, you remapped it to :8000 with your docker container run -p 8000:5000.
You should access your container at http://localhost:8000/swagger/index.html
Also, you can run docker image right from your Visual Studio with attached debugger.
Not sure how to run single dockerfile, but there is docker-compose.dcproj for docker-compose.yml.
If problem persist, please double check if something isnt already binding to your port :8000 before your container goes up: stop your container and try (powershell)
Test-NetConnection -ComputerName "localhost" -Port 8000 -InformationLevel "Detailed"
AFAIK best aproach:
Do NOT set any ENV in dockerfile "for runtime". If you dont need them for compilation, you dont need them in dockerfile.
Do NOT modify dockerfile generated with Visual Studio. When you (un)reference project, just delete it and generate it again. Your ENV declaration would be lost. Plus, you dont want to push to registry / publish to production your app with ENVIRONMENT=Development.
Go and delete explicit bind to 127.0.0.1:5000 from your Program.cs (cant remember where exactly, but it is there somewhere). Instead, set AllowedHosts=* in your applicationSettings.json. Kestrel will then bind to 0.0.0.0 interface by default.
Do NOT try to run your dockerfile in Development mode from console. Use docker-compose thru Visual Studio .dcproj (you will have docker-compose.yml where you can set any env variables / volumes / networks etc you want (ready for production*) and docker-compose.override.yml where you set your build context / path to dockerfile / your dev variables / volumes etc. When you hit f5 VS will bring up your docker-compose and will attach debugger.
Thruth to be told, you rarely do development with only one container (where is, for example, your database? :) )
*: with grain of salt, see Kubernetes etc, but thats different topic.