为docker配置固定ip

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连接到容器,非常方便。

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

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

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

时间: 2024-07-29 03:14:38

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

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

1.用vmware克隆一个centos 2.进入centos,打开命令行输入ifconfig,执行后发现没有eth0 3.执行网卡启动命令ifconfig eth0 up,再执行ifconfig 4.配置固定ip,执行vi /etc/sysconfig/network-scripts/ifcfg-eth0,编辑该文件(HWADDR这一项没有编辑,是上一个被克隆的centos系统的mac地址) 5.重启配置文件使之生效/etc/init.d/network restart 6.重新编辑配置文件vi

dhcp配置固定ip

DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用是集中的管理.分配IP地址,使网络环境中的主机动态的获得IP地址. Gateway地址.DNS服务器地址等信息,并能够提升地址的使用率. C/S 模式 Server:DHCP Server(运行dhcp服务)UDP服务:67 Client:DHCP Client(运行dhcp程序)     UDP服务:68 udp:适合发送较小的数据报文,且对时效性要求较

为centos虚拟机配置固定ip

在virtual上安装centos虚拟机以后,发现虚拟机没有ip,无法联网 将虚拟机的网络适配器改为桥接模式,桥接到物理机的无线网卡 为虚拟机配置固定IP(vi /etc/sysconfig/network-scripts/ifcfg-enp0s3), 子网掩码和网关设置成和物理机一样的值 重启网络 ( systemctl restart network ),查看IP 成功联网 原文地址:https://www.cnblogs.com/darrenlou/p/10667262.html

使用pipework配置docker容器固定ip

首先讲一下docker的网络模式: 我们使用docker run创建容器时,可以使用--net选项指定容器的网络模式,docker一共有4中网络模式: 1:bridge模式,--net=bridge(默认). 这是dokcer网络的默认设置.安装完docker,系统会自动添加一个供docker使用的网桥docker0,我们创建一个新的容器时,容器通过DHCP获取一个与docker0同网段的IP地址.并默认连接到docker0网桥,以此实现容器与宿主机的网络互通.如下: 2:host模式,--ne

docker 设置固定ip、配置网络

Docker安装后,默认会创建下面三种网络类型 $ docker network ls NETWORK ID NAME DRIVER SCOPE 9781b1f585ae bridge bridge local 1252da701e55 host host local 237ea3d5cfbf none null local 启动 Docker的时候,用 --network 参数,可以指定网络类型 docker run -itd --name test1 --network bridge --i

Docker容器固定IP分配

我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式: host模式,使用--net=host指定. container模式,使用--net=container:NAME_or_ID指定. none模式,使用--net=none指定. bridge模式,使用--net=bridge指定,默认设置. 默认选择bridge的情况下,容器启动后会通过DHCP获取一个地址,这可能不是我们想要的,本文介绍在centos7 docker

Docker设置固定ip地址

Docker安装后,默认会创建下面三种网络类型: $ docker network ls NETWORK ID NAME DRIVER SCOPE 9781b1f585ae bridge bridge local 1252da701e55 host host local 237ea3d5cfbf none null local 启动 Docker的时候,用 --network 参数,可以指定网络类型 docker run -itd --name test1 --network bridge --

手动配置固定IP参数vim vim

取得的固定 IP 参数不一定是公有 IP ,只是一组可接受的固定 IP. 需要修改的参数有 IP相关参数, DNS, 主机名. 1 IP / Netmask / Gateway 的配置 启动 查看 2 DNS 服务器 IP 的定义与查看 vim/etc/resolv.conf dig www.baidu.com 3 hostname 修改, 启动 查看 vim /etc/sysconfig/network vim /etc/hosts hostname 原文地址:https://www.cnbl

centos 配置固定ip

centos下网络配置方法(网关.dns.ip地址配置) 来源:互联网 作者:佚名 时间:07-13 00:32:07 [大 中 小] 本文介绍了centos网络配置的方法,centos网络配置主要包括dns.网关.IP地址,主要是配置resolv.conf\network\ifcfg-eth0这些网络配置文件,需要的朋友可以参考下 centos网络配置实例 1,配置DNSvi /etc/resolv.conf加入: 复制代码 代码如下: nameserver 192.168.0.1 names