在实时 Docker 容器上公开端口 [英] Exposing a port on a live Docker container

查看:21
本文介绍了在实时 Docker 容器上公开端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个充当完整虚拟机的 Docker 容器.我知道我可以使用 Dockerfile 中的 EXPOSE 指令来公开端口,并且我可以使用 -p 标志和 docker run 来分配端口,但是一旦容器实际运行中,是否有命令可以实时打开/映射其他端口?

I'm trying to create a Docker container that acts like a full-on virtual machine. I know I can use the EXPOSE instruction inside a Dockerfile to expose a port, and I can use the -p flag with docker run to assign ports, but once a container is actually running, is there a command to open/map additional ports live?

例如,假设我有一个运行 sshd 的 Docker 容器.其他人使用容器 ssh 并安装 httpd.有没有办法把容器上的80端口暴露出来,映射到宿主机上的8080端口,这样人们就可以访问容器内运行的web服务器了,不用重启?

For example, let's say I have a Docker container that is running sshd. Someone else using the container ssh's in and installs httpd. Is there a way to expose port 80 on the container and map it to port 8080 on the host, so that people can visit the web server running in the container, without restarting it?

推荐答案

您无法通过 Docker 执行此操作,但您可以从主机访问容器的未公开端口.

You cannot do this via Docker, but you can access the container's un-exposed port from the host machine.

如果你有一个容器在它的 8000 端口上运行着一些东西,你可以运行

If you have a container with something running on its port 8000, you can run

wget http://container_ip:8000

要获取容器的 IP 地址,请运行 2 个命令:

To get the container's IP address, run the 2 commands:

docker ps
docker inspect container_name | grep IPAddress

在内部,Docker 会在您运行映像时调用 iptables,因此可能对此进行一些更改.

Internally, Docker shells out to call iptables when you run an image, so maybe some variation on this will work.

要在本地主机的端口 8001 上公开容器的端口 8000:

To expose the container's port 8000 on your localhost's port 8001:

iptables -t nat -A  DOCKER -p tcp --dport 8001 -j DNAT --to-destination 172.17.0.19:8000

解决这个问题的一种方法是使用您想要的端口映射设置另一个容器,并比较 iptables-save 命令的输出(不过,我不得不删除其他一些强制流量通过 docker 代理的选项).

One way you can work this out is to setup another container with the port mapping you want, and compare the output of the iptables-save command (though, I had to remove some of the other options that force traffic to go via the docker proxy).

注意:这是在颠覆 docker,所以应该意识到它很可能会产生蓝烟.

另一种选择是查看(新的?0.6.6 后?)-P 选项 - 它将使用随机主机端口,然后将它们连接起来.

Another alternative is to look at the (new? post 0.6.6?) -P option - which will use random host ports, and then wire those up.

在 0.6.5 中,您可以使用 LINKs 功能来创建一个与现有容器通信的新容器,并附加一些中继到该容器的 -p 标志?(我还没有使用过 LINK.)

With 0.6.5, you could use the LINKs feature to bring up a new container that talks to the existing one, with some additional relaying to that container's -p flags? (I have not used LINKs yet.)

使用 docker 0.11?您可以使用 docker run --net host .. 将您的容器直接附加到主机的网络接口(即 net 没有命名空间),因此您打开的所有端口容器暴露在外.

With docker 0.11? you can use docker run --net host .. to attach your container directly to the host's network interfaces (i.e., net is not namespaced) and thus all ports you open in the container are exposed.

这篇关于在实时 Docker 容器上公开端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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