创建 docker 机器或容器时如何设置特定的固定 IP 地址? [英] How to set a specific fixed IP address when I create a docker machine or container?

查看:33
本文介绍了创建 docker 机器或容器时如何设置特定的固定 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建容器时,我想在同一个局域网中设置特定容器的 IP 地址.

这可能吗?如果没有,创建后可以修改DHCP IP地址吗?

解决方案

考虑到(现在是 2013 年 10 月)文章如何配置 Docker 以在特定 IP 地址范围内启动容器",这似乎是不可能的(或在至少由 Docker 自动为您完成").

<小时>

2015 年 11 月更新:docker/machine issue 1709 中讨论了类似的问题,其中包括 最近的解决方法(2015 年 11 月)Tobias Munk (schmunk42) for docker machine
(容器见下一节):

<块引用>

针对某些用例的解决方法可能是创建如下机器:

  • 192.168.98.100

    docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.98.1/24" m98

  • 192.168.97.100

    docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.97.1/24" m97

  • 192.168.96.100

    docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.96.1/24" m96

<块引用>

如果没有其他机器具有相同的cidr(无类域间路由),机器应该总是在启动时获得.100 IP.

另一种解决方法:

(请参阅如何使用 docker-machine 和 VirtualBox 创建具有特定 URL 的 docker 机器?"中的脚本?")

<块引用>

我的 virtualbox 的 dhcp 范围是 192.168.99.100 - 255,我想在 100 之前设置一个 IP.

我发现了一个设置静态 IP 的简单技巧:创建机器后,我运行此命令并重新启动机器:

echo "ifconfig eth1 192.168.99.50 网络掩码 255.255.255.0 广播 192.168.99.255 up" |docker-machine ssh prova-discovery sudo tee/var/lib/boot2docker/bootsync.sh >/dev/null

<块引用>

此命令创建一个文件bootsync.sh,该文件由boot2docker启动脚本搜索并执行.

现在在机器启动期间执行命令并设置静态 IP.

docker-machine ls名称活动驱动程序状态 URL 群test-1 - virtualbox 运行 tcp://192.168.99.50:2376 test-1 (mast

Michele Tedeschi (micheletedeschi) 添加

我已经更新了命令:

echo "kill `more/var/run/udhcpc.eth1.pid`
ifconfig eth1 192.168.99.50 网络掩码 255.255.255.0 广播 192.168.99.255 向上" |docker-machine ssh prova-discovery sudo tee/var/lib/boot2docker/bootsync.sh >/dev/null

<块引用>

然后运行命令(仅第一次)

docker-machine regenerate-certs prova-discovery

<块引用>

现在 DHCP 不会更改 IP

(将 prova-discovery 替换为您的 docker-machine 的名称)

<小时>

2015 年 4 月:

文章提到了创建自己的网桥(但这不会将其中一个 IP 地址分配给容器):

<块引用>

创建你自己的网桥,用固定地址配置它,告诉 Docker 使用它.完成.

如果您手动执行,它将如下所示(在 Ubuntu 上):

停止泊坞窗ip 链接添加 br0 类型网桥ip 地址添加 172.30.1.1/20 开发 br0ip 链接设置 br0码头工人 -d -b br0

要在现有网桥 IP 范围内分配静态 IP,您可以尝试如何设置静态 IP 地址在 Docker 容器中?",使用创建网桥和一对对等接口的静态脚本.

2015 年 7 月更新:

上面提到的想法也在如何在Docker容器中设置静态IP地址?"使用:

<块引用>

结果应该是 Docker 服务器启动成功,现在准备将容器绑定到新网桥.
暂停以验证网桥的配置后,尝试创建一个容器——您会看到它的 IP 地址在您的新 IP 地址范围内,Docker 将自动检测到该范围.

您可以在启动和停止容器时使用 brctl show 命令查看 Docker 在桥接器中添加和删除接口,并且可以在容器内运行 ip addr 和 ip route 以查看它是否具有获得了网桥 IP 地址范围内的地址,并被告知使用网桥上 Docker 主机的 IP 地址作为其通往 Internet 其余部分的默认网关.

  • 使用以下命令启动 docker:-b=br0(这也是 echo 'DOCKER_OPTS="-b=bridge0"' >>/etc/default/docker可以默认为你设置)

  • 使用pipework(192.168.1.1以下为默认网关IP地址):

    pipework br0 容器名称 192.168.1.10/24@192.168.1.1

