Unable to connect localhost in docker - 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

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.

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.

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>

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!

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.

It will be good if you could share the Docker version and OS you are working on. Meanwhile, please try these steps:
Check if your container is actually running on port 80
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
Maybe your app is on HTTPS. Try that as well

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.

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.

Related

Docker cannot access exposed port inside container

I have a container for which I expose my port to access a service running within the container. I am not exposing my ports outside the container i.e. to the host (using host network on mac). On getting inside the container using exec -t and running a curl for a post request, I get the error:
curl command: curl http://localhost:19999
Failed connect to localhost:19999; Connection refused.
I have the expose command in my dockerfile and do not want to expose ports to my host. My service is also up and running inside the container. I also have the property within config set as
"ExposedPorts": {"19999/tcp": {}}
(obtained through `docker inspect <container id/name>\ Any idea on why this is not working? Using docker for Mac
I'd post my docker-compose file too but this is being built through maven. I can ensure that I am exposing my port using 19999:19999. Another weird issue is that on disabling my proxies it would run a very light weight command for my custom service and wouldn't run it again returning the same error as above. The issue only occurs on my machine and not others
Hints:
The app must be listening on port 19999 which is probably not.
The EXPOSE that you're using inside the Dockerfile does nothing.
Usually there is no need to change the default port on which an application is listening, hence each container has its own IP and you shouldn't run in a port conflict.
Answer:
Instead of curling 19999 try to use the default port on which your app would normally be listening to(it's hard to guess what you are trying to run).
If you don't publish a port (with the docker run -p option or the Docker Compose ports: option), you cannot directly reach the container on Docker for Mac. See the Known limitations, use cases, and workarounds in the Docker Desktop for Mac documentation: the "per-container IP addressing is not possible" item ism what you're trying to attempt.
The docker inspect IP address is basically useless, except in one very specific Docker configuration (on a native-Linux host, calling from outside of Docker, on the same host); I wouldn't bother looking it up.
The Dockerfile EXPOSE directive and similar runtime options do very little and mostly serve as documentation. Even if you have that configured you still need to separately publish the port when you start the container to reach it from outside of Docker space.

Access service running in a container from browser

I'm using docker to run a test environment locally On Ubuntu 16.04. I spin up a container using the command -
fwx#asus:~$ docker run -p 3000:3000 -v ~/work/website/:/website -it test-env bash
This puts me inside the container where I do some initial setup and start the service like so -
root#c98d858cb1a4:/website# start-service
The service is configured to the endpoint http://127.0.0.1:3000/ and docker inspect on the container provides me the IP address 172.17.0.2.
As I understand from the multiple blog / forum posts addressing similar issues, I should be able to access the pages served through a browser on my host at http://172.17.0.2:3000. However, trying to access it thusly throws a connection error that the website cannot be reached.
I have tried various solutions proposed on similar questions including adding 172.17.0.0 to the route, but to no avail. Can someone please tell me what I'm doing wrong?
Whenever you create a docker container it will be assigned a private ip, To access the container you need to expose it which in your case is -p 3000:3000 try accessing the service from your host ip address and also try by disabling firewall.

Cannot access docker website by domain

I need help with configuring docker on Debian 9.
I installed docker and docker-compose successfully.
I can access my host by IP (ex. 172.18.0.7), but cannot access by domain name (sitename.loc). I see an error "ERR_NAME_NOT_RESOLVED" or "DNS_PROBE_FINISHED_NXDOMAIN".
Commands
$ docker-compose up -d
$ docker ps
works fine.
I tried disable firewall, it didn't help.
What's wrong? iptables?
Thanks in advance.
You can add the IP and name to your hosts file, but the container IP can change everytime you start it, so a better approach is to map the ports to your host, and then add to the hosts file this mapping:
sitename.loc 127.0.0.1

localhost not working docker windows 10

I am using VS2017 docker support. VS created DockerFile for me and when I build docker-compose file, it creates the container and runs the app on 172.x.x.x IP address. But I want to run my application on localhost.
I did many things but nothing worked. Followed the docker docs as a starter and building microsoft sample app . The second link is working perfectly but I get HTTP Error 404 when tried the first link approach.
Any help is appreciated.
Most likely a different application already runs at port 80. You'll have to forward your web site to a different port, eg:
docker run -d -p 5000:80 --name myapp myasp
And point your browser to http://localhost:5000.
When you start a container you specify which inner ports will be exposed as ports on the host through the -p option. -p 80:80 exposes the inner port 80 used by web sites to the host's port 80.
Docker won't complain though if another application already listens at port 80, like IIS, another web application or any tool with a web interface that runs on 80 by default.
The solution is to:
Make sure nothing else runs on port 80 or
Forward to a different port.
Forwarding to a different port is a lot easier.
To ensure that you can connect to a port, use the telnet command, eg :
telnet localhost 5000
If you get a blank window immediatelly, it means a server is up and running on this port. If you get a message and timeout after a while, it means nobody is running. You anc use this both to check for free ports and ensure you can connect to your container web app.
PS I run into this just a week ago, as I was trying to set up a SQL Server container for tests. I run 1 default and 2 named instances already, and docker didn't complain at all when I tried to create the container. Took me a while to realize what was wrong.
In order to access the example posted on Docker Docs, that you pointed out as not working, follow the below steps,
1 - List all the running docker containers
docker ps -a
After you run this command you should be able to view all your docker containers that are currently running and you should see a container with the name webserver listed there, if you have followed the docker docs example correctly.
2 - Get the IP address where your webserver container is running. To do that run the following command.
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" webserver
You should now get the IP address which the webserver container is running, hope you are familiar with this step as it was even available within the building Microsoft sample app example that you attached with the question.
Access the IP address you get once running the above command and you should see the desired output.
Answering to your first question (accessing docker container with localhost in docker for windows), in Windows host you cannot access the container with localhost due to a limitation in the default NAT network stack. A more detailed explanation for this issue can be obtained by visiting this link. Seems like the docker documentation is not yet updated but this issue only exists in Windows hosts.
There is an issue reported for this as well - Follow this link to see that.
Hope this helps you out.
EDIT
The solution for this issue seems to be coming in a future Windows release. Yet that release comes out this limitation is available in Windows host. Follow this link -> https://github.com/MicrosoftDocs/Virtualization-Documentation/issues/181
For those who encountering this issue in 2022, changing localhost to 127.0.0.1 solved an issue for me.
There is other problem too
You must have correct order with parameters
This is WRONG
docker run container:latest -p 5001:80
This sequence start container but parameter -p is ignore, therefore container have no ports mapping
This is good
docker run -p 5001:80 container:latest

Port binding is not working in docker on windows

I have installed docker on my Windows m/c.
I am trying to install Gerrit on that.
Pull image is done-Successfully
Run image is also done -->
docker run -d -p 8080:8080 -p 29418:29418 ******/gerrit
I try to connect it through browser with my container id:8080 but it throws error
This site can’t be reached
What is oing wrong.. Please help with suggestions.
BR,
Rash
You need to access your container by IP of virtual machine. You can obtain it with command: docker-machine ls. Then access container in browser by (replace ip) http://192.168.99.100:8080
This is a known limitation of windows containers at the moment as per the docker documentation (https://docs.docker.com/docker-for-windows/troubleshoot/#limitations-of-windows-containers-for-localhost-and-published-ports).
As of Windows 10 Creator's update this has kinda been fixed where you can use host IP with the bounded host port(http://<hostIp>:<hostBoundedPort>), but still not localhost or any of it's aliases.
Alternatively you can avoid port mapping hit the container IP directly. There is numerous ways to get your container IP. Personally I would use:
docker ps
This lists out all the the running docker containers allowing you to find the Container ID for the container that you want to hit followed by:
docker inspect <initial_part_or_full_id>
This will output low level information about the container, including it's Network settings where you will find the NAT-ed endpoint details containing the IP. Then simply http://<containerIP>:<containerPort>.

Resources