skip to Main Content

So this is my setup:

  • .NET Minimal APIs running in a Docker Container
  • Keycloak running in a Docker Container

In the development environment of my minimal-Api I can make a Http Request against the Keycloack Container to get an AccessToken

After running my api in a Docker Container the request isn’t working anymore.
After the making the request I get the Error:

"Address not available (localhost:8080)"

This is how I do the Request in the Api:

this is the Dockerfile to build the Image for the minimal Api:

FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build-env
WORKDIR /app

COPY . ./
RUN dotnet restore
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine
WORKDIR /app
COPY --from=build-env /app/out .

ENTRYPOINT ["dotnet", "projectName.dll"]

2

Answers


  1. Chosen as BEST ANSWER

    So this kinda helped me. Yes its not the localhost. Better solution is to look into the Ip-Address which is given to the container by docker. You can do it by using: docker inspect <container-name/id> look for the IP Address


  2. localhost:

    In computer networking, localhost is a hostname that refers to the current computer used to access it. The name localhost is reserved for loopback purposes.

    When you will run your app in the docker container localhost will mean the container itself, not the host machine, and your container obviously does not have docker installed. If you are running on Windows then you can use host.docker.internal to reach outside the container i.e. host.docker.internal:8080 (maybe it is supported on other systems but can’t verify this now). If this does work you can try getting acquainted with docker networking (and this) and leveraging it.

    Notes and see also:

    1. Since .NET 8 the default ASP.NET Core port configured in .NET container images has been updated from port 80 to 8080. so if you will upgrade then you will need to act accordingly
    2. docker-compose refused connection to other container in same network
    3. Common way to set up local infrastructure for testing is to use docker compose, for example How to connect an ASP.NET backend to a postgres database inside a docker container
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search