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?

  • Docker crash test with many containers of the same image
  • Docker-Tuleap-List all Tuleap database tables running in docker container
  • OpenVSwitch in container with IP-Tables
  • Force a problematic docker container to restart itself?
  • Running Visual Studio Remote Debugger in Windows Container (Docker managed)
  • Not copy container volume to host when docker run
  • If not, after the creation can I edit the DHCP IP address?

  • no space left on device while installing a tar using docker
  • How can I inspect the file system of a failed `docker build`?
  • Unable to connect to database from rails console on dockers
  • localhost within docker user defined network?
  • How to restart kubernetes Pods?
  • Setup a docker MS build server image
  • 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 "" m98

      docker-machine create -d virtualbox --virtualbox-hostonly-cidr "" m97

      docker-machine create -d virtualbox --virtualbox-hostonly-cidr "" 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 – 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 netmask broadcast up" \
    | docker-machine ssh prova-discovery sudo tee /var/lib/boot2docker/ > /dev/null 

    This command create a file 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://      test-1 (mast

    Michele Tedeschi (micheletedeschi) adds

    I’ve updated the commands with:

    echo "kill `more /var/run/`\nifconfig eth1 netmask broadcast up" | docker-machine ssh prova-discovery sudo tee /var/lib/boot2docker/ > /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 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 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 ( below being the default gateway ip address):

      pipework br0 container-name
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.