Basically my goal is to create an API with Visual Studio C# with .NET 8, and run it from Docker, and call it with a simple curl or postman call.
I've created an API-project-with-Docker in VS. Running the project via VS in Docker works. When I stop the 'Docker Start' in the IDE, the container is still running in Docker. The web browsers with swagger started by VS stop. If I then try to connect via some way it gives me 'Empty reply from server'. Also when I stop and start the docker container it does not work.
How can I run this example from Docker, and make an API call outside Visual Studio?
My code is the simple API project you get when using the project template 'ASP.NET Core Web API'. I have chosen to disable 'Configure for HTTPS'. The Docker OS is Linux. In my Program.cs I tried to disable 'UseAuthorization' but that did not help.
The result I am getting running from Visual Studio is for example from the command line:
C:\Users\wintinkm>curl -X GET http://localhost:32785/WeatherForecast
[{"date":"2024-11-19","temperatureC":17,"temperatureF":62,"summary":"Bracing"},{"date":"2024-11-20","temperatureC":19,"temperatureF":66,"summary":"Warm"},{"date":"2024-11-21","temperatureC":16,"temperatureF":60,"summary":"Mild"},{"date":"2024-11-22","temperatureC":17,"temperatureF":62,"summary":"Sweltering"},{"date":"2024-11-23","temperatureC":29,"temperatureF":84,"summary":"Mild"}]
When I stop the running docker from Visual Studio, I still see the docker container running, but the same command now gives:
C:\Users\wintinkm>curl -X GET http://localhost:32786/WeatherForecast
curl: (52) Empty reply from server
I get the same result if I stop and start the container VS created in Docker.
When I stop that container in Docker I get a result saying the server is not reachable at all. So something was running, I just got rejected. I do not know why because I am not using SSL and disabled Authorization.
C:\Users\wintinkm>curl -X GET http://localhost:32786/WeatherForecast
curl: (7) Failed to connect to localhost port 32786 after 2242 ms: Could not connect to server
What you're seeing is an artifact of VS's fast mode contaienr debugging (see here for details). The image / container used for debugging in the Debug configuration only works within VS. If you right-click on the Dockerfile you can build a full image that is usable outside of VS.