如何为docker容器分配静态公共IP [英] How to assign static public IP to docker container

查看:145
本文介绍了如何为docker容器分配静态公共IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 static public IP 分配给容器。所以容器有公共IP。客户端可以使用IP访问容器。

Is there any way to assign the static public IP to the container. So the container has the public IP. Client can access to container with the IP.

推荐答案

使用目前发布的Docker版本,这是不可能的(没有很多在Docker背后的手工工作),虽然很少有必要。

With currently released versions of Docker this isn't possible (without a lot of manual work behind Docker's back), although it is seldom necessary.

Docker通过使用端口映射公开了容器中的网络服务,端口映射可以绑定到主机上的特定IP地址。因此,如果您想在 192.168.10.10 和另一个网络服务器 192.168.10.20 之间安装一个Web服务器,请先确认这个地址在你的主机上可用:

Docker exposes network services in containers through the use of port mappings, and port mappings can bind to specific ip addresses on your host. So if you want to have one web server at 192.168.10.10 and another webserver at 192.168.10.20, first make sure this addresses are available on your host:

ip addr add 192.168.10.10/24 dev eth0
ip addr add 192.168.10.20/24 dev eth0

然后启动第一个容器:

docker run -p 192.168.10.10:80:80 mywebserver

最后启动第二个容器:

docker run -p 192.168.10.20:80:80 mywebserver

在上述命令中, -p 用于将端口映射绑定到特定的IP地址。现在您有两个容器在同一个端口(端口80)上提供服务,但是在不同的IP地址上。

In the above commands, the -p option is used to bind the port mapping to a particular ip address. Now you have two containers offering a service on the same port (port 80) but on different ip addresses.

这篇关于如何为docker容器分配静态公共IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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