How to set a specific (fixed) IP address when I create a docker machine or container?
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?
One Solution collect form web for “How to set a specific (fixed) IP address when I create a docker machine or container?”
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:
docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.98.1/24" m98
docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.97.1/24" m97
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
.100IP upon start.
(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.shthat is searched by
boot2dockerstartup 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 (
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
prova-discovery by the name of your docker-machine)
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:
- 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 showcommand 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/dockercan set for you by default)
Use pipework (192.168.1.1 below being the default gateway ip address):
pipework br0 container-name email@example.com