docker-machine: Can't access container's web server from host - docker

I just installed Docker with Docker-Toolbox on my Mac using homebrew: install docker with homebrew
After creating and configuring a Container with Rails, Postgres and starting docker-compose up everything looks fine but i can't access the webserver from host.
The output of
$ docker-compose up
dummy_1 | I, [2016-03-30T14:55:53.130639 #6] INFO -- : listening on addr=0.0.0.0:8000 fd=10
When i type in Google Chrome the url http://0.0.0.0:8000/ i get
This site can’t be reached
0.0.0.0 refused to connect.
ERR_CONNECTION_REFUSED
So i tried
$ docker-machine env dummy
with the following output:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/choi/.docker/machine/machines/dummy"
export DOCKER_MACHINE_NAME="dummy"
When i try in Chrome http://192.168.99.100:2376 i get a blank file downloaded. Why is it so? I expect the default greeting page of my Rails App.

192.168.99.100 is the IP of your Docker host, in this instance. You need to expose the port of your container and then you will be able to connect to it from the outside world.
I'm not familiar with Docker Compose, but the log you have posted suggests port 8000 is exposed. Try, therefore, http://192.168.99.100:8000.
(The reason http://192.168.99.100:2376 doesn't work is because that's the address and port of the Docker daemon itself, which isn't HTTP-based. As for 0.0.0.0: This is the address which your web server is listening on inside the container and equates to all external connections therein. However, without any ports exposed, there's no way in!)

For those who tried localhost:4000 as the tutorial said but failed:
Input this:
$ docker-machine env
and you will see something like:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/choi/.docker/machine/machines/dummy"
export DOCKER_MACHINE_NAME="dummy"
So you get the IP: 192.168.99.100. Then just visit 192.168.99.100:4000, as ip:your_port.

You can find a right address to call your page this way
First, find the CONTAINER ID by:
$ docker ps
You will get the information like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56dd4d582b08 abc01 "python app.py" 10 minutes ago Up 11 minutes 80/tcp, 0.0.0.0:80->4000/tcp flamboyant_bell
Now you know the port: 4000 (scroll right this row). So that the address should be like this:
http://192.168.99.100:4000/
However, you can double check:
$ docker port 56dd4d582b08
You will get in this case the following info:
4000/tcp -> 0.0.0.0:80
Now you can try to look you project at
http://192.168.99.100:4000/

After exposed the port, you can access the web app by the internal IP address created by docker. You can get the IP address using the container's name running the command:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' 'container name here'
Let's say that you got the IP 172.17.0.2. You can run open http://172.17.0.2:8000

Related

Cannot contact docker container on Windows host

tl;dr summary of the problem:
Application launches successfully within container, binds to 127.0.0.1:8080 within the container, and successfully services web requests, but only within the container
docker ps -a confirms that port 8080 is being exposed
I cannot communicate with the application from the host using the container's actual IP address when I request http://[Container IP address]:8080
The host is running Windows 10
The Windows Firewall is completely disabled for troubleshooting
To troubleshoot I have created the simplest possible application to run in a dockers container, an F# / Suave application like so:
open Suave
[<EntryPoint>]
let main args =
startWebServer defaultConfig (Successful.OK "Hello World!")
0
Which works fine, returning a simple "Hello World!" when I run it locally.
To containerize the app I have followed the instructions at "Dockerize a .NET Core application" which instructs me to run the container like
$ docker run -d -p 8080:80 --name myapp aspnetapp
I cannot connect to the "website" at http://localhost:80 nor http://localhost:8080, which apparently is a common problems for Docker users running Windows. However the solution that seems to have fixed this problem for every other Windows user on the internet, running
docker inspect myapp
and then hitting the resulting IPAddress, does not work either. I get:
Hitting both http://172.17.0.2:80 and http://172.17.0.2:8080 in Chrome gives me "Site can't be reached."
Also worth noting, when I run
docker logs myapp
The only line is
[17:43:21 INF] Smooth! Suave listener started in 73.476ms with binding 127.0.0.1:8080
As a guess, I have also tried
ipconfig
and then hitting the IP address of the Docker NAT adapter, but this also results in an unreachable site.
UPDATE:
Another observation which might or might not be relevant: Many online tutorials suggest that under Windows you need to directly connect to the IP Address of the container, and to get that IP address by running
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" myapp
which for me, always yields:
When I run a vanilla
docker inspect myapp
the resulting JSON is not structured exactly like the recommended query. I get a bridge node, but no nat node:
Your app says it’s bound to localhost:8080, but you’re publishing port 80. Stop the container, and rerun with:
docker run -d -p 8080:8080 --name myapp aspnetapp
Try adding the following lines in docker file
ENV ASPNETCORE_URLS http://+:80
EXPOSE 80
Reference: https://www.sep.com/sep-blog/2017/02/20/hosting-asp-net-core-docker/
My containerized app needed to listen/bind to 0.0.0.0 rather than 127.0.0.1.

Can't access service running in docker

I'm unable to access a nodejs based service via http://localhost:8000 running in a docker image. I'm using Docker for Mac (https://docs.docker.com/docker-for-mac/)
I'm following the tutorial here https://nodejs.org/en/docs/guides/nodejs-docker-webapp/.
The server runs on port 8000. I start the docker image with the following:
$ docker run -p 8000:8000 -d geuis/node-server:latest
If I run docker ps I see:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9fa2e446918b geuis/node-server:latest "npm start" 6 seconds ago Up 5 seconds 0.0.0.0:8000->8000/tcp unruffled_lewin
If I docker exec -it 9fa2e446918b /bin/bash I can access the docker vm and I can curl http://localhost:8000 and access the server from inside the container.
However, I try the same curl http://localhost:8000 from my system terminal and its not accessible.
Not sure what I need to do next.
Try the following listen statement:
app.listen(PORT, '0.0.0.0');
From reading the tutorial you mention it looks like express is listening on localhost. This is fine if you're running locally but inside of a container, localhost is not the same localhost that's outside of the container.
0.0.0.0 is the unspecified IPv4 address and so Express will bind on any IP it can find, which will be the IP that your requests are coming in from outside the container.

Docker error on Windows 7 “Client.Timeout exceeded while awaiting headers”

My Problem is similar to the other two Questions:starting tutorial and Timeout on windows 2016. But none of it resolved my problem. (on my other search I didn't find any articles that could help my case > search timeout)
For any of the following commands:
docker run hello-world
docker pull hello-world
docker login -u user -p pass
I get the same error:
My proxies are correctly set to my cntlm service:
when I try to get the address with curl I get the following answer:
My docker version:
Docker info:
I've tried all the troubleshoot from this link (create a new default docker machine and so on)
Do you have any idea what could I do to download hello-world (or other) container?
Finally I got it:
First of all the client should be the same Version as the Server (now both are 1.13.1)
Second because I am using a Cntlm I have to create a Tunnel to forward my port from the Cntlm configuration.
ssh -R tunnelPort:proxy-Cntlm docker#ip.docker.machine
where:
tunnelPort will be used on the docker-machine (ex: 3000 for 127.0.0.1:3000)
proxy-Cntlm is the ip + port from cntlm.ini (ex: 127.0.0.1:3128)
ip.docker.machine it can be found simply by running docker-machine ls
One more thing! you have to adjust the ~/.ssh/config (at least for cygwin)
Host docker 192.168.99.100
Hostname 192.168.99.100
IdentityFile "path/to/id_rsa"
#on windows it is C:/Users/user/.docker/machine/machines/default/id_rsa when you have a default Machine

docker running splash container but localhost does not load (windows 10)

I am following this tutorial to use splash to help with scraping webpages.I installed Docker toolbox and did these two steps:
$ docker pull scrapinghub/splash
$ docker run -p 5023:5023 -p 8050:8050 -p 8051:8051 scrapinghub/splash
I think it is running correctly, based on the prompted message in Docker window, which looks like this:
However, when I open the `localhost:8050' in a web browser, it says the localhost is not working.
What might have gone wrong in this case? Thanks!
You have mapped the port to your docker host (the VM), but you have not port-forwarded that same port to your actual "localhost" (your Windows host)
You need to declare that port-forwarding in the Network settings of your VM (for instance "default"), or with VBoxManage controlvm commands.
Then and only then could you access that port (used by your VM) from your Windows host (localhost).
That or you can access that same port using the IP address of your boot2docker VM: see docker-machine ls.
#user3768495, when you use http://192.168.99.100:8050/ , you are actually using the docker-machine ip and this ip would be available on your machine only and not on the network. To map it to localhost, you do need to port-forward the same port to your localhost. I was having the same issue and I detailed the process in below link.
https://stackoverflow.com/a/35737787/4820675

How to access Docker container's web server from host

I'm running under boot2docker 1.3.1.
I have a Docker container running a web server via uwsgi --http :8080.
If I attach to the container I can browse the web site using lynx http://127.0.0.1:8080 so I know the server is working.
I ran my container with:
$ docker run -itP --expose 8080 uwsgi_app:0.2
It has the following details:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5248ad86596d uwsgi_app:0.2 "bash" 11 minutes ago Up 11 minutes 0.0.0.0:49159->8080/tcp cocky_hypatia
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' 5248ad86596d
172.17.0.107
I thought I could access that web site from my host by going to http://172.17.0.107:49159.
This does not work. I just see 'connecting...' in Chrome, getting nowhere.
What am I doing wrong?
Extending Anentropic's answer: boot2docker is the old app for Mac and Windows, docker-machine is the new one.
Firstly, list your machines:
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM
default * virtualbox Running tcp://192.168.99.100:2376
Then select one of the machines (the default one is called default) and:
$ docker-machine ip default
192.168.99.100
Ok, stupid me, I found the answer in the docs for boot2docker
https://docs.docker.com/installation/mac/#container-port-redirection
I needed to use the ip address of the boot2docker vm, rather than the ip of the container, i.e.
$ boot2docker ip
192.168.59.103
and I am able to browse my site from the host at http://192.168.59.103:49159/
I did not need to add any route on the host
To find the IP address of your container, you should need NO additional installs:
docker inspect <container>
This provides a wealth of info. grep it for the IPAddress.
You could use boot2docker port mapping option -L, as described here.
So, in your case it would be
boot2docker ssh -L 0.0.0.0:8080:localhost:8080
and then
docker run -it -p 8080:8080 uwsgi_app:0.2
That way, you do not have to use boot2docker's IP address: you can use localhost or your own IP address (and your docker container can be accessed from outside).
Boot2docker is outdated, but you may still have this problem on Docker for Windows or Mac, even though the same container works on Linux. One symptom is that trying to access a page on the server inside the container gives the error "didn't send any data" as opposed to "could not connect."
If so, it may be because on Win/Mac the container host has its own IP, it's not localhost as it is on linux. Try running Django on IP 0.0.0.0, meaning accept connections from all IPs, like this:
python manage.py runserver 0.0.0.0:8000
Alternatively, if you need to make sure the server only responds to local requests (such as from your local proxy like nginx, apache, or gunicorn) you can use the host IP returned by hostname -i.
And make sure you are using the -p port forwarding option correctly in the docker run command.
Assuming all is well, you should be able to access your server at http://localhost in a browser running on the host machine.
docker build -t {imagename} .
docker build -t api-rest-test .
docker run -dp {localport}:{exposeport} image:name
docker run -dp 8080:8080 api-rest-test:latest
make sure you are using the same port for yourlocalport and exposeport
then you can access your rest service in your local machine http://localhost:8080
[EDIT: original version was ignoring the -P in question]
If you want to get to the containers without having to 'publish' the port (which changes its number)
there is a good run-through here.
The key is this line:
sudo route -n add 172.17.0.0/16 172.16.0.11
which tells the Mac how to route to the private network inside the VirtualBox VM that the Docker containers are on.
Had the same issue and in my case i was using AWS EC2 instance. I was trying with the container IP which did not work. Then I used the actual public IP of the AWS host as the IP, which worked.
How to troubleshoot the issue on hosting application on local host browser
For this launch the container with below command, in my case it was:
[root#centoslab3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b81d8a0e3e1 centos:baseweb "/bin/bash" 8 minutes ago Exited (0) 24 seconds ago webtest
[root#centoslab3 ~]# docker run --name=atul -v /root/dockertest:/var/www/html -i -t -p 5000:8000 centos:baseweb /bin/bash
In the httpd configuration:
[root#adb28b08c9ed /]# cd /etc/httpd/conf
[root#adb28b08c9ed conf]# ll
total 52
-rw-r--r--. 1 root root 34419 Sep 19 15:16 httpd.conf
edit the file with the port 8000 in listner and update the container ip and port under Servername.
Restart the httpd service and you are done.
Hope this helps

Resources