[英]How to expose docker container's ip and port to outside docker host without port mapping?
When i started two docker containers for a same web image on one docker host. 当我在一个docker主机上为同一个Web映像启动两个docker容器时。
5000
两个docker容器在同一个端口5000
上侦听 5000
of the two containers were mapped to different ports of docker host: 49155
, 49156
两个容器的端口5000
映射到49155
host的不同端口: 49156
49155
or 49156
从49155
host外部访问这两个容器需要访问49155
主机ip和端口49155
或49156
Is there a solution to access a docker container from outside docker host by its ip and port, xxxx:5000
, without port mapping? 是否有解决方案通过其IP和端口xxxx:5000
从端口docker主机访问docker容器,没有端口映射?
All docker containers on different dock hosts can access each other directly. 不同dock主机上的所有docker容器都可以直接相互访问。
You can accomplish this with IP aliasing on the host. 您可以使用主机上的IP别名来完成此操作。
First, add a virtual interface on the host that has a different IP address than the primary interface. 首先,在主机上添加一个虚拟接口,该接口的IP地址与主接口不同。 We'll call the primary interface eth0
with IP 10.0.0.10
, and the virtual interface eth0:1
with IP address 10.0.0.11
. 我们将主接口eth0
调用IP 10.0.0.10
,将虚拟接口eth0:1
调用IP地址10.0.0.11
。
ifconfig eth0:1 10.0.0.11 netmask 255.255.255.0 up
Now run the containers and map port 5000 to the corresponding interface. 现在运行容器并将端口5000映射到相应的接口。 For example: 例如:
docker run -p 10.0.0.10:5000:5000 -name container1 <someimage> <somecommand>
docker run -p 10.0.0.11:5000:5000 -name container2 <someimage> <somecommand>
Now you can access each container on port 5000 using different IP addresses externally. 现在,您可以使用外部的不同IP地址访问端口5000上的每个容器。
When creating a VM make sure that the following are selected under networking 创建VM时,请确保在网络下选择以下内容
Attached to: Bridged NetworkManager
Adapter Type: PCnet-Fast III (Am 79C973)
Promiscious Mode Allow All
Install docker, libvrt
Make sure the following are done using root 确保使用root完成以下操作
# chkconfig NetworkManager off
# chkconfig network on
# service NetworkManager stop
# service network start
Create file ifcfg-xxxxx in /etc/sysconfig/network-scripts 在/ etc / sysconfig / network-scripts中创建文件ifcfg-xxxxx
DEVICE=xxxxx
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes
DELAY=0
and append to ifcfg-p2p1
/ ifcfg-eth0
at the end of the file BRIDGE=xxxx
ifcfg-p2p1
文件末尾的ifcfg-p2p1
/ ifcfg-eth0
BRIDGE=xxxx
run 跑
brctl show
to make sure the bridged connected has an adapter either p2p1
or eth0
eg 确保桥接连接有一个适配器p2p1
或eth0
例如
# brctl show
bridge name bridge id STP enabled interfaces
gsbr01 8000.080027595649 no eth0
virbr0 8000.5254004c1564 yes virbr0-nic
now before starting docker we have to use our bridge and not docker0
to do that, run docker as docker -d -b=gsbr01
现在在启动docker0
之前我们必须使用我们的桥而不是docker0
来做到这一点,运行docker -d -b=gsbr01
作为docker -d -b=gsbr01
$ echo 'DOCKER_OPTS="-b=gsbr01"' >> /etc/sysconfig/docker
$ sudo service docker start
Check the result: 检查结果:
# brctl show
bridge name bridge id STP enabled interfaces
gsbr01 8000.080027595649 no eth0
veth5806f27
vethb3e33da
virbr0 8000.5254004c1564 yes virbr0-nic
docker -d -b=gsbr01
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.