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?

3
  • The tutorial explains this. Basically you use nginx for that: digitalocean.com/community/tutorials/… . Commented Mar 15, 2016 at 12:30
  • Using a front end server to agregate different service under one adress is the pro way ;) Commented Mar 15, 2016 at 12:34
  • By the way, the answer that you just accepted in stackoverflow.com/a/36385476/6309 would work here too. Commented Apr 3, 2016 at 14:01

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

11 Comments

this didn't work on OS X... any idea why? I did -p 127.0.0.1:8080:80
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?
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
Show your docker run command please, what is the output of nc 127.0.0.1 8080 when your container is running?
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.