skip to Main Content

When running a Standard ASP.NET Core Web API project created by Visual Studio 2022 with docker option enabled, connecting to the server via browser is possible by using https://localhost:65215/swagger/index.html (which even pops up by itself).

When running the container with specifying the mapping of port 80 and 443 (such as 65214 and 65215), reaching Swagger is not possible on the specified ports. So there must clearly be more to it.

How can one run the docker container from the console, using a docker run command? And how can the port be set or at least identified?

Dockerfile

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

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

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

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

Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) {
    app.UseSwagger();
    app.UseSwaggerUI(); }

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Project creation

Standard ASP.NET Core Web API created by Visual Studio 2022

2

Answers


  1. By default, Swagger is only available when you run your app in the ‘development’ environment.

    By default, a containerized app runs in production.

    To change it, you set the ASPNETCORE_ENVIRONMENT variable to ‘Development’. You can either do that on the docker run or you can set it in the Dockerfile.

    As for the ports, Microsoft set ASPNETCORE_URLS to http://+:80 in the aspnet images, causing the app to listen on port 80.

    Add a line to set ASPNETCORE_ENVIRONMENT to your Dockerfile like this

    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    WORKDIR /app
    ENV ASPNETCORE_ENVIRONMENT Development
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    WORKDIR /src
    COPY ["WebApiContainerized/WebApiContainerized.csproj", "WebApiContainerized/"]
    RUN dotnet restore "WebApiContainerized/WebApiContainerized.csproj"
    COPY . .
    WORKDIR "/src/WebApiContainerized"
    RUN dotnet build "WebApiContainerized.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "WebApiContainerized.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "WebApiContainerized.dll"]
    

    Build and run with

    docker build -t myimage .
    docker run -d -p 65214:80 myimage
    

    You should now be able to reach the Swagger page at http://localhost:65214/swagger

    Login or Signup to reply.
  2. Assuming your code contains something like this…

    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
    }
    

    … Swagger will only be available in development environment mode that is controlled by the ASPNETCORE_ENVIRONMENT environment variable.

    The easiest is to set this to Development as part of your run command:

    docker run -p 65215:80 -e ASPNETCORE_ENVIRONMENT=Development yourimage:yourtag
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search