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

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

问题描述

创建容器时,我想在同一LAN中设置特定容器的IP地址。

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

可以吗?如果没有,创建后可以编辑DHCP IP地址吗?

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

推荐答案

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

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.

2015年11月更新: docker / machine问题1709 ,其中包括最近的解决方法( 2015年11月) Tobias Munk( schmunk42 用于 docker机器

(对于con tainer参见下一节):

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

    • 192.168.98.100

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


    • 192.168.97.100

    • 192.168.97.100

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


    • 192.168.96.100

    • 192.168.96.100

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



    • 如果没有其他机器相同的 cidr(无类域间路由) 计算机在启动时应始终获得 .100 IP。

      另一种解决方法


      我的虚拟机的dhcp范围为192.168.99.100-255,我想在100之前设置IP。

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

      我找到了设置静态IP的简单技巧:创建机器后,我运行以下命令并重新启动机器:

      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 
      




      此命令创建文件 boot2docker 启动脚本搜索并执行的> bootsync.sh 。

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

      现在在机器启动期间,该命令将执行并设置静态IP。

      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
      

      米歇尔·特德斯基( micheletedeschi 添加

      我用以下命令更新了命令:

      I've updated the commands with:

      echo "kill `more /var/run/udhcpc.eth1.pid`\nifconfig 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
      




      现在IP将不会被DHCP更改

      now the IP will not be changed by the DHCP

      (用docker-machine名称替换 prova-discovery

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

      2015年4月:

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

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


      创建您自己的网桥,为它配置一个固定地址,并告诉Docker使用它。完成。

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

      如果您手动进行操作,它将看起来像这样(在Ubuntu上):

      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
      

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

      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.

      2015年7月更新:

      Update July 2015:

      上面提到的想法 如何在Docker容器中设置静态IP地址?中也有详细说明,使用:

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

      • Building your own bridge

      结果应该是Docker服务器成功启动,现在准备将容器绑定到新的桥接器。

      暂停验证桥接器的配置后,尝试创建一个容器-您将看到它的IP地址在Docker将自动检测到的新IP地址范围内。

      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.

      您可以使用 brctl显示命令以查看Docker在启动和停止容器时添加和从网桥删除接口的操作,并且可以在容器内运行ip addr和ip route来查看是否已在网桥的IP地址范围内为其分配了地址。告诉使用桥上的Docker主机的IP地址作为通往Internet其余部分的默认网关。

      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.




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

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

          使用管道(下面的192.168.1.1是默认网关ip地址):

          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天全站免登陆