How to expose a service running inside a docker container, bound to localhost

吃可爱长大的小学妹 提交于 2019-12-11 04:29:05

问题


I have a service running inside a docker-container, which by default only binds to localhost.

I could reconfigure / reprogram this service to bind to all interfaces, but this might have security implications in other contexts. Is there any way to expose a service bound to localhost inside a docker container?


回答1:


I ended up using socat inside the docker container, to proxy any tcp connections on the relevant port-number coming in on the public interface, to the private interface.

For example, this can be added to the docker-container run-script to proxy a service bound on localhost:3000 to the :3000, where it can be EXPOSED and --linked like any other service. Make sure to install socat inside the container.

socat TCP4-LISTEN:3000,bind=`hostname -I | tr -d '[:space:]'`,fork TCP4:localhost:3000 &

Please note; I am using hostname -I | tr -d '[:space:]' to discover the ip of the docker container. Since normally docker containers have only one public ip, this works well.



来源:https://stackoverflow.com/questions/40383804/how-to-expose-a-service-running-inside-a-docker-container-bound-to-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!