简体   繁体   中英

docker is running but I am unable to connect using localhost

I have the following Dockerfile

FROM microsoft/dotnet:latest

WORKDIR /project

Which I need for a.Net webapi application. I have the following.Net configuration generated when running the dotnet command.

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:4900",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ContentBroker": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

The port for running the web API is set to use 5000 and when I run dotnet run I see the following warning, but the application starts

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Hosting environment: Development
Content root path: /project/ContentBroker
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

When running docker I use docker run -it -v $(pwd)/project:/project -p 5000:80...

But when I try to access the application from outside of docker using http://localhost/ I just see This site can't be reached .

I have tried to change the port used in the.Net configuration file. I have tried to change the port:port combination used when running docker and I tried to run 2 identical docker images, 1 running the.Net application on port 5000 and try to access it from the 2nd container using curl. But I get no access.

I usually never answer my own questions, but I think this is a legit answer.

After trying several times with the same result, I tried changing the launchSettings.json file. As mentioned, I use the same approach for a Go lang project where I use -p 80:80 when running the container and inside the container the Go based HTTP server has absolutely no configuration what so ever.

Below is the Dockerfile I am using to build the container

FROM microsoft/dotnet:latest

WORKDIR /project

Once built I run it using the following command

docker run -it -p 8080:5000 -v $(pwd):/project b049b9d625c9

And inside the container I created a new website using this command

dotnet new web --no-https

followed by dotnet run

The template "ASP.NET Core Empty" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on /project/project.csproj...
  Restore completed in 2.55 sec for /project/project.csproj.

Restore succeeded.

root@20e80795dbea:/project# dotnet run
Using launch settings from /project/Properties/launchSettings.json...
warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Hosting environment: Development
Content root path: /project
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

At this point I am not able to access the site. Then I changed the launchSettings.json from:

"project": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }

to:

"project": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://::5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }

and ran dotnet run again and this time I see the following output in the container

dotnet run
Using launch settings from /project/Properties/launchSettings.json...
Hosting environment: Development
Content root path: /project
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.

now, when accessing the site using http://localhost:8080/ I see the "Hello World," message printed, and I see the requests being logged in the container

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:8080/  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 54.3697ms 200 
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:8080/favicon.ico  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 1.367ms 200

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM