Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect localhost in docker

Tags:

docker

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

like image 390
Ankita shah Avatar asked May 10 '17 06:05

Ankita shah


People also ask

How can Docker container connect to localhost?

You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

What is localhost IP from Docker container?

Ok, your localhost server has a default docker interface docker0 with IP address 172.17. 0.1 .

Does Docker use port 8080?

Docker also finds ports you expose with --expose 8080 (assuming you want to expose port 8080). Docker maps all of these ports to a host port within a given epehmeral port range . You can find the configuration for these ports (usually 32768 to 61000) in /proc/sys/net/ipv4/ip_local_port_range .


3 Answers

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.

like image 138
zoe Avatar answered Oct 12 '22 03:10

zoe


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.

like image 42
Thiago Brauer Avatar answered Oct 12 '22 02:10

Thiago Brauer


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>
like image 22
Randil Tennakoon Avatar answered Oct 12 '22 02:10

Randil Tennakoon