Home > Net >  API responds locally (vs 2019) but not in local docker container
API responds locally (vs 2019) but not in local docker container

Time:10-04

Good Morning,

I am new to docker and going through documentation and Pluralsight videos. I have a really simple API I am practicing/learning to run as a docker container. Everything builds and runs but it doesn't respond to postman request.

However when I run the project in VS 2019 it responds to postman?

Docker desktop 4.X Linux containers VS 2019

API

[HttpGet("basic")]
[AllowAnonymous]
public IActionResult Basic()
{
   return Ok("Alive");
}

postman

GET http://localhost:5150/HealthCheck/basic

docker compose

version: "3"

services:
  api:
    #snipped container name etc for brevity
    ports:
        - "5150:5150"
    networks:
        - backend                    
networks:
  backend:

dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 5150
#snipped brevity
ENTRYPOINT ["dotnet", "TestingDocker.API.dll"]

launch settings

{
  "iisSettings": {
  "windowsAuthentication": false,
  "anonymousAuthentication": true,
  "iisExpress": {
    "applicationUrl": "http://localhost:5150/",
    "sslPort": 0
  }
},

Compose output

[12:49:29 INF] Now listening on: http://[::]:80

I don't understand where it get's the port 80 from.

Docker Desktop

Running Port:5150

Docker Inspect on the image

"StdinOnce": false,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "ASPNETCORE_URLS=http:// :80",
            "DOTNET_RUNNING_IN_CONTAINER=true",
            "DOTNET_VERSION=5.0.10",
            "ASPNET_VERSION=5.0.10"
        ],

I read something in troubleshooting this about how windows docker desktop hyper-v's have their own IP address so I tried to find that and see if that would respond but all I could find was the entire subnet defined in docker desktop settings. 192.168.65.0/24

When I look at the hyper v virtual nic configs they have an ip of a totally different subnet. 172.19.240.1

So then I pulled a mongo image and it DOES respond. When I run inspect on it the only network related difference I can see it that mongo has a hostname.

So I'm sure it is something with my network settings but I can't figure it out.

TIA

Edit--------------------

per Camilo's comment I added back the Entrypoint I had tried earlier.

ENTRYPOINT ["dotnet", "DockerTest.API.dll", "--server.urls", "http://0.0.0.0:5150"]

I thought 0.0.0.0 was a wildcard ip binding but that didn't work so I switched to suggested

ENTRYPOINT ["dotnet", "DockerTest.API.dll", "--urls", "http:// :5150"]

And that worked!!

One thing to note. All these tiny little changes didn't seem to be getting picked us as supposedly a new image was building in like .03 seconds. So to be sure my changes throughout this process were being picked up I used

docker-compose build --no-cache

docker-compose up -d --force-recreate

CodePudding user response:

You have an environment variable like:

"ASPNETCORE_URLS=http:// :80"

That one is created automatically by ASP.NET Core when it's not given any specific URL to use, which is the case in this question.
Your confusion probably comes from the launch.settings.json file, which appears to be configuring the app to run on port 5150, but that is not correct for 2 reasons:

  1. The file is only for Visual Studio debugging, and it's not published with the application, and
  2. Even if the file was used, you're configuring the iisExpress settings, and IIS Express doesn't exist on Linux.

You have a few ways to solve this issue:

  • Add a Docker map for the port, as mentioned by @OneCricketeer, by using 5150:80.
  • Specify the URL to use as part of the ENTRYPOINT:
ENTRYPOINT ["dotnet", "DockerTest.API.dll", "--urls", "http:// :5150"]
  • As well as others, as specifying the URLs directly in your Program, when building the WebHost.

CodePudding user response:

Those logs are internal to the container. It's not clear how you've told your server to run on 5150, but EXPOSE doesn't change the code behavior, and there appears to be this ASPNETCORE_URLS environment variable which controls the server binding, and you've not overridden this anywhere

If you simply want to use localhost:5150, with no other changes, you would need to use 5150:80 as your compose ports definition

  • Related