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

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

问题描述

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



可以吗?



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

解决方案

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






更新2015年11月:类似的问题在 docker /机器问题1709 ,其中包括最近的解决方法(2015年11月) Tobias Munk( schmunk42 for 码头机器

(容器见下一节):


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





  • 192.168 .98.100

      docker-machine create -d virtualbox --virtualbox-hostonly-cidr192.168.98.1/24m98 


  • 192.168.97.100

      docker-machine create -d virtualbox --virtualbox-hostonly-cidr192.168.97.1/24m97 


  • 192.168.96.100

      docker-machine create -d virtualbox --virtualbox-hostonly -cidr192.168.96.1/24m96 





如果没有其他机器具有相同的 cidr(Classless Inter-Domain Routing) ,机器应始终在启动时得到 .100 IP


另一种解决方法



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




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



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




  echoifconfig 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




此命令创建一个文件 c code code code code code code code code code code在机器启动期间,执行命令并设置静态IP。




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

Michele Tedeschi( micheletedeschi 添加



我已经更新了命令:

  echokill`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




然后运行命令(只有第一次) / p>



  docker-machine regenerate-certs prova-discovery 




现在,DHCP不会更改IP


(以您的码头机的名称替换 prova-discovery






2015年4月:



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


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



如果您手动进行操作,它将如下所示(在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地址,使用静态创建桥梁和一对对等接口的脚本。



2015年7月更新:



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






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

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



您可以使用 brctl show 命令可以在启动和停止容器时看到Docker添加和删除接口,并且可以在容器内运行ip addr和ip route,以便在桥接器的IP地址范围内给出一个地址告诉您使用Docker主机的IP地址作为Internet的其余部分的默认网关。





  • 启动码头工具: -b = br0 (也是 echo'DOCKER_OPTS = - b = bridge0'> ;> / etc / default / docker 可以默认设置)


  • 使用管道作业(192.168.1.1以下是默认网关ip地址):

      pipework br0 container-name 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`\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

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
    

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

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