skip to Main Content

I am deploying an API in my local docker using Docker Desktop for Windows. My API is running well on Visual Studio. I also tried deploying it on IIS and is working. But when I deployed it on Docker, I cannot view it on browser and cannot call the endpoints on Postman.

Here is my docker build and run commands:

docker build -f docker/local/Dockerfile -t kc.api.prototype
docker run -d --name kc.api.prototype --restart=unless-stopped -p 10230:80 kc.api.prototype

This runs without error. The container is also running. The only problem is that it cannot be seen on browser (I am using Swagger) and endpoints cannot be used.

Here is my DockerFile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-local

WORKDIR /app

COPY ./kc-api-prototype ./

RUN dotnet restore *.csproj -s https://api.nuget.org/v3/index.json

RUN dotnet publish *.csproj -c Dev -o out

# build runtime image
# FROM microsoft/dotnet:aspnetcore-runtime REMOVED: Upgraded to .NET Core 3.1
FROM mcr.microsoft.com/dotnet/sdk:6.0
RUN apt-get update 
    && apt-get install -y --allow-unauthenticated 
        libc6-dev 
        libgdiplus 
        libx11-dev 
     && rm -rf /var/lib/apt/lists/*  
WORKDIR /app
COPY --from=build-local /app/out ./

ENTRYPOINT ["dotnet", "kc-api-prototype.dll"]

I already tried removing app.UseHttpsRedirection() as suggested on other posts. I haven’t tried disabling SSL since I cannot access the property on Visual Studio 2022.

2

Answers


  1. Chosen as BEST ANSWER

    I already found out that the cause of the issue is mainly this line in the final image base:

    FROM mcr.microsoft.com/dotnet/sdk:6.0
    

    I have to change it to this for .netcore 6.0:

    FROM mcr.microsoft.com/dotnet/aspnet:6.0
    

    The new dockerfile would look like this:

    # FROM microsoft/dotnet:sdk AS build-env REMOVED: Upgraded to .NET Core 6.0
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-local
    
    WORKDIR /app
    
    COPY ./kc-api-prototype ./
    
    RUN dotnet restore *.csproj -s https://api.nuget.org/v3/index.json -s
    
    RUN dotnet publish *.csproj -c Dev -o out
    
    # build runtime image
    # FROM microsoft/dotnet:aspnetcore-runtime REMOVED: Upgraded to .NET Core 6.0
    FROM mcr.microsoft.com/dotnet/aspnet:6.0
    RUN apt-get update 
        && apt-get install -y --allow-unauthenticated 
            libc6-dev 
            libgdiplus 
            libx11-dev 
         && rm -rf /var/lib/apt/lists/*  
    WORKDIR /app
    COPY --from=build-local /app/out ./
    
    ENTRYPOINT ["dotnet", "kc-api-prototype.dll"]
    

  2. You need to set the ASPNETCORE_ENVIRONMENT environment variable to ‘Development’ for Swagger to be available. You can do it on the docker run command or you can do it in your Dockerfile like this

    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-local
    WORKDIR /app
    
    COPY ./kc-api-prototype ./
    
    RUN dotnet restore *.csproj -s https://api.nuget.org/v3/index.json
    
    RUN dotnet publish *.csproj -c Dev -o out
    
    # build runtime image
    # FROM microsoft/dotnet:aspnetcore-runtime REMOVED: Upgraded to .NET Core 3.1
    FROM mcr.microsoft.com/dotnet/sdk:6.0
    ENV ASPNETCORE_ENVIRONMENT=Development
    RUN apt-get update 
        && apt-get install -y --allow-unauthenticated 
            libc6-dev 
            libgdiplus 
            libx11-dev 
         && rm -rf /var/lib/apt/lists/*  
    WORKDIR /app
    COPY --from=build-local /app/out ./
    
    ENTRYPOINT ["dotnet", "kc-api-prototype.dll"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search