I am trying to deploy my application using Docker Compose, but I encounter the following error:
System.IO.IOException: Failed to bind to address http://[::]:8080: address already in use.
When I configure my application to use only HTTP, it works without any issues. However, when I switch to HTTPS, I receive this error. I have removed HTTP from the configuration, but the same error persists for HTTPS:
System.IO.IOException: Failed to bind to address https://[::]:8081: address already in use.
I have searched for numerous solutions but nothing seems to work. Here are the steps I have tried:
- Removing the “binding” in Docker Compose, changing the port mapping from 8081:8081 to 8081
- Checking if the port is occupied by any process and terminating it (none are occupying it)
- Testing on both Alpine Linux and Windows Docker Desktop, but I still get the same error
Here are some relevant parts of my code, docker-compose.yml and launshSettings.json :
Program.cs
webBuilder.ConfigureKestrel((context, options) =>
{
var configuration = context.Configuration;
// Configure HTTP endpoint
options.ListenAnyIP(8080, listenOptions =>
{
listenOptions.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1AndHttp2;
});
// Configure HTTPS endpoint
options.ListenAnyIP(8081, listenOptions =>
{
listenOptions.UseHttps(httpsOptions =>
{
httpsOptions.ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(
configuration["Kestrel:Endpoints:Https:Certificate:Path"],
configuration["Kestrel:Endpoints:Https:Certificate:Password"]);
});
});
});
docker-composer.yml:
myApp:
container_name: appdocker
image: ${DOCKER_REGISTRY-}myApp
build:
context: .
dockerfile: myApp/Dockerfile
ports:
- "8080"
- "8081"
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ASPNETCORE_URLS=https://+:8081;http://+:8080
- ASPNETCORE_Kestrel__Certificates__Default__Password=1234@999-
- ASPNETCORE_Kestrel__Certificates__Default__Path=/root/.aspnet/https/certDocker.pfx
volumes:
- ./aspnet/https/certDocker.pfx:/root/.aspnet/https/certDocker.pfx
depends_on:
- myAppdb
networks:
- myNet
launshSettings.json:
"Container (Dockerfile)": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": {
"ASPNETCORE_HTTPS_PORTS": "8081",
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true,
"useSSL": true
}
}
Has anyone faced a similar issue or can provide guidance on how to resolve this? Thank you!