Access Docker forwarded port on Mac

2019-02-19 14:48发布

There is a related post here: Port mapping in Docker on Mac OSX installed with Docker Toolbox

but it didn't work for me

Get ports for container

docker port 485186e65a5e

8080/tcp -> 0.0.0.0:33360
8088/tcp -> 0.0.0.0:33359
19888/tcp -> 0.0.0.0:33358
50070/tcp -> 0.0.0.0:33357
50075/tcp -> 0.0.0.0:33356
8042/tcp -> 0.0.0.0:33361

Check that someone listens to ports in container

bash-4.1# netstat -alnpt | grep 19888
tcp        0      0 127.0.0.1:19888             0.0.0.0:*                   LISTEN      1094/java   

Do wget in container

bash-4.1# wget 127.0.0.1:19888
--2016-04-11 14:16:54--  http://127.0.0.1:19888/
Connecting to 127.0.0.1:19888... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://127.0.0.1:19888/jobhistory [following]
--2016-04-11 14:16:54--  http://127.0.0.1:19888/jobhistory
Reusing existing connection to 127.0.0.1:19888.
HTTP request sent, awaiting response... 200 OK
Length: 6763 (6.6K) [text/html]
Saving to: `index.html'

100%[================================================================================================================================================================================>] 6,763       --.-K/s   in 0s      

2016-04-11 14:16:54 (182 MB/s) - `index.html' saved [6763/6763]

Try to access forwarded port from host, no luck... :(((

$docker-machine ip default
192.168.99.100
$ wget 192.168.99.100:33358
--2016-04-11 16:18:04--  http://192.168.99.100:33358/
Connecting to 192.168.99.100:33358... failed: Connection refused.

What do I do wrong?

2条回答
唯我独甜
2楼-- · 2019-02-19 14:54

Omg, desired service started on 127.0.0.1 in container, that is why it wasn't visible from outside world. I've modified service config to start on 0.0.0.0 and now I can access forwarded container port

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-19 15:06

I had the same problem and was able to fix it by specifying the host that the server within the container uses.

NOTE: when using host below, it means a web server host. When I use host-machine, I mean the main operating system I'm using, (i.e. not a container or a web server, just my laptop as a machine)

The Problem

Running web servers on the container like webpack-dev-server and http-server automatically run the app using a host of http://localhost. Typically you will see that in the output when you start the server. Something like :

Project is running at http://localhost:8080

or

Server available at http://127.0.0.1:8080

On most machines, localhost and 127.0.0.1 are the same thing. This host is not publicly viewable. As a result, your host machine can't see anything, even though it's looking in the right place.

Solution

You should specify a public host when you run the server inside your container.

webpack-dev-server --port 8080 --host 0.0.0.0

or

http-server -p 8080 -a 0.0.0.0

Because the 0.0.0.0 address is viewable to any outside machine, you should be able to see your app working as expected from your host machine.

NOTE: This works for any server, like Python's SimpleHTTPServer, etc. Just look up how to change the host for your chosen server in the documentation

Resources/Nods (how to run webpack-dev-erver with a publicly accessible host)[How to make the webpack dev server run on port 80 and on 0.0.0.0 to make it publicly accessible?

查看更多
登录 后发表回答