3

The Sinatra web app I created works inside the container and I am able to access it at 9393 within the container. Following is my Dockerfile (which uses the image specified by the Dockerfile: jikkujose/red):

FROM jikkujose/red
MAINTAINER Jikku Jose <jikkujose@gmail.com>

COPY . /banana_app
WORKDIR /banana_app

RUN bundle install
EXPOSE 9393
ENTRYPOINT ["bundle", "exec", "shotgun"]

I launched the built image by, docker run -itdP hey

When I do, docker ps -a:

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                     NAMES
a815e2852c68        hey                 "bundle exec shotgun   13 minutes ago      Up 13 minutes       0.0.0.0:32783->9393/tcp   cranky_rosalind

When I do, curl -v 'http://localhost:32783':

* Rebuilt URL to: http://localhost:32783/
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 32783 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:32783
> Accept: */*
>
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server

PS: I have specified to bind the app to 0.0.0.0.

What am I missing? Why can't I access the app at the host too?

Jikku Jose
  • 18,306
  • 11
  • 41
  • 61
  • Can you try with exact port spec, like -p 9393:9393? Have you checked your app's logs? – allprog Nov 12 '15 at 09:38
  • Have just tried like this: `-p 5000:9393` without avail. I also added a new rule in the Virtual Box to map port 5000 to 5000 in the mac (when I am testing my development machine). Somehow I have been stuck at the point for so long. – Jikku Jose Nov 12 '15 at 10:31
  • Your app seems to still bind to 127.0.0.1 `root@7a06d44328a6:/banana_app# netstat -ln Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 127.0.0.1:9393 0.0.0.0:* LISTEN Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path` – allprog Nov 12 '15 at 12:30
  • I don't follow you, I created two apps one that binds to `0.0.0.0` at `9393` and another that doesn't bind at `2323`. `netstat -ln` gave this: https://gist.github.com/JikkuJose/3353d9e43052e67f4cc1 Any clue whats happening? – Jikku Jose Nov 13 '15 at 07:17
  • I started a container from your repo and got the netstat output I sent you. 9393 is bound to localhost in my case. I haven't seen the other listener. – allprog Nov 13 '15 at 08:30
  • Do you mean to say that you are able to access the bound port through your host? – Jikku Jose Nov 13 '15 at 10:19
  • I didn't mean I am running two apps; I meant I created two small apps after starting the container just to test binding and unbinding outputs in `netstat`. – Jikku Jose Nov 13 '15 at 10:20
  • As mentioned in your previous question (comment http://stackoverflow.com/questions/33170150/cant-access-the-dockerized-app-launched-from-the-command-line-from-outside/33170641?noredirect=1#comment54178152_33170641), did you port forwarded 9393? (preferably by mapping to to 9393: `-p 9393:9393`, and adding that port forward to the VM networking setting) – VonC Nov 13 '15 at 12:11
  • I started your app like so: `docker run -ti -p 9393:9393 banana` Then I ran `docker exec netstat -lnt` that printed `tcp 0 0 127.0.0.1:9393 0.0.0.0:* LISTEN`. This means your app is bound to localhost, not 0.0.0.0. This is why you cannot access it even with port forwarding. – allprog Nov 13 '15 at 12:27
  • This is also visible if you run `curl -v http://172.17.0.2:9393/` (substituting your container's eth0 ip in the address) inside the container. It won't be able to connect. – allprog Nov 13 '15 at 12:28
  • I'm making these statements based on the container created from your Dockerfile. If you committed changes since yesterday, then let me know. – allprog Nov 13 '15 at 12:31
  • @VonC what I don't understand is port forwarding isn't required for other images, for example nginx. And I confirmed the port nginx image was using; it wasn't any standard port or already mapped one. Anyway, I also manually mapped the port, still it didn't work. – Jikku Jose Nov 14 '15 at 02:33
  • @allprog what I am confused is that `IP address`: `172.17.0.2`; didn't you get that via `docker inspect `? I was assuming we should be checking the `IP` from `docker-machine ip default`? – Jikku Jose Nov 14 '15 at 02:39
  • Also, I am certainly using the setting to bind to `0.0.0.0`. As I have mentioned in the question itself. – Jikku Jose Nov 14 '15 at 02:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95110/discussion-between-jikku-jose-and-allprog). – Jikku Jose Nov 14 '15 at 06:43

1 Answers1

1

Did you solve this? I'm having the same problem - in my case for port 80. For the time being, I've worked around it by using the ip address of the docker host instead of referencing localhost. I'm getting this using /sbin/ifconfig on interface docker0.

Ie:

DOCKER_HOST_IP=`/sbin/ifconfig docker0 | /bin/grep "inet addr" | /bin/cut -d: -f2 | /bin/awk '{print $1}'`

Then

curl http://${DOCKER_HOST_IP} gives me the expected result whereas curl http://localhost fails.

JJ.
  • 5,425
  • 3
  • 26
  • 31