How to access host port from docker container

前端 未结 14 1537
南旧
南旧 2020-11-22 03:41

I have a docker container running jenkins. As part of the build process, I need to access a web server that is run locally on the host machine. Is there a way the host web s

相关标签:
14条回答
  • 2020-11-22 04:06

    Use --net="host" in your docker run command, then localhost in your docker container will point to your docker host.

    0 讨论(0)
  • 2020-11-22 04:07

    For macOS and Windows

    Docker v 18.03 and above (since March 21st 2018)

    Use your internal IP address or connect to the special DNS name host.docker.internal which will resolve to the internal IP address used by the host.

    Linux support pending https://github.com/docker/for-linux/issues/264

    MacOS with earlier versions of Docker

    Docker for Mac v 17.12 to v 18.02

    Same as above but use docker.for.mac.host.internal instead.

    Docker for Mac v 17.06 to v 17.11

    Same as above but use docker.for.mac.localhost instead.

    Docker for Mac 17.05 and below

    To access host machine from the docker container you must attach an IP alias to your network interface. You can bind whichever IP you want, just make sure you're not using it to anything else.

    sudo ifconfig lo0 alias 123.123.123.123/24

    Then make sure that you server is listening to the IP mentioned above or 0.0.0.0. If it's listening on localhost 127.0.0.1 it will not accept the connection.

    Then just point your docker container to this IP and you can access the host machine!

    To test you can run something like curl -X GET 123.123.123.123:3000 inside the container.

    The alias will reset on every reboot so create a start-up script if necessary.

    Solution and more documentation here: https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

    0 讨论(0)
提交回复
热议问题