docker默认使用bridge模式,通过网桥连接到宿主机,而容器内部的ip则从网桥所在的ip段取未用的ip。这样做一个不方便的地方在于容器内部的ip不是固定的,想要连接容器时只能通过映射到宿主机的端口,因而有很多项目使用overlay来为docker提供网络的配置,比如Pipework、Flannel、Kubernetes、Weave、opencontrail等。

想要使用overlay来为docker配置网络,需要首先了解下docker的网络模式:

  1. --net=bridge — The default action, that connects the container to the Docker bridge as described above.

  2. --net=host — Tells Docker to skip placing the container inside of a separate network stack. In essence, this choice tells Docker to not containerize the container's networking! While container processes will still be confined to their own filesystem and process list and resource limits, a quick ip addr command will show you that, network-wise, they live “outside” in the main Docker host and have full access to its network interfaces. Note that this doesnot let the container reconfigure the host network stack — that would require --privileged=true — but it does let container processes open low-numbered ports like any other root process. It also allows the container to access local network services like D-bus. This can lead to processes in the container being able to do unexpected things like restart your computer. You should use this option with caution.

  3. --net=container:NAME_or_ID — Tells Docker to put this container's processes inside of the network stack that has already been created inside of another container. The new container's processes will be confined to their own filesystem and process list and resource limits, but will share the same IP address and port numbers as the first container, and processes on the two containers will be able to connect to each other over the loopback interface.

  4. --net=none — Tells Docker to put the container inside of its own network stack but not to take any steps to configure its network, leaving you free to build any of the custom configurations explored in the last few sections of this document.

上面这几种方式只有--net=none才可以为docker分配固定ip,来看看如何操作。

首先,配置一个用于创建container interface的网桥,可以使用ovs,也可以使用Linux bridge,以Linux bridge为例:

br_name=docker
brctl addbr $br_name
ip addr add 192.168.33.2/24 dev $br_name
ip addr del 192.168.33.2/24 dev em1
ip link set $br_name up
brctl addif $br_name eth0

接着,可以启动容器了,注意用--net=none方式启动:

# start new container
hostname='docker.test.com'
cid=$(docker run -d -i -h $hostname --net=none -t centos)
pid=$(docker inspect -f '{{.State.Pid}}' $cid)

下面,为该容器配置网络namespace,并设置固定ip:

# set up netns
mkdir -p /var/run/netns
ln -s /proc/$pid/ns/net /var/run/netns/$pid
# set up bridge
ip link add q$pid type veth peer name r$pid
brctl addif $br_name q$pid
ip link set q$pid up
# set up docker interface
fixed_ip='192.168.33.3/24'
gateway='192.168.33.1'
ip link set r$pid netns $pid
ip netns exec $pid ip link set dev r$pid name eth0
ip netns exec $pid ip link set eth0 up
ip netns exec $pid ip addr add $fixed_ip dev eth0
ip netns exec $pid ip route add default via 192.168.33.1

这样,容器的网络就配置好了,如果容器内部开启了sshd服务,通过192.168.33.3就可以直接ssh连接到容器,非常方便。上面的步骤比较长,可以借助pipework来为容器设置固定ip(除了设置IP,还封装了配置网关、macvlan、vlan、dhcp等功能):

pipework docker0 be8365e3b2834 10.88.88.8/24

那么,当容器需要删除的时候,怎么清理网络呢,其实也很简单:

# stop and delete container
docker stop $cid
docker rm $cid
# delete docker's net namespace (also delete veth pair)
ip netns delete $pid

更多docker网络的配置,可以参考官方手册

