Getting "localhost refused to connect." when trying to access the containerized .netcore3.1 WeatherForecast application

Viewed 557

I have a .netcore 3.1 api app created with the default WeatherForecast Controller in it. I have created the Dockerfile which looks like the below:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /build

COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o output

FROM mcr.microsoft.com/dotnet/core/sdk:3.1
WORKDIR /app
EXPOSE 5000

COPY --from=build-env /build/output .
ENTRYPOINT ["dotnet","AnotherSampleDockerExample.dll"]

Also I have the docker-compose.yml which looks like below:

version : '3'
services:
    api:
      image: dockerapi
      container_name: docker_api
      build:
        context: .
      ports: 
        - 8080:80

I am able to successfully create the image and the container by using the command "docker-compose up". But when I try to access it on the Chrome Browser with http://localhost:5000/WeatherForecast, the browser returns

"This site can’t be reached
localhost refused to connect." error. 

Need help here to debug this issue. Thanks!

1 Answers

As per your Dockerfile, you are exposing port 5000. Thus, in your docker-compose.yaml you should update the port mapping from 8080:80 to 8080:5000 i.e your file should like this:

version : '3'
services:
    api:
      image: dockerapi
      container_name: docker_api
      build:
        context: .
      ports: 
        - 8080:5000

On your machine, the site should now be reachable at http://localhost:8080/WeatherForecast

Related