When I create my container, I want to set a specific container's IP address in the same LAN.

Is that possible? If not, after the creation can I edit the DHCP IP address?

解决方案

Considering the conclusion of the (now old October 2013) article "How to configure Docker to start containers on a specific IP address range", this doesn't seem to be possible (or at least "done automatically for you by Docker") yet.


Update Nov 2015: a similar problem is discussed in docker/machine issue 1709, which include the recent workaround (Nov 2015)proposed by Tobias Munk (schmunk42) for docker machine
(for container see the next section):

A workaround for some use-cases could be to create machines like so:

  • 192.168.98.100

    docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.98.1/24" m98
    

  • 192.168.97.100

    docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.97.1/24" m97
    

  • 192.168.96.100

    docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.96.1/24" m96
    

If there's no other machine with the same cidr (Classless Inter-Domain Routing), the machine should always get the .100 IP upon start.

Another workaround:

(see my script in "How do I create a docker machine with a specific URL using docker-machine and VirtualBox?")

My virtualbox has dhcp range 192.168.99.100 - 255 and I want to set an IP before 100.

I've found a simple trick to set a static IP: after create a machine I run this command and restart the machine:

echo "ifconfig eth1 192.168.99.50 netmask 255.255.255.0 broadcast 192.168.99.255 up" 
| docker-machine ssh prova-discovery sudo tee /var/lib/boot2docker/bootsync.sh > /dev/null 

This command create a file bootsync.sh that is searched by boot2docker startup scripts and executed.

Now during machine boot the command is executed and set static IP.

docker-machine ls
NAME              ACTIVE   DRIVER       STATE     URL                                      SWARM
test-1                      -        virtualbox     Running   tcp://192.168.99.50:2376      test-1 (mast

Michele Tedeschi (micheletedeschi) adds

I've updated the commands with:

echo "kill `more /var/run/udhcpc.eth1.pid`
ifconfig eth1 192.168.99.50 netmask 255.255.255.0 broadcast 192.168.99.255 up" | docker-machine ssh prova-discovery sudo tee /var/lib/boot2docker/bootsync.sh > /dev/null

then run command (only the first time)

docker-machine regenerate-certs prova-discovery

now the IP will not be changed by the DHCP

(replace prova-discovery by the name of your docker-machine)


April 2015:

The article mentions the possibility to create your own bridge (but that doesn't assign one of those IP addresses to a container though):

create your own bridge, configure it with a fixed address, tell Docker to use it. Done.

If you do it manually, it will look like this (on Ubuntu):

stop docker
ip link add br0 type bridge
ip addr add 172.30.1.1/20 dev br0
ip link set br0 up
docker -d -b br0

To assign a static IP within the range of an existing bridge IP range, you can try "How can I set a static IP address in a Docker container?", using a static script which creates the bridge and a pair of peer interfaces.

Update July 2015:

The idea mention above is also detailed in "How can I set a static IP address in a Docker container?" using:

The result should be that the Docker server starts successfully and is now prepared to bind containers to the new bridge.
After pausing to verify the bridge’s configuration, try creating a container — you will see that its IP address is in your new IP address range, which Docker will have auto-detected.

you can use the brctl show command to see Docker add and remove interfaces from the bridge as you start and stop containers, and can run ip addr and ip route inside a container to see that it has been given an address in the bridge’s IP address range and has been told to use the Docker host’s IP address on the bridge as its default gateway to the rest of the Internet.

  • Start docker with: -b=br0 (that is also what the echo 'DOCKER_OPTS="-b=bridge0"' >> /etc/default/docker can set for you by default)

  • Use pipework (192.168.1.1 below being the default gateway ip address):

    pipework br0 container-name 192.168.1.10/24@192.168.1.1
    

这篇关于创建 docker 机器或容器时如何设置特定的固定 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