为docker配置固定ip的更多相关文章

  1. Docker 配置固定IP及桥接的实现方法(转载)

    这篇文章主要介绍了Docker 配置固定IP和桥接的实现方法的相关资料,这里详细介绍了Docker 的四种网络模式及如何实现桥接的案例,需要的朋友可以参考下 docker默认使用bridge模式,通过 ...

  2. Docker桥接宿主机网络与配置固定IP地址

    有些需求是把这个容器与宿主机在同一个网段,但是本人不建议这样子去操作,因为一个容器本身就是一个封装好的服务.建议去按默认的网络去实现. 临时设置 [root@linux-docker01 ~]# vi ...

  3. linux 《vmware下克隆的centos无法配置固定ip》

    1.用vmware克隆一个centos 2.进入centos,打开命令行输入ifconfig,运行后发现没有eth0 3.运行网卡启动命令ifconfig eth0 up,再运行ifconfig wa ...

  4. 为centos虚拟机配置固定ip

    在virtual上安装centos虚拟机以后,发现虚拟机没有ip,无法联网 将虚拟机的网络适配器改为桥接模式,桥接到物理机的无线网卡 为虚拟机配置固定IP(vi /etc/sysconfig/netw ...

  5. Ubuntu 14.04 虚拟机配置固定ip地址

    Ubuntu 14.04 虚拟机配置固定ip地址: 虚拟机用的NAT方式配置: 1.虚拟机: 虚拟机→设置→网络适配器→NAT模式: 2.虚拟机:编辑→虚拟网络编辑器→更改设置→选择NAT→填入子网I ...

  6. Docker容器固定ip

    Docker容器固定IP 必须停止docker服务才能创建网桥 查看docker服务状态 停止docker服务 启动docker服务 [root@docker Tools]# systemctl st ...

  7. 为Docker容器配置固定IP

    当docker以桥接的方式启动容器时,容器内部的IP是经过DHCP获取的,例如:172.17.0.8/32,且每重启依次IP都会发生变动.某些特殊的情况下,需要容器内有自己固定的一个内部IP.我的实现 ...

  8. docker 设置固定ip、配置网络

    Docker安装后,默认会创建下面三种网络类型 $ docker network ls NETWORK ID NAME DRIVER SCOPE 9781b1f585ae bridge bridge ...

  9. 给虚拟机中的CentOS7配置固定ip

    在虚拟机中安装完了CentOS7之后,使用了DHCP来获取ip,vmware的网络连接使用了NAT模式.但是在把Linux设置为固定ip地址后,虚拟机里的linux可以ping通全网段的ip地址,但是 ...

随机推荐

  1. 安卓--界面--改变image view

    switch (v.getId()) { case R.id.button: imageView.setImageResource(R.drawable.jelly_bean); break; def ...

  2. REVERSE-Daily(4)-Elfcrackme2

    非常坑爹的一道题目,看似非常简单,实则有套路 链接: http://pan.baidu.com/s/1i4XLCd3 密码:9zho 为了练手 我会写出三种解法,包括 结合ascii码值范围的爆破,动 ...

  3. Android开源控件PhotoView的使用

    整体来说,它是一个更高级的ImageView,支持缩放,多点触控缩放,滚动和滑动,单机,长按等事件: PhotoView的git托管地址:https://github.com/chrisbanes/P ...

  4. cocos2dx 之 android java 与 c++ 互相调用 代码(以百度定位为例子)

    在作cocosdx项目移植到android上时,预见各种头痛问题,今天首先就说说如何在 java 中调用c++ 代码. 这里就用百度定位为例吧,也是我项目中的一小块内容.首先,先百度一下 “百度定位s ...

  5. linux awk的使用

    awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...

  6. RealProxy深入

    Program.cs class Program { static void Main(string[] args) { NoMethodLogging(); Console.WriteLine(&q ...

  7. Mac安装GitLab CE记录

    0 REF REF1 原始的GitLab Documentation REF2 Installation-guide-for-GitLab-on-OS-X REF3 如何在Mac 终端升级ruby版本 ...

  8. Zookeeper概论(对zookeeper的概论、原理、架构等的理解)

    Zookeeper概论(对zookeeper的概论.原理.架构等的理解) 一.概论 Zookeeper是一个分布式的.开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是h ...

  9. 【Cocos2d-x 3.x】屏幕自适应匹配

    在进行游戏开发时, 由于市场上的Android移动设备的分辨率有很多种,而且IOS移动设备的分辨率也不相同,为了能让手游能在90%以上的移动设备较为完美的运行,因此需要考虑屏幕的自适应问题,让一套资源 ...

  10. Android深度探索HAL与驱动开发 第三章 Git入门

    Git功能十分复杂,简单来说它使你的开发更为快捷和可控,尤其是在开源项目上展现的友好的交互和回馈. 熟悉一些git指令操作对开发者的帮助可以避免开发者受到一些外在因素打断开发进度,甚至延误项目的che ...