2

I'm using Docker on my OS X.

Currently i'm connecting to my container at ip.address.received.from.docker:8080 using:

docker run  -d -p 8080:80 -p 2022:22

how can I set localhost:8080 to forward to ip.address.received.from.docker:8080 so i could use localhost instead of the ip address?

Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154
  • The tutorial explains this. Basically you use nginx for that: https://www.digitalocean.com/community/tutorials/docker-explained-how-to-containerize-and-use-nginx-as-a-proxy . – atlaste Mar 15 '16 at 12:30
  • Using a front end server to agregate different service under one adress is the pro way ;) – Rene M. Mar 15 '16 at 12:34
  • By the way, the answer that you just accepted in http://stackoverflow.com/a/36385476/6309 would work here too. – VonC Apr 03 '16 at 14:01

1 Answers1

1

Problem

How to bind a dockerized service to a local socket?


Solution

According to the documentation, the format of --publish option is: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort. Hence, if you want to bind to localhost you could use -p 127.0.0.1:8080:8080 and you're done.

This was tested with this command: docker run -p 127.1.2.3:9080:9080 --rm -it debian running a netcat listening on the port 9080.


Bonus

Btw, I guess that you run sshd in your container according to the option -p 2022:22. I would like to point this article made by a Docker engineer that worth the reading.

Have fun!

Auzias
  • 3,638
  • 14
  • 36
  • this didn't work on OS X... any idea why? I did -p 127.0.0.1:8080:80 – Asaf Nevo Mar 15 '16 at 13:55
  • What do you mean "didn't work"? Any error message? Any log? Anything that would help you (and I) to have an idea on why it didn't work. Are you using a VM? – Auzias Mar 15 '16 at 14:14
  • I'm using Docker with an Ubuntu + Apache container. When browsing to 127.0.0.1:8080 using Safari, I receive: Safari can't open the page because Safari can't connect to the server 127.0.0.1 – Asaf Nevo Mar 15 '16 at 14:30
  • Show your `docker run` command please, what is the output of `nc 127.0.0.1 8080` when your container is running? – Auzias Mar 15 '16 at 15:28
  • `docker run -d -p 127.0.0.1:8080:80 -p 127.0.0.1:2022:22 -v /Users/asafnevo/Sites:/var/www -it --name="pico" pico_server` – Asaf Nevo Mar 15 '16 at 15:44
  • It does not output "nothing". Actually, **once a successful connection**, it waits for input from `stdin`. Try the same `nc` command when the container is not running and you will get an error message (`nc: unable to connect to address 127.1.2.3, service 9080`). In other words this "nothing" means that you can connect to your Apache service from within the container. Using `docker exec` from the same container try to [`wget` (or `curl`)](https://daniel.haxx.se/docs/curl-vs-wget.html) the default web page served by Apache. – Auzias Mar 15 '16 at 15:51
  • It actually outputs the same nothing.. What got me thinking.. maybe the localhost:8080 port is taken? I will try a random port to see – Asaf Nevo Mar 15 '16 at 15:53
  • well even when trying the random ports 9080 and 9999 it does the same behaviour – Asaf Nevo Mar 15 '16 at 15:56
  • What about `wget` or `curl` ? – Auzias Mar 15 '16 at 15:59
  • apache is live an running on the container.. if i'm running the same run command with `-p 8080:80`, then browsing to the docker-machine ip on port 8080 brings me to the apache server in the container – Asaf Nevo Mar 15 '16 at 16:00
  • Btw, maybe it make sense just to forward the localhost to the ip with the port from OS X instead from Docker ? just to set that localhost:8080 will forward to my.ip.address:8080 (which set to forward to the container 80 port) – Asaf Nevo Mar 15 '16 at 16:04