Access Docker forwarded port on Mac

前端 未结 2 468
故里飘歌
故里飘歌 2021-01-06 05:53

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

2条回答
  •  礼貌的吻别
    2021-01-06 06:21

    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?

提交回复
热议问题