Search code examples
c#.netdockerasp.net-coreazure-devops

.NET Core API: "local host refused to connect " when run via Docker


I am trying to run a simple web api via docker.

I have followed all the steps mentioned in the docs.

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

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

Screenshot:

enter image description here enter image description here

Commands that I am using to run api via docker:

docker build -t aspnetapp .
docker run -d -p 8080:80 --name myapp aspnetapp

Please note that the api works when I execute the following commands in command line so there is likely nothing wring with the api itself:

dotnet publish -c Release -o out
dotnet run out/ApiForDocker.dll

EDIT:

enter image description here

launchSettings

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:63852",
      "sslPort": 44318
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ApiForDocker": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Solution

  • It looks right to me.

    A useful debugging option is to add RUN ls -al to your runtime step to make sure your are targeting the right app-name.dll entry.

    # Build runtime image
    FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
    WORKDIR /app
    COPY --from=build-env /app/out .
    RUN ls -al
    ENTRYPOINT ["dotnet", "aspnetapp.dll"]
    

    Then look for the output during build. Or you can ssh into the a running container to verify it.

    $ docker exec -it container-id bash.

    Also, you can always try switching to another port in Properties > launchSettings.json and then update your Dockerfile entrypoint with that port and --urls-args like:

    ENTRYPOINT ["dotnet", "aspnetapp.dll", "--urls", "http://*:6001"]

    Then you also need to map that port on your host machine -p 8080:6001. Also make sure your container is listening to that interface as well (0.0.0.0).

    Update

    Now when you have provided your launchSettings:

    "ApiForDocker": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
    

    You can update your docker entrypoint to:

    ENTRYPOINT ["dotnet", "aspnetapp.dll", "--urls", "http://*:5000"]

    and map the container on your host machine with -p 5000:5000 and then localhost:5000/api/values should give you your API GET response on that route in your browser.