ASP.NET Core Docker Container App not accessible
Solution 1
Turns out this is a bug in Visual Studio for Mac v18.1.2 (docker-compose up works)
Solution 2
I had your problem a while ago, this is how I fixed it. You need to specify --server.urls
as a running argument like below:
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Service.API.dll", "--server.urls", "http://+:80;https://+:443"]
And where that 5000
and 5001
come from?
Kestrel Endpoint configuration
By default, ASP.NET Core binds to:
https://localhost:5001 (when a local development certificate is present)
Update 1:
According to your docker-compose configuration, you have set ASPNETCORE_ENVIRONMENT
to Development
. I think you should change it to Production
because when you enable Development
the ASP.NET Core
will read settings from launchSettings.json
.
The development environment can enable features that shouldn't be exposed in production. For example, the ASP.NET Core templates enable the Developer Exception Page in the development environment.
The environment for local machine development can be set in the Properties\launchSettings.json file of the project. Environment values set in launchSettings.json override values set in the system environment.
As far as I remember the default ports for Kestrel
are 80
and 443
in every default launchSettings.json
.
If you need to run your project in development
mode on Docker you should change configuration inside launchSettings.json
but I think it's not recommended and it's better to change the mode to Production
.
service.api:
build:
context: .
dockerfile: src/Services/Service.API/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ASPNETCORE_URLS=http://+:80;https://+:443
- ASPNETCORE_HTTPS_PORT=5254
- ASPNETCORE_Kestrel__Certificates__Default__Password=crypticpassword
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
ports:
- "5204:80"
- "5254:443"
volumes:
- ${HOME}/.aspnet/https:/https/
Solution 3
Basically, in the asp.net core code, it only listens to localhost which is in Container, but not your local pc. You can solve the issue by updating your setting to listen "http://*:5000" instead of "http://localhost:5000".
Here is a detailed explaination of the cause and solution. https://stackoverflow.com/a/65953771/8918445
Related videos on Youtube
Palmi
Updated on June 04, 2022Comments
-
Palmi almost 2 years
I run an ASP.NET Core service in a Docker container on MacOS.
Visual Studio for Mac v18.1.2 (build 2) .NET Core SDK: 2.2.300Here is the Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build WORKDIR / COPY src/Services/Service.API/Service.API.csproj src/Services/Service.API/ RUN dotnet restore src/Services/Service.API/Service.API.csproj COPY . . WORKDIR /src/Services/Service.API RUN dotnet build Service.API.csproj -c Release -o /app FROM build AS publish RUN dotnet publish Service.API.csproj -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "Service.API.dll"]
Here is how the docker-compose file for the service looks like:
service.api: build: context: . dockerfile: src/Services/Service.API/Dockerfile environment: - ASPNETCORE_ENVIRONMENT=Development - ASPNETCORE_URLS=http://+:80;https://+:443 - ASPNETCORE_HTTPS_PORT=5254 - ASPNETCORE_Kestrel__Certificates__Default__Password=crypticpassword - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx ports: - "5204:80" - "5254:443" volumes: - ${HOME}/.aspnet/https:/https/
The ports of the running Docker container looks good too:
0.0.0.0:5204->80/tcp, 0.0.0.0:5254->443/tcp
But when I try to call https://localhost:5254 it says site cannot be reached.
Also in the output I see following warning:
warn: Microsoft.AspNetCore.Server.Kestrel[0] Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'. Microsoft.AspNetCore.Server.Kestrel:Warning: Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'. Hosting environment: Development Content root path: /app Now listening on: https://localhost:5001
Why doesn't it take the urls set by the ASPNETCORE_URLS enivronment variable?
What else could I do for troubleshooting to find the problem?
-
Palmi almost 5 yearscontainer is up and running. Regarding the entrypoint, I added the Dockerfile in my question
-
nPcomp almost 5 yearsDid you check the Kestrel? Is it running too?
-
Palmi almost 5 yearsHow do I check it in the container?
-
nPcomp almost 5 yearsJust do
docker exec -it your_container /bin/bash
-
nPcomp almost 5 yearsOr
docker logs your_container
if you want to follow the logs use:docker logs -f your_container
syntax -
Palmi almost 5 yearsdotnet Service.API.dll in the container returns: Did you mean to run dotnet SDK commands? Please install dotnet SDK from: go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
-
Palmi almost 5 yearsThanks, but this didn't help. Same problem. Also according to your link you can use the ASPNETCORE_URLS environment variable, what I do, but it seems to just ignore it and use the default bindings.
-
Palmi almost 5 yearsI removed it in my question. I just tried to merge the the docker-compose.yml and docker-compose.override.yml to simplify it for the question...
-
Edward almost 5 years@Palmi What do you mean by merge docker-compose.yam and docker-compose.override.yml? Run
docker container -ls -a
to see whether there is any container with image nameservice.api
, remove them and rundocker-compose up
again. Not sure whether you configured anywhere, try to use this dockerfile and docker-compose with a new default .net core project. -
Ali Bahrami almost 5 years@Palmi I see, I updated the answer please take a look.
-
NyoNor over 2 yearsOn windows 10 using docker desktop and linux containers it's not accessible too...