从docker容器中进行portforwarding [英] portforwarding from within docker container

查看:457
本文介绍了从docker容器中进行portforwarding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建具有转发端口的容器,例如,在容器中运行的代码可以访问远程数据库. 所以我把线放在

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

在我的dockerfile中然后运行它,但是在任何情况下我都看不到端口转发(我尝试使用RUN而不是cmd,并尝试了交互式运行容器或使用-d,但是在前一种情况下,我没有转发和后一种情况下,即使在将&&/bin/bash附加到ssh命令的结尾之后,容器也会在启动后立即退出). 我成功完成此操作的唯一方法是进行交互式运行

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

,然后从交互式外壳程序执行端口转发所需的ssh(现在将外壳程序绑定).然后从本地主机上的另一个窗口中,我使用

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.

推荐答案

我认为您想要做的是将端口从容器绑定"到主机.您需要知道的第一件事是端口是否已通过您计划使用的Docker容器中的EXPOSE公开.接下来是将其添加到docker run...:

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

让我们假设它是您正在使用的mysql实例,暴露的端口是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.

这篇关于从docker容器中进行portforwarding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