1

I am trying to setup a dotnet core development environment with docker. I am referring to the following guide to set the same up. LINK

docker run --rm -it -p 8000:80 -v c:\git\dotnet-docker\samples\aspnetapp:/app/ -w /app/aspnetapp microsoft/dotnet:2.1-sdk dotnet watch run

The sample project runs on port 80. No issues here.

But when I scaffold a project using dotnet cli the application would run on port 5000, 5001 inside the container instead of 80.

dotnet new webapi

What makes the sample app run on port 80? I compared the following files but not find anything helpful here.

  • appsettings.Development.json
  • appsettings.json
  • aspnetapp.csproj
  • Program.cs
  • Startup.cs

Link to a GitHub for my code. LINK

Command on my project

docker run --rm -it -p 8000:80 -v c:\git\dotnet-docker-demo:/app/ -w /app microsoft/dotnet:2.1-sdk dotnet watch run

EDIT:

There is no docker file involved here. I am trying to use dotnet watch run directly on the source. I don't want to build dll and then run it in the container.

Saurabh Harwande
  • 161
  • 6
  • 18
  • 1
    Possible duplicate of [Why does aspnet core start on port 80 from within Docker?](https://stackoverflow.com/questions/48669548/why-does-aspnet-core-start-on-port-80-from-within-docker) – omajid Aug 07 '18 at 18:48
  • @omajid I have went through the question. But for me the scenario is different. I am using the very same docker image to run both the applications, but one does not start the application on port 80. Also, note that I am not using any _Dockerfile_ I am directly running the image as is. – Saurabh Harwande Aug 07 '18 at 18:58

2 Answers2

0

You can mention the port number in your docker file, I have highlighted with an arrow in the below docker image file

# FROM microsoft/dotnet:2.0-sdk AS build
FROM microsoft/dotnet:2.0-sdk
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY TodoService/*.csproj ./TodoService/
RUN dotnet restore

# copy everything else and build app
COPY TodoService/. ./TodoService/
WORKDIR /app/TodoService
RUN dotnet publish -o out /p:PublishWithAspNetCoreTargetManifest="false"


# FROM microsoft/dotnet:2.0-runtime AS runtime
FROM microsoft/dotnet:2.0-runtime
ENV ASPNETCORE_URLS http://+:80  # <----------------
WORKDIR /app
COPY --from=0 /app/TodoService/out ./

ENTRYPOINT ["dotnet", "TodoService.dll"]
skjagini
  • 3,142
  • 5
  • 34
  • 63
  • Dockerfile is not being used to run the container. We are directly passing the base dotnet-sdk image and command+args as parameter to docker run command. – Saurabh Harwande Aug 08 '18 at 02:02
  • Also this would build a dll and run it. I want to use the _dotnet watch run_ command on the source directly. That is when the problem occurs. – Saurabh Harwande Aug 08 '18 at 02:12
0

The value of the environment variable ASPNETCORE_URLS decides which port is used to bind the HTTP listener to. This variable can be set using a shell or inline with dotnet run:

ASPNETCORE_URLS=http://+8080 \
dotnet run

Another technique to define URLs whilst developing locally is by using a launchSettings.json as described in DOCS here

example from docs:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:54339/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_My_Environment": "1",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    },
    "EnvironmentsSample": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:54340/"
    },
    "Kestrel Staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_My_Environment": "1",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:51997/"
    }
  }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Sudhanshu Mishra
  • 6,523
  • 2
  • 59
  • 76