13

I run the images in docker terminal:

docker run -p 4000:80 friendlyhello

Localhost does not connect and display images.

This site can’t be reached
localhost refused to connect.
- Did you mean http://localhost4000.org/?
- Search Google for localhost 4000
ERR_CONNECTION_REFUSED

This site can’t be reached

Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
Ankita shah
  • 161
  • 1
  • 1
  • 4
  • try `http://localhost:4000/` – Black May 10 '17 at 06:28
  • That doesn't work. Localhost hasn't been responding to any of the images I run in Docker. But they work for other applications like Google AppEngine. – Ankita shah May 10 '17 at 06:30
  • where are you using the link? in your host machine or within container? if you use in the host machine, this will not work. – Thangadurai May 10 '17 at 06:32
  • In my host machine. What should I do to make it work? – Ankita shah May 10 '17 at 07:01
  • 1
    If you run `docker ps` does the container show up as running? Also what's the output of `docker version`? If you're using [Docker for Windows](https://docs.docker.com/docker-for-windows/) you won't have access to localhost, [Published Ports On Windows Containers Don't Do Loopback](https://blog.sixeyed.com/published-ports-on-windows-containers-dont-do-loopback/) – Mano Marks May 10 '17 at 15:46

9 Answers9

14

i think maybe you visit http://localhost:4000 in browser on Windows,then you should use the docker default machine ip(generally 192.168.99.100). just try http://192.168.99.100:4000.

zoe
  • 141
  • 2
  • 3
  • Thanks for this suggestion. `192.162.99.100` works for me on Virtual Box using Linux as well. But localhost - no. – invzbl3 Aug 25 '19 at 19:37
5

I had this problem too, solved following this part of the documentation:

Note: If you are using Docker Toolbox on Windows 7, use the Docker Machine IP instead of localhost. For example, http://192.168.99.100:4000/. To find the IP address, use the command docker-machine ip.

Thiago Brauer
  • 321
  • 4
  • 9
4

If you running a nodejs app in the docker container, try '0.0.0.0' instead of 'localhost'.

example: suppose your app works on port 3000

server.listen(3000, 'localhost' () => {

console.log('listening for requests on port 3000');

});


server.listen(3000, '0.0.0.0' () => {
    
console.log('listening for requests on port 3000');
    
});

then you can do port-mapping in docker to your web app.

docker run -p 4000:3000 --name 'your_container_name' 'your_image_name'

start the container and see the port using the below command on your cmd or terminal.

docker port <your_container_name>
Kalhara Tennakoon
  • 1,302
  • 14
  • 20
2

I also had this error using docker for windows.

Despite numerous attempts it wasnt resolved by playing with the port numbers and image rebuilds, or complete wipe of the docker container.

It took me a while to resolve so want to save you all some time.

1- enter the command Run docker network ls

Look for output similar to the below-

   Network ID       Name   Driver    Scope
   cd6a217449e3     nat    nat       local

2- Copy the network ID

3-enter the command docker network inspect THENETWORKID

4- You will receive some outpuut to screen which looks like a json output, look for text similar to the below:-

"Containers": {

"4b953b6a597e38eac1de39119d30fb4c87bca6faae7da444b02e223685eed5c7": {

"Name": "jolly_rosalind",

"EndpointID": "5919da54af323505e18d9807742fd12bb3acbe260dcee28911ffdf56fb905baf",

"MacAddress": "00:15:5d:3c:50:58",

"IPv4Address": "172.30.49.218/16",

"IPv6Address": ""

}

5- enter 'http://THEIPV4ADDRESS:5000/' in your web browser, and you may be presnted with a beautiful '0'.

Good Luck!

brass monkey
  • 5,841
  • 10
  • 36
  • 61
Sim
  • 21
  • 2
1

A short solution that work fine for me with "Docker Desktop".

  1. Open a command line window run cmd
  2. enter netstat and validate

You will see all listener ip/port name

TCP    127.0.0.1:50684        kubernetes:65001       ESTABLISHED
...
TCP    192.168.0.15:53796     123:https              ESTABLISHED
...

I have test with 192.168.0.15: and all work fine.

Arnaud.P
  • 11
  • 2
0

With that docker run command, you should access to the container from your host using http://127.0.0.1:4000 as @Black said on comments. Anyway, your "friendlyhello" image what exactly is? because there are a lot of "friendlyhello" images on dockerhub, but yours is not any of them. There is no official friendlyhello image. Can you put your Dockerfile? Anyway, it is suppossed that a standard "friendlyhello" will run a web server on port 80 with a Hello World welcome page. It should work.

I already tested with docker run -p 4000:80 movila/friendlyhello and is working for me.

If is not working for you, it sounds like you have some kind of iptables problem. Reboot your machine in order to restore iptables rules (docker does it automatically) and try again.

Other possibility is to access directly to the container's ip instead of hosts ip. When you launch your docker container, if you don't specify -d parameter, it's going to get your terminal to print its log. On that log there is an ip. That ip is the container's ip. Example of my log:

* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
172.17.0.1 - - [10/May/2017 07:13:53] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [10/May/2017 07:13:53] "GET /favicon.ico HTTP/1.1" 404 -

You can try to use http://172.17.0.1 to access to it.

OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51
  • I tried exactly what you mentioned without using `d` `* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)` After this line I see no logs I checked with IP of the controller but i get time out error in browser `http://172.17.0.2/4000` `This site can’t be reached 172.17.0.2 took too long to respond. Search Google for 4000 ERR_CONNECTION_TIMED_OUT` – Ankita shah May 10 '17 at 08:48
0

It will be good if you could share the Docker version and OS you are working on. Meanwhile, please try these steps:

  1. Check if your container is actually running on port 80
  2. Instead of using http://localhost:4000, try http://{CONTAINER_IP}:4000/ you should get your container IP by

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

  1. Maybe your app is on HTTPS. Try that as well
humblebee
  • 1,044
  • 11
  • 25
0

I resolved this situation on MacOS by installing and starting docker-machine This is because a Virtual Machine is required to run docker on MacOS.

An alternative is to use Docker Desktop.

robrecord
  • 504
  • 5
  • 15
0

Even I was facing same problem and after implement the below solution where I replaced the "localhost" with docker ip address and it worked fine for me. Just assigned the port and use below url http://192.168.99.100:5050/ instead of http://localhost:5050 . Just replce your port number with "5050", it will work fine.

i think maybe you visit http://localhost:4000 in browser on Windows,then you should use the docker default machine ip(generally 192.168.99.100). just try http://192.168.99.100:4000.