创建docker机器或容器时如何设置特定(固定)IP地址?

前端之家收集整理的这篇文章主要介绍了创建docker机器或容器时如何设置特定(固定)IP地址?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

那可能吗?

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

考虑到(现在是旧的2013年10月)文章How to configure Docker to start containers on a specific IP address range”的结论,这似乎不可能(或者至少“由Docker自动完成”).

2015年11月更新:在docker/machine issue 1709中讨论了类似的问题,其中包括Tobias Munk (schmunk42) recent workaround (Nov 2015)为docker machine提出的问题
(对于容器,请参阅下一节):

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 07004,the machine should always get the .100 IP upon start.

另一种解决方法

(参见“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

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

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

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

2015年4月:

文章提到了create your own bridge的可能性(但是这并没有将其中一个IP地址分配给容器):

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

要在现有网桥IP范围内分配静态IP,可以使用创建网桥和一对对等接口的静态脚本尝试“How can I set a static IP address in a Docker container?”.

2015年7月更新:

上面提到的想法也在“How can I set a static IP address in a Docker container?”中详述,使用:

> Building your own bridge

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.

>启动docker:-b = br0(也就是回声’DOCKER_OPTS =“ – b = bridge0”’>> / etc / default / docker默认设置为你)
>使用pipework(下面的192.168.1.1是默认网关IP地址):

pipework br0 container-name 192.168.1.10/24@192.168.1.1

猜你在找的Docker相关文章