github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/docs/sources/articles/networking.md (about)

     1  page_title: Network Configuration
     2  page_description: Docker networking
     3  page_keywords: network, networking, bridge, docker, documentation
     4  
     5  # Network Configuration
     6  
     7  ## TL;DR
     8  
     9  When Docker starts, it creates a virtual interface named `docker0` on
    10  the host machine.  It randomly chooses an address and subnet from the
    11  private range defined by [RFC 1918](http://tools.ietf.org/html/rfc1918)
    12  that are not in use on the host machine, and assigns it to `docker0`.
    13  Docker made the choice `172.17.42.1/16` when I started it a few minutes
    14  ago, for example — a 16-bit netmask providing 65,534 addresses for the
    15  host machine and its containers.
    16  
    17  > **Note:**
    18  > This document discusses advanced networking configuration
    19  > and options for Docker. In most cases you won't need this information.
    20  > If you're looking to get started with a simpler explanation of Docker
    21  > networking and an introduction to the concept of container linking see
    22  > the [Docker User Guide](/userguide/dockerlinks/).
    23  
    24  But `docker0` is no ordinary interface.  It is a virtual *Ethernet
    25  bridge* that automatically forwards packets between any other network
    26  interfaces that are attached to it.  This lets containers communicate
    27  both with the host machine and with each other.  Every time Docker
    28  creates a container, it creates a pair of “peer” interfaces that are
    29  like opposite ends of a pipe — a packet sent on one will be received on
    30  the other.  It gives one of the peers to the container to become its
    31  `eth0` interface and keeps the other peer, with a unique name like
    32  `vethAQI2QT`, out in the namespace of the host machine.  By binding
    33  every `veth*` interface to the `docker0` bridge, Docker creates a
    34  virtual subnet shared between the host machine and every Docker
    35  container.
    36  
    37  The remaining sections of this document explain all of the ways that you
    38  can use Docker options and — in advanced cases — raw Linux networking
    39  commands to tweak, supplement, or entirely replace Docker's default
    40  networking configuration.
    41  
    42  ## Quick Guide to the Options
    43  
    44  Here is a quick list of the networking-related Docker command-line
    45  options, in case it helps you find the section below that you are
    46  looking for.
    47  
    48  Some networking command-line options can only be supplied to the Docker
    49  server when it starts up, and cannot be changed once it is running:
    50  
    51   *  `-b BRIDGE` or `--bridge=BRIDGE` — see
    52      [Building your own bridge](#bridge-building)
    53  
    54   *  `--bip=CIDR` — see
    55      [Customizing docker0](#docker0)
    56  
    57   *  `--fixed-cidr` — see
    58      [Customizing docker0](#docker0)
    59  
    60   *  `-H SOCKET...` or `--host=SOCKET...` —
    61      This might sound like it would affect container networking,
    62      but it actually faces in the other direction:
    63      it tells the Docker server over what channels
    64      it should be willing to receive commands
    65      like “run container” and “stop container.”
    66  
    67   *  `--icc=true|false` — see
    68      [Communication between containers](#between-containers)
    69  
    70   *  `--ip=IP_ADDRESS` — see
    71      [Binding container ports](#binding-ports)
    72  
    73   *  `--ip-forward=true|false` — see
    74      [Communication between containers](#between-containers)
    75  
    76   *  `--iptables=true|false` — see
    77      [Communication between containers](#between-containers)
    78  
    79   *  `--mtu=BYTES` — see
    80      [Customizing docker0](#docker0)
    81  
    82  There are two networking options that can be supplied either at startup
    83  or when `docker run` is invoked.  When provided at startup, set the
    84  default value that `docker run` will later use if the options are not
    85  specified:
    86  
    87   *  `--dns=IP_ADDRESS...` — see
    88      [Configuring DNS](#dns)
    89  
    90   *  `--dns-search=DOMAIN...` — see
    91      [Configuring DNS](#dns)
    92  
    93  Finally, several networking options can only be provided when calling
    94  `docker run` because they specify something specific to one container:
    95  
    96   *  `-h HOSTNAME` or `--hostname=HOSTNAME` — see
    97      [Configuring DNS](#dns) and
    98      [How Docker networks a container](#container-networking)
    99  
   100   *  `--link=CONTAINER_NAME:ALIAS` — see
   101      [Configuring DNS](#dns) and
   102      [Communication between containers](#between-containers)
   103  
   104   *  `--net=bridge|none|container:NAME_or_ID|host` — see
   105      [How Docker networks a container](#container-networking)
   106  
   107   *  `--mac-address=MACADDRESS...` — see
   108      [How Docker networks a container](#container-networking)
   109  
   110   *  `-p SPEC` or `--publish=SPEC` — see
   111      [Binding container ports](#binding-ports)
   112  
   113   *  `-P` or `--publish-all=true|false` — see
   114      [Binding container ports](#binding-ports)
   115  
   116  The following sections tackle all of the above topics in an order that
   117  moves roughly from simplest to most complex.
   118  
   119  ## Configuring DNS
   120  
   121  <a name="dns"></a>
   122  
   123  How can Docker supply each container with a hostname and DNS
   124  configuration, without having to build a custom image with the hostname
   125  written inside?  Its trick is to overlay three crucial `/etc` files
   126  inside the container with virtual files where it can write fresh
   127  information.  You can see this by running `mount` inside a container:
   128  
   129      $$ mount
   130      ...
   131      /dev/disk/by-uuid/1fec...ebdf on /etc/hostname type ext4 ...
   132      /dev/disk/by-uuid/1fec...ebdf on /etc/hosts type ext4 ...
   133      tmpfs on /etc/resolv.conf type tmpfs ...
   134      ...
   135  
   136  This arrangement allows Docker to do clever things like keep
   137  `resolv.conf` up to date across all containers when the host machine
   138  receives new configuration over DHCP later.  The exact details of how
   139  Docker maintains these files inside the container can change from one
   140  Docker version to the next, so you should leave the files themselves
   141  alone and use the following Docker options instead.
   142  
   143  Four different options affect container domain name services.
   144  
   145   *  `-h HOSTNAME` or `--hostname=HOSTNAME` — sets the hostname by which
   146      the container knows itself.  This is written into `/etc/hostname`,
   147      into `/etc/hosts` as the name of the container's host-facing IP
   148      address, and is the name that `/bin/bash` inside the container will
   149      display inside its prompt.  But the hostname is not easy to see from
   150      outside the container.  It will not appear in `docker ps` nor in the
   151      `/etc/hosts` file of any other container.
   152  
   153   *  `--link=CONTAINER_NAME:ALIAS` — using this option as you `run` a
   154      container gives the new container's `/etc/hosts` an extra entry
   155      named `ALIAS` that points to the IP address of the container named
   156      `CONTAINER_NAME`.  This lets processes inside the new container
   157      connect to the hostname `ALIAS` without having to know its IP.  The
   158      `--link=` option is discussed in more detail below, in the section
   159      [Communication between containers](#between-containers). Because
   160      Docker may assign a different IP address to the linked containers
   161      on restart, Docker updates the `ALIAS` entry in the `/etc/hosts` file
   162      of the recipient containers.
   163  
   164   *  `--dns=IP_ADDRESS...` — sets the IP addresses added as `server`
   165      lines to the container's `/etc/resolv.conf` file.  Processes in the
   166      container, when confronted with a hostname not in `/etc/hosts`, will
   167      connect to these IP addresses on port 53 looking for name resolution
   168      services.
   169  
   170   *  `--dns-search=DOMAIN...` — sets the domain names that are searched
   171      when a bare unqualified hostname is used inside of the container, by
   172      writing `search` lines into the container's `/etc/resolv.conf`.
   173      When a container process attempts to access `host` and the search
   174      domain `example.com` is set, for instance, the DNS logic will not
   175      only look up `host` but also `host.example.com`.
   176      Use `--dns-search=.` if you don't wish to set the search domain.
   177  
   178  Note that Docker, in the absence of either of the last two options
   179  above, will make `/etc/resolv.conf` inside of each container look like
   180  the `/etc/resolv.conf` of the host machine where the `docker` daemon is
   181  running.  The options then modify this default configuration.
   182  
   183  ## Communication between containers and the wider world
   184  
   185  <a name="the-world"></a>
   186  
   187  Whether a container can talk to the world is governed by two factors.
   188  
   189  1.  Is the host machine willing to forward IP packets?  This is governed
   190      by the `ip_forward` system parameter.  Packets can only pass between
   191      containers if this parameter is `1`.  Usually you will simply leave
   192      the Docker server at its default setting `--ip-forward=true` and
   193      Docker will go set `ip_forward` to `1` for you when the server
   194      starts up.  To check the setting or turn it on manually:
   195  
   196      ```
   197      $ cat /proc/sys/net/ipv4/ip_forward
   198      0
   199      $ echo 1 > /proc/sys/net/ipv4/ip_forward
   200      $ cat /proc/sys/net/ipv4/ip_forward
   201      1
   202      ```
   203  
   204      Many using Docker will want `ip_forward` to be on, to at
   205      least make communication *possible* between containers and
   206      the wider world.
   207  
   208      May also be needed for inter-container communication if you are
   209      in a multiple bridge setup.
   210  
   211  2.  Do your `iptables` allow this particular connection? Docker will
   212      never make changes to your system `iptables` rules if you set
   213      `--iptables=false` when the daemon starts.  Otherwise the Docker
   214      server will append forwarding rules to the `DOCKER` filter chain.
   215  
   216  Docker will not delete or modify any pre-existing rules from the `DOCKER`
   217  filter chain. This allows the user to create in advance any rules required
   218  to further restrict access to the containers.
   219  
   220  Docker's forward rules permit all external source IPs by default. To allow
   221  only a specific IP or network to access the containers, insert a negated
   222  rule at the top of the `DOCKER` filter chain. For example, to restrict
   223  external access such that *only* source IP 8.8.8.8 can access the
   224  containers, the following rule could be added:
   225  
   226      $ iptables -I DOCKER -i ext_if ! -s 8.8.8.8 -j DROP
   227  
   228  ## Communication between containers
   229  
   230  <a name="between-containers"></a>
   231  
   232  Whether two containers can communicate is governed, at the operating
   233  system level, by two factors.
   234  
   235  1.  Does the network topology even connect the containers' network
   236      interfaces?  By default Docker will attach all containers to a
   237      single `docker0` bridge, providing a path for packets to travel
   238      between them.  See the later sections of this document for other
   239      possible topologies.
   240  
   241  2.  Do your `iptables` allow this particular connection? Docker will never
   242      make changes to your system `iptables` rules if you set
   243      `--iptables=false` when the daemon starts.  Otherwise the Docker server
   244      will add a default rule to the `FORWARD` chain with a blanket `ACCEPT`
   245      policy if you retain the default `--icc=true`, or else will set the
   246      policy to `DROP` if `--icc=false`.
   247  
   248  It is a strategic question whether to leave `--icc=true` or change it to
   249  `--icc=false` (on Ubuntu, by editing the `DOCKER_OPTS` variable in
   250  `/etc/default/docker` and restarting the Docker server) so that
   251  `iptables` will protect other containers — and the main host — from
   252  having arbitrary ports probed or accessed by a container that gets
   253  compromised.
   254  
   255  If you choose the most secure setting of `--icc=false`, then how can
   256  containers communicate in those cases where you *want* them to provide
   257  each other services?
   258  
   259  The answer is the `--link=CONTAINER_NAME:ALIAS` option, which was
   260  mentioned in the previous section because of its effect upon name
   261  services.  If the Docker daemon is running with both `--icc=false` and
   262  `--iptables=true` then, when it sees `docker run` invoked with the
   263  `--link=` option, the Docker server will insert a pair of `iptables`
   264  `ACCEPT` rules so that the new container can connect to the ports
   265  exposed by the other container — the ports that it mentioned in the
   266  `EXPOSE` lines of its `Dockerfile`.  Docker has more documentation on
   267  this subject — see the [linking Docker containers](/userguide/dockerlinks)
   268  page for further details.
   269  
   270  > **Note**:
   271  > The value `CONTAINER_NAME` in `--link=` must either be an
   272  > auto-assigned Docker name like `stupefied_pare` or else the name you
   273  > assigned with `--name=` when you ran `docker run`.  It cannot be a
   274  > hostname, which Docker will not recognize in the context of the
   275  > `--link=` option.
   276  
   277  You can run the `iptables` command on your Docker host to see whether
   278  the `FORWARD` chain has a default policy of `ACCEPT` or `DROP`:
   279  
   280      # When --icc=false, you should see a DROP rule:
   281  
   282      $ sudo iptables -L -n
   283      ...
   284      Chain FORWARD (policy ACCEPT)
   285      target     prot opt source               destination
   286      DOCKER     all  --  0.0.0.0/0            0.0.0.0/0
   287      DROP       all  --  0.0.0.0/0            0.0.0.0/0
   288      ...
   289  
   290      # When a --link= has been created under --icc=false,
   291      # you should see port-specific ACCEPT rules overriding
   292      # the subsequent DROP policy for all other packets:
   293  
   294      $ sudo iptables -L -n
   295      ...
   296      Chain FORWARD (policy ACCEPT)
   297      target     prot opt source               destination
   298      DOCKER     all  --  0.0.0.0/0            0.0.0.0/0
   299      DROP       all  --  0.0.0.0/0            0.0.0.0/0
   300  
   301      Chain DOCKER (1 references)
   302      target     prot opt source               destination
   303      ACCEPT     tcp  --  172.17.0.2           172.17.0.3           tcp spt:80
   304      ACCEPT     tcp  --  172.17.0.3           172.17.0.2           tcp dpt:80
   305  
   306  > **Note**:
   307  > Docker is careful that its host-wide `iptables` rules fully expose
   308  > containers to each other's raw IP addresses, so connections from one
   309  > container to another should always appear to be originating from the
   310  > first container's own IP address.
   311  
   312  ## Binding container ports to the host
   313  
   314  <a name="binding-ports"></a>
   315  
   316  By default Docker containers can make connections to the outside world,
   317  but the outside world cannot connect to containers.  Each outgoing
   318  connection will appear to originate from one of the host machine's own
   319  IP addresses thanks to an `iptables` masquerading rule on the host
   320  machine that the Docker server creates when it starts:
   321  
   322      # You can see that the Docker server creates a
   323      # masquerade rule that let containers connect
   324      # to IP addresses in the outside world:
   325  
   326      $ sudo iptables -t nat -L -n
   327      ...
   328      Chain POSTROUTING (policy ACCEPT)
   329      target     prot opt source               destination
   330      MASQUERADE  all  --  172.17.0.0/16       !172.17.0.0/16
   331      ...
   332  
   333  But if you want containers to accept incoming connections, you will need
   334  to provide special options when invoking `docker run`.  These options
   335  are covered in more detail in the [Docker User Guide](/userguide/dockerlinks)
   336  page.  There are two approaches.
   337  
   338  First, you can supply `-P` or `--publish-all=true|false` to `docker run`
   339  which is a blanket operation that identifies every port with an `EXPOSE`
   340  line in the image's `Dockerfile` and maps it to a host port somewhere in
   341  the range 49153–65535.  This tends to be a bit inconvenient, since you
   342  then have to run other `docker` sub-commands to learn which external
   343  port a given service was mapped to.
   344  
   345  More convenient is the `-p SPEC` or `--publish=SPEC` option which lets
   346  you be explicit about exactly which external port on the Docker server —
   347  which can be any port at all, not just those in the 49153-65535 block —
   348  you want mapped to which port in the container.
   349  
   350  Either way, you should be able to peek at what Docker has accomplished
   351  in your network stack by examining your NAT tables.
   352  
   353      # What your NAT rules might look like when Docker
   354      # is finished setting up a -P forward:
   355  
   356      $ iptables -t nat -L -n
   357      ...
   358      Chain DOCKER (2 references)
   359      target     prot opt source               destination
   360      DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:49153 to:172.17.0.2:80
   361  
   362      # What your NAT rules might look like when Docker
   363      # is finished setting up a -p 80:80 forward:
   364  
   365      Chain DOCKER (2 references)
   366      target     prot opt source               destination
   367      DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 to:172.17.0.2:80
   368  
   369  You can see that Docker has exposed these container ports on `0.0.0.0`,
   370  the wildcard IP address that will match any possible incoming port on
   371  the host machine.  If you want to be more restrictive and only allow
   372  container services to be contacted through a specific external interface
   373  on the host machine, you have two choices.  When you invoke `docker run`
   374  you can use either `-p IP:host_port:container_port` or `-p IP::port` to
   375  specify the external interface for one particular binding.
   376  
   377  Or if you always want Docker port forwards to bind to one specific IP
   378  address, you can edit your system-wide Docker server settings (on
   379  Ubuntu, by editing `DOCKER_OPTS` in `/etc/default/docker`) and add the
   380  option `--ip=IP_ADDRESS`.  Remember to restart your Docker server after
   381  editing this setting.
   382  
   383  Again, this topic is covered without all of these low-level networking
   384  details in the [Docker User Guide](/userguide/dockerlinks/) document if you
   385  would like to use that as your port redirection reference instead.
   386  
   387  ## Customizing docker0
   388  
   389  <a name="docker0"></a>
   390  
   391  By default, the Docker server creates and configures the host system's
   392  `docker0` interface as an *Ethernet bridge* inside the Linux kernel that
   393  can pass packets back and forth between other physical or virtual
   394  network interfaces so that they behave as a single Ethernet network.
   395  
   396  Docker configures `docker0` with an IP address, netmask and IP
   397  allocation range. The host machine can both receive and send packets to
   398  containers connected to the bridge, and gives it an MTU — the *maximum
   399  transmission unit* or largest packet length that the interface will
   400  allow — of either 1,500 bytes or else a more specific value copied from
   401  the Docker host's interface that supports its default route.  These
   402  options are configurable at server startup:
   403  
   404   *  `--bip=CIDR` — supply a specific IP address and netmask for the
   405      `docker0` bridge, using standard CIDR notation like
   406      `192.168.1.5/24`.
   407  
   408   *  `--fixed-cidr=CIDR` — restrict the IP range from the `docker0` subnet,
   409      using the standard CIDR notation like `172.167.1.0/28`. This range must
   410      be and IPv4 range for fixed IPs (ex: 10.20.0.0/16) and must be a subset
   411      of the bridge IP range (`docker0` or set using `--bridge`). For example
   412      with `--fixed-cidr=192.168.1.0/25`, IPs for your containers will be chosen
   413      from the first half of `192.168.1.0/24` subnet.
   414  
   415   *  `--mtu=BYTES` — override the maximum packet length on `docker0`.
   416  
   417  On Ubuntu you would add these to the `DOCKER_OPTS` setting in
   418  `/etc/default/docker` on your Docker host and restarting the Docker
   419  service.
   420  
   421  Once you have one or more containers up and running, you can confirm
   422  that Docker has properly connected them to the `docker0` bridge by
   423  running the `brctl` command on the host machine and looking at the
   424  `interfaces` column of the output.  Here is a host with two different
   425  containers connected:
   426  
   427      # Display bridge info
   428  
   429      $ sudo brctl show
   430      bridge name     bridge id               STP enabled     interfaces
   431      docker0         8000.3a1d7362b4ee       no              veth65f9
   432                                                              vethdda6
   433  
   434  If the `brctl` command is not installed on your Docker host, then on
   435  Ubuntu you should be able to run `sudo apt-get install bridge-utils` to
   436  install it.
   437  
   438  Finally, the `docker0` Ethernet bridge settings are used every time you
   439  create a new container.  Docker selects a free IP address from the range
   440  available on the bridge each time you `docker run` a new container, and
   441  configures the container's `eth0` interface with that IP address and the
   442  bridge's netmask.  The Docker host's own IP address on the bridge is
   443  used as the default gateway by which each container reaches the rest of
   444  the Internet.
   445  
   446      # The network, as seen from a container
   447  
   448      $ sudo docker run -i -t --rm base /bin/bash
   449  
   450      $$ ip addr show eth0
   451      24: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
   452          link/ether 32:6f:e0:35:57:91 brd ff:ff:ff:ff:ff:ff
   453          inet 172.17.0.3/16 scope global eth0
   454             valid_lft forever preferred_lft forever
   455          inet6 fe80::306f:e0ff:fe35:5791/64 scope link
   456             valid_lft forever preferred_lft forever
   457  
   458      $$ ip route
   459      default via 172.17.42.1 dev eth0
   460      172.17.0.0/16 dev eth0  proto kernel  scope link  src 172.17.0.3
   461  
   462      $$ exit
   463  
   464  Remember that the Docker host will not be willing to forward container
   465  packets out on to the Internet unless its `ip_forward` system setting is
   466  `1` — see the section above on [Communication between
   467  containers](#between-containers) for details.
   468  
   469  ## Building your own bridge
   470  
   471  <a name="bridge-building"></a>
   472  
   473  If you want to take Docker out of the business of creating its own
   474  Ethernet bridge entirely, you can set up your own bridge before starting
   475  Docker and use `-b BRIDGE` or `--bridge=BRIDGE` to tell Docker to use
   476  your bridge instead.  If you already have Docker up and running with its
   477  old `docker0` still configured, you will probably want to begin by
   478  stopping the service and removing the interface:
   479  
   480      # Stopping Docker and removing docker0
   481  
   482      $ sudo service docker stop
   483      $ sudo ip link set dev docker0 down
   484      $ sudo brctl delbr docker0
   485  
   486  Then, before starting the Docker service, create your own bridge and
   487  give it whatever configuration you want.  Here we will create a simple
   488  enough bridge that we really could just have used the options in the
   489  previous section to customize `docker0`, but it will be enough to
   490  illustrate the technique.
   491  
   492      # Create our own bridge
   493  
   494      $ sudo brctl addbr bridge0
   495      $ sudo ip addr add 192.168.5.1/24 dev bridge0
   496      $ sudo ip link set dev bridge0 up
   497  
   498      # Confirming that our bridge is up and running
   499  
   500      $ ip addr show bridge0
   501      4: bridge0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state UP group default
   502          link/ether 66:38:d0:0d:76:18 brd ff:ff:ff:ff:ff:ff
   503          inet 192.168.5.1/24 scope global bridge0
   504             valid_lft forever preferred_lft forever
   505  
   506      # Tell Docker about it and restart (on Ubuntu)
   507  
   508      $ echo 'DOCKER_OPTS="-b=bridge0"' >> /etc/default/docker
   509      $ sudo service docker start
   510  
   511  The result should be that the Docker server starts successfully and is
   512  now prepared to bind containers to the new bridge.  After pausing to
   513  verify the bridge's configuration, try creating a container — you will
   514  see that its IP address is in your new IP address range, which Docker
   515  will have auto-detected.
   516  
   517  Just as we learned in the previous section, you can use the `brctl show`
   518  command to see Docker add and remove interfaces from the bridge as you
   519  start and stop containers, and can run `ip addr` and `ip route` inside a
   520  container to see that it has been given an address in the bridge's IP
   521  address range and has been told to use the Docker host's IP address on
   522  the bridge as its default gateway to the rest of the Internet.
   523  
   524  ## How Docker networks a container
   525  
   526  <a name="container-networking"></a>
   527  
   528  While Docker is under active development and continues to tweak and
   529  improve its network configuration logic, the shell commands in this
   530  section are rough equivalents to the steps that Docker takes when
   531  configuring networking for each new container.
   532  
   533  Let's review a few basics.
   534  
   535  To communicate using the Internet Protocol (IP), a machine needs access
   536  to at least one network interface at which packets can be sent and
   537  received, and a routing table that defines the range of IP addresses
   538  reachable through that interface.  Network interfaces do not have to be
   539  physical devices.  In fact, the `lo` loopback interface available on
   540  every Linux machine (and inside each Docker container) is entirely
   541  virtual — the Linux kernel simply copies loopback packets directly from
   542  the sender's memory into the receiver's memory.
   543  
   544  Docker uses special virtual interfaces to let containers communicate
   545  with the host machine — pairs of virtual interfaces called “peers” that
   546  are linked inside of the host machine's kernel so that packets can
   547  travel between them.  They are simple to create, as we will see in a
   548  moment.
   549  
   550  The steps with which Docker configures a container are:
   551  
   552  1.  Create a pair of peer virtual interfaces.
   553  
   554  2.  Give one of them a unique name like `veth65f9`, keep it inside of
   555      the main Docker host, and bind it to `docker0` or whatever bridge
   556      Docker is supposed to be using.
   557  
   558  3.  Toss the other interface over the wall into the new container (which
   559      will already have been provided with an `lo` interface) and rename
   560      it to the much prettier name `eth0` since, inside of the container's
   561      separate and unique network interface namespace, there are no
   562      physical interfaces with which this name could collide.
   563  
   564  4.  Set the interface's MAC address according to the `--mac-address`
   565      parameter or generate a random one.
   566  
   567  5.  Give the container's `eth0` a new IP address from within the
   568      bridge's range of network addresses, and set its default route to
   569      the IP address that the Docker host owns on the bridge. If available
   570      the IP address is generated from the MAC address. This prevents ARP
   571      cache invalidation problems, when a new container comes up with an
   572      IP used in the past by another container with another MAC.
   573  
   574  With these steps complete, the container now possesses an `eth0`
   575  (virtual) network card and will find itself able to communicate with
   576  other containers and the rest of the Internet.
   577  
   578  You can opt out of the above process for a particular container by
   579  giving the `--net=` option to `docker run`, which takes four possible
   580  values.
   581  
   582   *  `--net=bridge` — The default action, that connects the container to
   583      the Docker bridge as described above.
   584  
   585   *  `--net=host` — Tells Docker to skip placing the container inside of
   586      a separate network stack.  In essence, this choice tells Docker to
   587      **not containerize the container's networking**!  While container
   588      processes will still be confined to their own filesystem and process
   589      list and resource limits, a quick `ip addr` command will show you
   590      that, network-wise, they live “outside” in the main Docker host and
   591      have full access to its network interfaces.  Note that this does
   592      **not** let the container reconfigure the host network stack — that
   593      would require `--privileged=true` — but it does let container
   594      processes open low-numbered ports like any other root process.
   595      It also allows the container to access local network services
   596      like D-bus.  This can lead to processes in the container being
   597      able to do unexpected things like
   598      [restart your computer](https://github.com/docker/docker/issues/6401).
   599      You should use this option with caution.
   600  
   601   *  `--net=container:NAME_or_ID` — Tells Docker to put this container's
   602      processes inside of the network stack that has already been created
   603      inside of another container.  The new container's processes will be
   604      confined to their own filesystem and process list and resource
   605      limits, but will share the same IP address and port numbers as the
   606      first container, and processes on the two containers will be able to
   607      connect to each other over the loopback interface.
   608  
   609   *  `--net=none` — Tells Docker to put the container inside of its own
   610      network stack but not to take any steps to configure its network,
   611      leaving you free to build any of the custom configurations explored
   612      in the last few sections of this document.
   613  
   614  To get an idea of the steps that are necessary if you use `--net=none`
   615  as described in that last bullet point, here are the commands that you
   616  would run to reach roughly the same configuration as if you had let
   617  Docker do all of the configuration:
   618  
   619      # At one shell, start a container and
   620      # leave its shell idle and running
   621  
   622      $ sudo docker run -i -t --rm --net=none base /bin/bash
   623      root@63f36fc01b5f:/#
   624  
   625      # At another shell, learn the container process ID
   626      # and create its namespace entry in /var/run/netns/
   627      # for the "ip netns" command we will be using below
   628  
   629      $ sudo docker inspect -f '{{.State.Pid}}' 63f36fc01b5f
   630      2778
   631      $ pid=2778
   632      $ sudo mkdir -p /var/run/netns
   633      $ sudo ln -s /proc/$pid/ns/net /var/run/netns/$pid
   634  
   635      # Check the bridge's IP address and netmask
   636  
   637      $ ip addr show docker0
   638      21: docker0: ...
   639      inet 172.17.42.1/16 scope global docker0
   640      ...
   641  
   642      # Create a pair of "peer" interfaces A and B,
   643      # bind the A end to the bridge, and bring it up
   644  
   645      $ sudo ip link add A type veth peer name B
   646      $ sudo brctl addif docker0 A
   647      $ sudo ip link set A up
   648  
   649      # Place B inside the container's network namespace,
   650      # rename to eth0, and activate it with a free IP
   651  
   652      $ sudo ip link set B netns $pid
   653      $ sudo ip netns exec $pid ip link set dev B name eth0
   654      $ sudo ip netns exec $pid ip link set eth0 address 12:34:56:78:9a:bc
   655      $ sudo ip netns exec $pid ip link set eth0 up
   656      $ sudo ip netns exec $pid ip addr add 172.17.42.99/16 dev eth0
   657      $ sudo ip netns exec $pid ip route add default via 172.17.42.1
   658  
   659  At this point your container should be able to perform networking
   660  operations as usual.
   661  
   662  When you finally exit the shell and Docker cleans up the container, the
   663  network namespace is destroyed along with our virtual `eth0` — whose
   664  destruction in turn destroys interface `A` out in the Docker host and
   665  automatically un-registers it from the `docker0` bridge.  So everything
   666  gets cleaned up without our having to run any extra commands!  Well,
   667  almost everything:
   668  
   669      # Clean up dangling symlinks in /var/run/netns
   670  
   671      find -L /var/run/netns -type l -delete
   672  
   673  Also note that while the script above used modern `ip` command instead
   674  of old deprecated wrappers like `ipconfig` and `route`, these older
   675  commands would also have worked inside of our container.  The `ip addr`
   676  command can be typed as `ip a` if you are in a hurry.
   677  
   678  Finally, note the importance of the `ip netns exec` command, which let
   679  us reach inside and configure a network namespace as root.  The same
   680  commands would not have worked if run inside of the container, because
   681  part of safe containerization is that Docker strips container processes
   682  of the right to configure their own networks.  Using `ip netns exec` is
   683  what let us finish up the configuration without having to take the
   684  dangerous step of running the container itself with `--privileged=true`.
   685  
   686  ## Tools and Examples
   687  
   688  Before diving into the following sections on custom network topologies,
   689  you might be interested in glancing at a few external tools or examples
   690  of the same kinds of configuration.  Here are two:
   691  
   692   *  Jérôme Petazzoni has created a `pipework` shell script to help you
   693      connect together containers in arbitrarily complex scenarios:
   694      <https://github.com/jpetazzo/pipework>
   695  
   696   *  Brandon Rhodes has created a whole network topology of Docker
   697      containers for the next edition of Foundations of Python Network
   698      Programming that includes routing, NAT'd firewalls, and servers that
   699      offer HTTP, SMTP, POP, IMAP, Telnet, SSH, and FTP:
   700      <https://github.com/brandon-rhodes/fopnp/tree/m/playground>
   701  
   702  Both tools use networking commands very much like the ones you saw in
   703  the previous section, and will see in the following sections.
   704  
   705  ## Building a point-to-point connection
   706  
   707  <a name="point-to-point"></a>
   708  
   709  By default, Docker attaches all containers to the virtual subnet
   710  implemented by `docker0`.  You can create containers that are each
   711  connected to some different virtual subnet by creating your own bridge
   712  as shown in [Building your own bridge](#bridge-building), starting each
   713  container with `docker run --net=none`, and then attaching the
   714  containers to your bridge with the shell commands shown in [How Docker
   715  networks a container](#container-networking).
   716  
   717  But sometimes you want two particular containers to be able to
   718  communicate directly without the added complexity of both being bound to
   719  a host-wide Ethernet bridge.
   720  
   721  The solution is simple: when you create your pair of peer interfaces,
   722  simply throw *both* of them into containers, and configure them as
   723  classic point-to-point links.  The two containers will then be able to
   724  communicate directly (provided you manage to tell each container the
   725  other's IP address, of course).  You might adjust the instructions of
   726  the previous section to go something like this:
   727  
   728      # Start up two containers in two terminal windows
   729  
   730      $ sudo docker run -i -t --rm --net=none base /bin/bash
   731      root@1f1f4c1f931a:/#
   732  
   733      $ sudo docker run -i -t --rm --net=none base /bin/bash
   734      root@12e343489d2f:/#
   735  
   736      # Learn the container process IDs
   737      # and create their namespace entries
   738  
   739      $ sudo docker inspect -f '{{.State.Pid}}' 1f1f4c1f931a
   740      2989
   741      $ sudo docker inspect -f '{{.State.Pid}}' 12e343489d2f
   742      3004
   743      $ sudo mkdir -p /var/run/netns
   744      $ sudo ln -s /proc/2989/ns/net /var/run/netns/2989
   745      $ sudo ln -s /proc/3004/ns/net /var/run/netns/3004
   746  
   747      # Create the "peer" interfaces and hand them out
   748  
   749      $ sudo ip link add A type veth peer name B
   750  
   751      $ sudo ip link set A netns 2989
   752      $ sudo ip netns exec 2989 ip addr add 10.1.1.1/32 dev A
   753      $ sudo ip netns exec 2989 ip link set A up
   754      $ sudo ip netns exec 2989 ip route add 10.1.1.2/32 dev A
   755  
   756      $ sudo ip link set B netns 3004
   757      $ sudo ip netns exec 3004 ip addr add 10.1.1.2/32 dev B
   758      $ sudo ip netns exec 3004 ip link set B up
   759      $ sudo ip netns exec 3004 ip route add 10.1.1.1/32 dev B
   760  
   761  The two containers should now be able to ping each other and make
   762  connections successfully.  Point-to-point links like this do not depend
   763  on a subnet nor a netmask, but on the bare assertion made by `ip route`
   764  that some other single IP address is connected to a particular network
   765  interface.
   766  
   767  Note that point-to-point links can be safely combined with other kinds
   768  of network connectivity — there is no need to start the containers with
   769  `--net=none` if you want point-to-point links to be an addition to the
   770  container's normal networking instead of a replacement.
   771  
   772  A final permutation of this pattern is to create the point-to-point link
   773  between the Docker host and one container, which would allow the host to
   774  communicate with that one container on some single IP address and thus
   775  communicate “out-of-band” of the bridge that connects the other, more
   776  usual containers.  But unless you have very specific networking needs
   777  that drive you to such a solution, it is probably far preferable to use
   778  `--icc=false` to lock down inter-container communication, as we explored
   779  earlier.
   780  
   781  ## Editing networking config files
   782  
   783  Starting with Docker v.1.2.0, you can now edit `/etc/hosts`, `/etc/hostname`
   784  and `/etc/resolve.conf` in a running container. This is useful if you need
   785  to install bind or other services that might override one of those files.
   786  
   787  Note, however, that changes to these files will not be saved by
   788  `docker commit`, nor will they be saved during `docker run`.
   789  That means they won't be saved in the image, nor will they persist when a
   790  container is restarted; they will only "stick" in a running container.