Search code examples
dockerkubernetes.net-8.0rancher

.NET 8 app in Rancher only listening on http port


I have a seemingly basic question, however after searching, and adding https_port as an environment variable, appsettings toplevel property, and other attempts, whenever I deploy my app to Rancher, it only listens for http on port 8080.

It's a simple web app, with no real functionality, other than testing authorization via keycloak, hence the demand for all traffic being https.

I would expect the startup logs to include "Now listening on: https://[::]:8081" or some other port for https traffic.

Instead I get:

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
2024-09-20T09:50:14.146416699Z       Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed. For more information go to https://aka.ms/aspnet/dataprotectionwarning
2024-09-20T09:50:14.540842462Z warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
2024-09-20T09:50:14.540881640Z       No XML encryptor configured. Key {b25d69c8-24c9-4281-b5df-71ab43c8182f} may be persisted to storage in unencrypted form.
2024-09-20T09:50:15.340432431Z info: Microsoft.Hosting.Lifetime[14]
2024-09-20T09:50:15.340467342Z       Now listening on: http://[::]:8080
2024-09-20T09:50:15.340471006Z info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
2024-09-20T09:50:15.340475874Z info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
2024-09-20T09:50:15.340480847Z info: Microsoft.Hosting.Lifetime[0]
2024-09-20T09:50:15.340483241Z       Content root path: /app

I expect I'm missing something obvious, regarding .NET 8.

My deployment file looks like this (with some values replaced with dummy values):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myPOCsite
  namespace: backend
spec:
  selector:
    matchLabels:
      app: myPOCsite
  replicas: 1
  template:
    metadata:
      labels:
        app: myPOCsite
        log: "yes"
    spec:
      containers:
      - name: myPOCsite
        image: myimageurl
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8081
        resources:
          limits:
            memory: "100Mi"
            cpu: "80m"
          requests:
            memory: "60Mi"
            cpu: "40m"
      hostname: myPOCsite
      imagePullSecrets:
      - name: regsecret
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: myPOCsite
  name: myPOCsite
  namespace: backend
spec:
  ports:
  - port: 443
    protocol: TCP
    name: https
    targetPort: 8081
  selector:
    app: myPOCsite
  type: ClusterIP

Solution

  • In their Docker images, Microsoft set the environment variable ASPNETCORE_HTTP_PORTS to 8080 which causes your app to listen for HTTP on that port. Their Dockerfile for the runtime-deps image is here and that image is used as the base for the aspnet image.

    Listening on port 8080 for HTTP traffic is the default behaviour for a Dockerized ASP.NET application.

    If you want it to listen on another port and/or listen for HTTPS, you need to configure it as described here.

    Be aware that since Microsoft configure a port using an environment variable, they make it impossible to configure your ports using appsettings.json since the environment variable overrides appsettings.