portforwarding from within docker container

隐身守侯 提交于 2019-12-11 03:42:03

问题


I am trying to get a container built that has ports forwarded, such that code running in the container can access a remote db for instance. So I put the line

CMD ssh -L 27017:localhost:27017

in my dockerfile and then run it, but in no case do I see ports forwarded (I tried RUN instead of cmd, and tried both interactively running the container or using -d , but in the former case I don't have forwarding and in the latter case the container exits soon after starting, even after tacking && /bin/bash to the end of the ssh command). The only way I have succeeded doing this is doing an interactive run

$docker run -it --name cont_name im_name /bin/bash

and then from the interactive shell doing the ssh necessary for port forwarding (which now ties up the shell). Then from another window on my local host, I 'get inside' the first container using

$ docker exec -it cont_name bash 

where I now indeed see ports forwarded . Is there a better/automatic way to do this? 'screen' seems to be a hassle to get running in a container.


回答1:


I think what you want to do is "bind" the port from the container to the host. First thing you need to know is if the port has been exposed via EXPOSE in the docker container you plan on using. The next thing would be to add this to the docker run...:

-p 127.0.0.1:3360:3306

Let's assume it's a mysql instance that you are using, the port that is exposed is 3306, so you would bind that to the host on the same port or whatever port you prefer on the host.



来源:https://stackoverflow.com/questions/32070789/portforwarding-from-within-docker-container

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