Home > Software engineering >  browse .net application in .net6 with docker cause a problem
browse .net application in .net6 with docker cause a problem

Time:11-24

I have this application as you can see that I run in IIS Express :

enter image description here

So I want to run it on docker so the docker file is generated :

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Docker.dll"]

So I create the image as you can see with this command :

docker build -t containername/tag -f docker/Dockerfile .

The image is created so I want to run my container using this :

docker run -d -p 42419:42419 --name apitest containername/tag:latest

The container is created but when I call this url http://localhost:42419/swagger/index.html I can't see the swagger page .Why?

CodePudding user response:

Your application needs to have the environment variable set to 'Development' for Swagger to be available. It's not available in production which is what Docker defaults to.

You can add the ASPNETCORE_ENVIRONMENT variable on your run command like this

docker run -d -p 42419:80 -e ASPNETCORE_ENVIRONMENT=Development --name apitest containername/tag:latest

You can also add it to the Dockerfile, if you want Development to be the default by adding

ENV ASPNETCORE_ENVIRONMENT=Development

somewhere in the 'final' part of the Dockerfile.

A third option is to modify your startup.cs file and remove the check for the environment variable when it sets up Swagger. That way, it'll always be available no matter what.

  • Related