详解如何让为docker容器添加新的端口

要为容器添加端口,首先在docker宿主机执行:

brctl addbr br1

ifconfig br1 up

ifconfig br1 promisc

先建立一个网桥br1

docker inspect -f '{{.State.Pid}}' " instanceName

instanceName是OpenStack为容器分配的实例名,楼主的项目中用openstack管理了容器,主要是为了结合虚拟机和容器。这句执行后得到容器的pid这个pid是容器在命名空间的进程id。如果直接在非openstack环境下,这个instanceName则应该是执行docker run –name后面跟的那个name。

ip link add teth1 type veth peer name tethx1

添加veth peer,一端命名teth1,一端命名tethx1;添加更多可命名为2,3,4…

brctl addif br1 teth1

teth1这端挂到之前建立的br1

ip link set teth1 up

teth1这端启动起来

docker inspect instanceName | grep Id

这句话执行得到容器的id:cid这个id也是docker ps上显示的id,但是这里需要的是完整的才需要这样取

ln -s /proc/pid/ns/net /var/run/netns/cid

链接

ip link set dev tethx1 name eth1 netns pid

tethx1这端命名为eth1

ip netns exec pid ip link set eth1 up

eth1启动

docker exec instanceName ifconfig eth1 up

把容器的eth1手动启动

以上操作实现了:

猜你喜欢

转载自blog.csdn.net/SEUSUNJM/article/details/86636017