44

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
StandardNerd
  • 4,093
  • 9
  • 46
  • 77
  • 5
    0.0.0.0 isn't the IP address of your container - "listening on 0.0.0.0" means the server will accept connections on any network interface. See http://stackoverflow.com/questions/20778771/what-is-the-difference-between-0-0-0-0-127-0-0-1-and-localhost – Ben Mar 30 '16 at 15:14

4 Answers4

40

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!)

Xophmeister
  • 8,884
  • 4
  • 44
  • 87
  • 5
    Special thanks for pointing out that container needs to be accessed via machine's IP. I thought it should be `localhost`, but it seems in context of machine everything goes through it... – Grokking Jun 04 '18 at 11:28
  • 2
    I'm running into a problem on Win10 Home where : does not let me hit my app server which is listening on localhost:8080 inside the container, where 8080 in the container is mapped to 3000 on my host. (0.0.0.0:8080->3000/tcp) Still searching for root cause. – CaffeinateOften Oct 07 '18 at 18:17
  • Took me sometime but found this answer FINALLYYY! Thanks – Aimal Khan Nov 10 '18 at 02:23
  • 1
    I did this but still can't access it from the browser. It does return correctly via curl. Any idea on this? – libzz Dec 19 '18 at 05:20
21

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.

ch271828n
  • 15,854
  • 5
  • 53
  • 88
16

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/
Roman
  • 19,236
  • 15
  • 93
  • 97
11

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

  • 6
    This wont work because he is running docker using docker machine. The containers you create in docker machine are accessible via the host's address and not directly. – Willa Jun 30 '16 at 21:11
  • You are right, @willa. But in the older versions it was possible to access the container's private IP after adding an route as `sudo route -n add 172.17.0.0/16 $(docker-machine ip dev)`. – Gustavo Henrique Jul 01 '16 at 22:11
  • @GustavoHenrique I'm using Docker Desktop for windows and I want to run httpd image but can't via the browser ! the ip address is `172.17.0.2:` still can't access it ! – SlimenTN Mar 16 '20 at 13:45
  • What if an output of your command is empty? – Nairum Mar 19 '23 at 14:21
  • This is a surprisingly helpful answer - it helped me realized I had passed the two ports to `docker run -p` backward. Thank you. – Harold May 27 '23 at 22:11