2013-12-06 18:26:57 -05:00
|
|
|
:title: Configure Networking
|
2013-12-05 13:02:19 -05:00
|
|
|
:description: Docker networking
|
|
|
|
:keywords: network, networking, bridge, docker, documentation
|
|
|
|
|
|
|
|
|
2013-12-06 18:26:57 -05:00
|
|
|
Configure Networking
|
|
|
|
====================
|
2013-12-05 13:02:19 -05:00
|
|
|
|
2013-12-06 18:26:57 -05:00
|
|
|
Docker uses Linux bridge capabilities to provide network connectivity
|
|
|
|
to containers. The ``docker0`` bridge interface is managed by Docker
|
2014-01-22 01:24:04 -05:00
|
|
|
for this purpose. When the Docker daemon starts it :
|
2013-12-05 13:02:19 -05:00
|
|
|
|
|
|
|
- creates the ``docker0`` bridge if not present
|
|
|
|
- searches for an IP address range which doesn't overlap with an existing route
|
|
|
|
- picks an IP in the selected range
|
|
|
|
- assigns this IP to the ``docker0`` bridge
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# List host bridges
|
|
|
|
$ sudo brctl show
|
|
|
|
bridge name bridge id STP enabled interfaces
|
|
|
|
docker0 8000.000000000000 no
|
|
|
|
|
|
|
|
# Show docker0 IP address
|
|
|
|
$ sudo ifconfig docker0
|
|
|
|
docker0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx
|
|
|
|
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-12-06 18:26:57 -05:00
|
|
|
At runtime, a :ref:`specific kind of virtual
|
2014-01-22 01:08:11 -05:00
|
|
|
interface<vethxxxx-device>` is given to each container which is then
|
|
|
|
bonded to the ``docker0`` bridge. Each container also receives a
|
2013-12-06 18:26:57 -05:00
|
|
|
dedicated IP address from the same range as ``docker0``. The
|
2014-01-22 01:24:04 -05:00
|
|
|
``docker0`` IP address is used as the default gateway for the
|
2014-01-22 01:08:11 -05:00
|
|
|
container.
|
2013-12-05 13:02:19 -05:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Run a container
|
|
|
|
$ sudo docker run -t -i -d base /bin/bash
|
|
|
|
52f811c5d3d69edddefc75aff5a4525fc8ba8bcfa1818132f9dc7d4f7c7e78b4
|
|
|
|
|
|
|
|
$ sudo brctl show
|
|
|
|
bridge name bridge id STP enabled interfaces
|
|
|
|
docker0 8000.fef213db5a66 no vethQCDY1N
|
|
|
|
|
|
|
|
|
2013-12-06 18:26:57 -05:00
|
|
|
Above, ``docker0`` acts as a bridge for the ``vethQCDY1N`` interface
|
|
|
|
which is dedicated to the 52f811c5d3d6 container.
|
2013-12-05 13:02:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
How to use a specific IP address range
|
|
|
|
---------------------------------------
|
|
|
|
|
2014-01-22 01:24:04 -05:00
|
|
|
Docker will try hard to find an IP range that is not used by the
|
|
|
|
host. Even though it works for most cases, it's not bullet-proof and
|
2013-12-06 18:26:57 -05:00
|
|
|
sometimes you need to have more control over the IP addressing scheme.
|
|
|
|
|
|
|
|
For this purpose, Docker allows you to manage the ``docker0`` bridge
|
|
|
|
or your own one using the ``-b=<bridgename>`` parameter.
|
2013-12-05 13:02:19 -05:00
|
|
|
|
|
|
|
In this scenario:
|
|
|
|
|
|
|
|
- ensure Docker is stopped
|
|
|
|
- create your own bridge (``bridge0`` for example)
|
|
|
|
- assign a specific IP to this bridge
|
|
|
|
- start Docker with the ``-b=bridge0`` parameter
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# Stop Docker
|
|
|
|
$ sudo service docker stop
|
|
|
|
|
|
|
|
# Clean docker0 bridge and
|
|
|
|
# add your very own bridge0
|
|
|
|
$ sudo ifconfig docker0 down
|
|
|
|
$ sudo brctl addbr bridge0
|
|
|
|
$ sudo ifconfig bridge0 192.168.227.1 netmask 255.255.255.0
|
|
|
|
|
|
|
|
# Edit your Docker startup file
|
2014-01-07 09:20:38 -05:00
|
|
|
$ echo "DOCKER_OPTS=\"-b=bridge0\"" >> /etc/default/docker
|
2013-12-05 13:02:19 -05:00
|
|
|
|
|
|
|
# Start Docker
|
|
|
|
$ sudo service docker start
|
|
|
|
|
|
|
|
# Ensure bridge0 IP is not changed by Docker
|
|
|
|
$ sudo ifconfig bridge0
|
|
|
|
bridge0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx
|
|
|
|
inet addr:192.168.227.1 Bcast:192.168.227.255 Mask:255.255.255.0
|
|
|
|
|
|
|
|
# Run a container
|
|
|
|
$ docker run -i -t base /bin/bash
|
|
|
|
|
|
|
|
# Container IP in the 192.168.227/24 range
|
|
|
|
root@261c272cd7d5:/# ifconfig eth0
|
|
|
|
eth0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx
|
|
|
|
inet addr:192.168.227.5 Bcast:192.168.227.255 Mask:255.255.255.0
|
|
|
|
|
|
|
|
# bridge0 IP as the default gateway
|
|
|
|
root@261c272cd7d5:/# route -n
|
|
|
|
Kernel IP routing table
|
|
|
|
Destination Gateway Genmask Flags Metric Ref Use Iface
|
|
|
|
0.0.0.0 192.168.227.1 0.0.0.0 UG 0 0 0 eth0
|
|
|
|
192.168.227.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
|
|
|
|
|
|
|
|
# hits CTRL+P then CTRL+Q to detach
|
|
|
|
|
|
|
|
# Display bridge info
|
|
|
|
$ sudo brctl show
|
|
|
|
bridge name bridge id STP enabled interfaces
|
|
|
|
bridge0 8000.fe7c2e0faebd no vethAQI2QT
|
|
|
|
|
|
|
|
|
|
|
|
Container intercommunication
|
|
|
|
-------------------------------
|
2013-12-06 18:26:57 -05:00
|
|
|
|
2014-01-22 01:24:04 -05:00
|
|
|
The value of the Docker daemon's ``icc`` parameter determines whether
|
|
|
|
containers can communicate with each other over the bridge network.
|
2013-12-05 13:02:19 -05:00
|
|
|
|
2014-03-13 13:46:02 -04:00
|
|
|
- The default, ``--icc=true`` allows containers to communicate with each other.
|
|
|
|
- ``--icc=false`` means containers are isolated from each other.
|
2013-12-05 13:02:19 -05:00
|
|
|
|
2014-01-22 01:24:04 -05:00
|
|
|
Docker uses ``iptables`` under the hood to either accept or
|
2013-12-06 18:26:57 -05:00
|
|
|
drop communication between containers.
|
2013-12-05 13:02:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
.. _vethxxxx-device:
|
|
|
|
|
2014-01-22 01:24:04 -05:00
|
|
|
What is the vethXXXX device?
|
2013-12-05 13:02:19 -05:00
|
|
|
-----------------------------------
|
|
|
|
Well. Things get complicated here.
|
|
|
|
|
2013-12-06 18:26:57 -05:00
|
|
|
The ``vethXXXX`` interface is the host side of a point-to-point link
|
2014-01-22 01:08:11 -05:00
|
|
|
between the host and the corresponding container; the other side of
|
2014-01-22 01:24:04 -05:00
|
|
|
the link is the container's ``eth0``
|
2013-12-06 18:26:57 -05:00
|
|
|
interface. This pair (host ``vethXXX`` and container ``eth0``) are
|
|
|
|
connected like a tube. Everything that comes in one side will come out
|
|
|
|
the other side.
|
2013-12-05 13:02:19 -05:00
|
|
|
|
2013-12-06 18:26:57 -05:00
|
|
|
All the plumbing is delegated to Linux network capabilities (check the
|
|
|
|
ip link command) and the namespaces infrastructure.
|
2013-12-05 13:02:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
I want more
|
|
|
|
------------
|
2013-12-06 18:26:57 -05:00
|
|
|
|
2014-03-07 08:45:08 -05:00
|
|
|
Jérôme Petazzoni has created ``pipework`` to connect together
|
2013-12-06 18:26:57 -05:00
|
|
|
containers in arbitrarily complex scenarios :
|
|
|
|
https://github.com/jpetazzo/pipework
|