I'm using VSCode to develop an ASP.NET Core 5.0 application on Docker. I'm using the default Kestrel webserver behing an Nginx reverse proxy.
When I want to run the app, using docker-compose build
and docker-compose up
the app runs with a message saying that deployed apps cannot be run in development environment. Alright so I tried to run the app in debug with VSCode.
The problem is that whichever port I specify in the config, Kestrel will shoot an error saying
Failed to bind to address http://127.0.0.1:*port*: address already in use.
Name a port, any port, it will give an error.
Given that it kinda works in docker-compose mode, I suspect the tasks.json file, or maybe the app is double-launched.
I ensured that all containers were stopped before launching in debug. I even did a docker system prune -a
to start with a fresh environment. Nothing take this error out of the way.
Here are some config files :
launch.json
"preLaunchTask": "docker-run: debug"
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/app/app.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "docker-build: debug",
"type": "docker-build",
"dependsOn": [
"build"
],
"dockerBuild": {
"tag": "appnet_app:dev",
"target": "base",
"dockerfile": "${workspaceFolder}/app/Dockerfile",
"context": "${workspaceFolder}/app",
"pull": true
},
"netCore": {
"appProject": "${workspaceFolder}/app/app.csproj"
},
"options": {
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
},
{
"label": "docker-run: debug",
"type": "docker-run",
"dependsOn": [
"docker-build: debug"
],
"dockerRun": {
"env": {
"ASPNETCORE_ENVIRONMENT":"Development"
},
"image": "appnet_app:dev"
},
"netCore": {
"appProject": "${workspaceFolder}/app/app.csproj",
"enableDebugging": true
},
"options": {
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
]
}
app Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim as base
WORKDIR /app
COPY bin/Debug/net5.0 .
ENV ASPNETCORE_URLS=http://+:8000
EXPOSE 8000
ENTRYPOINT ["dotnet", "app.dll"]
I also have a launchSettings.json in Properties directory, which I'm quite confused which file is used between this one and the launch.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:4324",
"sslPort": 5356
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"app": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "http://localhost:8000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Program.cs
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run(); // <----- Crashes on this line
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls("http://localhost:8000");
});
question from:
https://stackoverflow.com/questions/65861998/kestrel-port-is-always-in-use