0

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
  • 1
    Which port/url are you using to access the API? Whats the output on CMD after executing docker run? Commented Mar 22, 2022 at 5:52
  • Share your launchSettings.json Commented Mar 22, 2022 at 12:03

2 Answers 2

1

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"]
Sign up to request clarification or add additional context in comments.

Comments

0

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.