Dotnet docker issue with ports

200 Views Asked by At

I have a dotnet application and use this ksetrel appsettings.json file, When I run the application, everything is worked on port 8082.

"Kestrel": {
  "Endpoints": {
    "Http": {
      "Url": "http://localhost:8082"
    }
  }
},

I have created the Dockerfile like this for my app:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 8082

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

<COPY ...> # copy my files and projects

RUN dotnet restore "proj/proj.csproj"
COPY . .
WORKDIR "/src/proj"
RUN dotnet build "proj.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "proj.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "proj.dll"]

and finally here is my docker-compose.yml file:

services:
  bs:
    image: bs
    ports:
      - "8091:8082"

But when I build my image with docker build -t bs . command and run ths with docker compose up -d command, I cant see anything in port 8091 of my machine and when I enter the http://localhost:8091/swagger in my browser, it shows the This site can’t be reached message.

and here is the logs for the container:

2023/07/29 09:47:07 [WRN] Overriding address(es) '"http://+:80"'. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead.
2023/07/29 09:47:07 [INF] Now listening on: "http://localhost:8082"
2023/07/29 09:47:07 [INF] Application started. Press Ctrl+C to shut down.
2023/07/29 09:47:07 [INF] Hosting environment: "Production"
2023/07/29 09:47:07 [INF] Content root path: "/app/"
2023/07/29 09:52:40 [INF] Application is shutting down...
2023/07/29 09:54:57 [WRN] Overriding address(es) '"http://+:80"'. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead.
2023/07/29 09:54:57 [INF] Now listening on: "http://localhost:8082"
2023/07/29 09:54:57 [INF] Application started. Press Ctrl+C to shut down.
2023/07/29 09:54:57 [INF] Hosting environment: "Production"
2023/07/29 09:54:57 [INF] Content root path: "/app/"
1

There are 1 best solutions below

0
Sumantra Mukherjee On

Try modifying the docker-compose.yml like below. Replace "your project name" with your project name in Visual Studio (or the relative path of the Dockerfile with respect to docker-compose.yml).

services:
  bs:
    image: ${DOCKER_REGISTRY-}bs
    restart: always
    build:
      context: .
      dockerfile: <your project name>/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:80
    ports:
      - "8091:8082"