简体   繁体   English

码头工人网络直接访问容器

[英]docker networking direct access to container

I try to migrate from multiple VM with static ip to container based solution. 我尝试从具有静态IP的多个VM迁移到基于容器的解决方案。

Now I'm using VM with static ip: 现在,我将VM与静态IP配合使用:

在此处输入图片说明

I can ping and telnet my VMs telnet 10.48.0.10 5432 and telnet 10.48.0.11 5432 我可以ping和telnet我的虚拟机telnet 10.48.0.10 5432telnet 10.48.0.11 5432

I want to create a single docker host that allows me to do the same : 我想创建一个单一的docker主机,使我可以执行以下操作:

在此处输入图片说明

It would be great if I can telnet 172.17.0.2 5432 and telnet 172.17.0.3 5432 如果能telnet 172.17.0.2 5432telnet 172.17.0.3 5432那就太好了

I try to do it via docker because I want to manage the configuration. 我尝试通过docker进行操作,因为我想管理配置。

What would be the proper way to do this ? 这样做的正确方法是什么? Should I use a TCP Proxy inside a container to manage this ? 我是否应该在容器内使用TCP代理来管理它?

The solution is pretty simple. 解决方案非常简单。

create a network and bind it to the host 创建网络并将其绑定到主机

docker network create --subnet=10.0.0.0/24 -o "com.docker.network.bridge.host_binding_ipv4"="0.0.0.0" mynet

then run a container on mynet network 然后在mynet网络上运行一个容器

docker run -ti --net=mynet --ip=10.0.0.30 busybox

Now from another computer if you add route to your docker host (192.168.2.156) for this subnet : 现在从另一台计算机上,如果您将路由添加到该子网的docker主机(192.168.2.156):

sudo route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.2.156

You can ping your container ( ping 10.0.0.30 ) 您可以ping您的容器( ping 10.0.0.30

If you want to access the containers from your host or from any other server that can get your host, you will need to map each container to a different port in the host server. 如果要从主机或可以获取主机的任何其他服务器访问容器,则需要将每个容器映射到主机服务器中的其他端口。

docker run -d -p 54321:5432 my_app
docker run -d -p 54322:5432 my_app

So you will can telnet 10.200.0.1 54321 and telnet 10.200.0.1 54322 因此您可以telnet 10.200.0.1 54321telnet 10.200.0.1 54322

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM