-1

I can curl localhost:5000 inside container but not from outside even when port binding added as 5000:5000

pramodjangam@Pramods-MacBook-Pro:~/code/helloworld$ curl localhost:5000/WeatherForecast
curl: (52) Empty reply from server

pramodjangam@Pramods-MacBook-Pro:~/code/helloworld$ docker ps
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                              NAMES
5f0c986867d9        kitematic/hello-world-nginx:latest   "sh /start.sh"           10 minutes ago      Up 10 minutes       0.0.0.0:32768->80/tcp              hello-world-nginx
1200a6c8c7df        helloworlddotnet                     "/bin/sh -c out/Hell…"   19 minutes ago      Up 19 minutes       0.0.0.0:5000-5001->5000-5001/tcp   great_haslett

pramodjangam@Pramods-MacBook-Pro:~/code/helloworld$ docker exec -it 1200a6c8c7df bash

root@1200a6c8c7df:/# curl localhost:5000

root@1200a6c8c7df:/# curl localhost:5000/WeatherForecast
[{"date":"2019-12-07T19:00:43.0919669+00:00","temperatureC":5,"temperatureF":40,"summary":"Balmy"},{"date":"2019-12-08T19:00:43.0920037+00:00","temperatureC":13,"temperatureF":55,"summary":"Cool"},{"date":"2019-12-09T19:00:43.0920128+00:00","temperatureC":52,"temperatureF":125,"summary":"Warm"},{"date":"2019-12-10T19:00:43.0920303+00:00","temperatureC":-3,"temperatureF":27,"summary":"Balmy"},{"date":"2019-12-11T19:00:43.0920383+00:00","temperatureC":46,"temperatureF":114,"summary":"Balmy"}]root@1200a6c8c7df:/# 
root@1200a6c8c7df:/# exit
exit
Pramod Jangam
  • 316
  • 3
  • 10
  • What "flavor" of Docker are you using (the "whale icon" Docker for Mac or Docker Toolbox)? What's the process inside the container, and what does it print on startup? (If it says something like "listening on 127.0.0.1:5000" then it won't be reachable from outside the container.) – David Maze Dec 06 '19 at 19:31
  • @DavidMaze It is dotnet webapi. It does say `listening on: http://localhost:5000` Why won't port be reachable in that case? – Pramod Jangam Dec 06 '19 at 19:35
  • Docker flavor: docker desktop community 2.1.0.5 – Pramod Jangam Dec 06 '19 at 19:44
  • 2
    I have a pretty detailed answer at https://stackoverflow.com/a/59182290/10008173. I can't speak to .Net specifics, but as a general rule you need to set processes inside containers to be listening on 0.0.0.0 ("all interfaces") and not 127.0.0.1 ("only accessible from this container"). – David Maze Dec 06 '19 at 19:50

3 Answers3

1

I have ran into this sort of issues. Please make sure that your dotnet application running inside your docker container is also listening on all network interfaces.

For instance, whenever I run a Django application (in dev mode), I always make sure to see a message like this:

Starting development server at http://0.0.0.0:8000/

The key here is 0.0.0.0:8000 which indicates that my app, inside the container, is listening on all network interfaces.

Another option is to run your container with host networking mode (https://docs.docker.com/network/network-tutorial-host/)

Yoanis Gil
  • 3,022
  • 2
  • 15
  • 22
0

I am having the same problem. It seems to be a dotnet 3.1 problem. When I start the app in the container it only binds to localhost:5000 and not to the other network interface. The problem is that localhost is always only reachable from the host (or in this case the container) itself, even if you port forward in docker it will not work.

I tried adding

.UseUrls("http://0.0.0.0:5000")

to the hostbuilder to make the app listen to all available network devices but it does not work, neither does adding:

ENV ASPNETCORE_URLS http://*:5000

This to the Dockerfile or

environment:
 - ASPNETCORE_URLS=http://*:5000

to the docker-compose.

These options used to work fine in dotnet 2 but in 3.1 they do not have any effect. I also tried 0.0.0.0 instead of * nothing seems to work.

So basically dotnet always starts the Server on localhost (even on my developmachine) which makes your app only reachable from the host it is running on.

PWFraley
  • 1,052
  • 12
  • 20
-1

Inside the container, localhost refers to the container. Outside the container, localhost is your machine, not the container. When you want to access something running in the container outside the container, you need to use the container's IP address, not localhost.

You can get the container's IP address using:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [container_name_here]
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • This gives me IP address. But when I try with that IP address, I get a timeout error. `Failed to connect to 172.17.0.2 port 5000: Operation timed out` – Pramod Jangam Dec 06 '19 at 19:25
  • You cannot reach this IP address from a Mac host (or a Windows host, or from the host that's not running the Docker daemon itself, or in any environment using Docker Machine or Docker Toolbox). – David Maze Dec 06 '19 at 19:30