Cannot access asp .net core app when run with docker on local machine, or to kubernetes

10/10/2019

I have a very simple asp.net core app (C# Web Application with Docker Support for Linux) and when i build the docker image and try to run it on my local PC the following happens; In docker with my image called test, i type docker run test, at which point it states "Content root path: /app Now listening on: http://[::]:80" And even though when i type docker ps i can see the process running, when i try to navigate to localhost:80 all i get is a long wait and then "This site can’t be reached, localhost refused to connect."

I typed

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ec158cc3b344

which gave me the containers IP address, but even navigating directly to the container i either get "This site can’t be reached " if i navigate on port 80, or "Your connection was interrupted" if i try to access the IP directly.

I also tried to step over docker completely and deploy the image to Kubernetes to see if this would give me any luck, but instead when i try to access the services External-IP (In this case localhost), i get the following "This page isn’t working, localhost didn’t send any data."

I also tried to use

kubectl get pods -o wide

and access the IP's of the pods directly, but this just gives me "This 10.1.0.32 page can’t be found", for example.

And incase you're wondering, this is my dockerfile and kubernetes deployment .yml

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Test/Test.csproj", "Test/"]
RUN dotnet restore "Test/Test.csproj"
COPY . .
WORKDIR "/src/Test"
RUN dotnet build "Test.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Test.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Test.dll"]
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: <DockerEndpoint>.io/test:v5 #Sorry, can't include the real endpoint!
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: test
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: test

I also understand that .net core work in weird way that don't allow it to expose its ports to the outside world unless you tell it to, but that combined with my relative newness to the docker/kubernetes stack and leaving me bewildered.

Does anybody have any idea how i can make a .net core app, any app, work with docker?

P.S. I am really using such a simple app that even if i create a brand new .net core app with docker support, and try to immediately build and run the basic .net core app, it doesnt work. i cannot make it work with literally any .net core app!

-- Tom Baker
asp.net-core
c#
docker
kubernetes

2 Answers

10/10/2019

When it says listening on http://[::]:80, it's talking about localhost in the container. When you try to access it via http://localhost in your web browser running on your computer, localhost is your computer, not the container. You need to use the container's IP.

From your description, it sounds like you tried that as well, but there's no reason you should have any issues with that. You either didn't get the right IP or you did something else incorrect not detailed here.

-- Chris Pratt
Source: StackOverflow

10/10/2019

You have 8080 as your target port for port forwarding, but your application is listening on port 80. Change the targetport setting to 80 and you should be able to connect to your containerized application as expected.

You may need to adjust the containerPort setting as well.

-- Jonathon Chase
Source: StackOverflow