skip to Main Content

I am a beginner in Docker, and I have a problem When I am trying to run Image Docker with a C# Project.
In my Asp.net net8 project, there is a basic Dockerfile such as this:

#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["MyApiProject/MyApiProject.csproj", "MyApiProject/"]
RUN dotnet restore "./MyApiProject/MyApiProject.csproj"
COPY . .
WORKDIR "/src/MyApiProject"
RUN dotnet build "./MyApiProject.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./MyApiProject.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

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

When I am running the project from the Run button (by Visual studio), It’s working fine. The image is created, The container is running, and I can see it in the Browser with swagger on the specific container ports.
But, When I run it from PowerShell, It’s wrong. The container is running, But the result of the URL with swagger is:

This page is not working 
localhost did not send data.
ERR_EMPTY_RESPONSE.

The status of cantainer is runnig, but log is empty.

I am trying some things, But I can’t resolve this problem.
I used one Docker Image with 2 containers, and ran it from PowerShell cmd.
I copy the Docker run command from output window of Visual Studio and run it by Powershell:

docker run -dt -v "C:…vsdbgvs2017u5:/remote_debugger:rw" -v
"C:…AppDataRoamingMicrosoftUserSecrets:/root/.microsoft/usersecrets:ro"
-v "C:…AppDataRoamingMicrosoftUserSecrets:/home/app/.microsoft/usersecrets:ro"
-v "C:…AppDataRoamingASP.NETHttps:/root/.aspnet/https:ro" -v "C:…AppDataRoamingASP.NETHttps:/home/app/.aspnet/https:ro" -v
"C:Program FilesMicrosoft Visual
Studio2022CommunityMSBuildSdksMicrosoft.Docker.Sdktoolslinux-x64net8.0:/VSTools:ro"
-v "C:Program FilesMicrosoft Visual Studio2022CommunityCommon7IDECommonExtensionsMicrosoftHotReload:/HotReloadAgent:ro"
-e "ASPNETCORE_LOGGING__CONSOLE__DISABLECOLORS=true" -e "ASPNETCORE_ENVIRONMENT=Development" -P –name MyApiProject_1
–entrypoint dotnet myapiproject –roll-forward Major /VSTools/DistrolessHelper/DistrolessHelper.dll –wait

But still is existing some problem.
Why can’t I run .net Docker application from PowerShell?
Maybe, If I will understand the behavior of the RUN button in Visual Studio, I can solve it?

Thank’s

2

Answers


  1. You can’t directly access the ports of a service in a container unless you publish those ports.

    You’re using the docker run -P option, with a capital P. This takes every port that’s exposed in the Dockerfile, and publishes it on a random host port. You can use the docker port command to find out what those ports are

    $ docker port MyApiProject_1
    8000/tcp -> 0.0.0.0:32768
    8001/tcp -> 0.0.0.0:32769
    

    and then you can use the Docker-chosen port number to interact with the service; http://localhost:32768/ in this example.

    This setup is all but guaranteed to not have port conflicts, and you can run multiple copies of the container, but in many cases you’ll find it easier to pick a port you know. If you change this option to

    docker run -p 8765:8000 ...
    

    then port 8765 on the host will forward to port 8000 in the container. The container will fail to start up if something else on the host is already using that port. The port numbers in practice are frequently the same, but they don’t need to be.

    (Depending on what you’re trying to do, you may not need the docker run --entrypoint dotnet option or the --roll-forward Major ... command part; these override the default ENTRYPOINT in the Dockerfile. You may not need the docker run -v options to bind-mount IDE components into the container either.)

    Login or Signup to reply.
  2. In addition to the ports mentioned by David, you also need to configure the ASP.NET environment.

    If you look at your Program.cs you should see:

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

    This makes it so the swagger pages only respond when in the development environment. You can pass an environment variable when running the container to configure the ASP.NET environment:

    docker run -it -P -e ASPNETCORE_ENVIRONMENT=Development webapitst
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search