Docker networking
Docker containers will often need to communicate to other servers. Networking will enable this.
Network design is part of deployment architecture and can help with container isolation.
Bridge networks
Section titled “Bridge networks”A bridge network is a subnet on the host, e.g. 172.20.0.0/16
Creating a network
Section titled “Creating a network”To add a new network named example_network with a subnet 172.20.0.0/16, run:
$ docker network create --subnet 172.20.0.0/16 example_networkb6df28662f17b32787e770a5404820178e3968af277cbe6e873ff97cbaf944c9This creates the network on the host,
Running a container in a network
Section titled “Running a container in a network”To start a container inside a given network, use the --network option, for example:
$ docker container run --network example_network httpd:2.4AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.20.0.2.It will be assigned an IP within the network’s subnet.
If the --network option is not specified then it will be run in Docker’s default bridge network.
To start a container with a specific IP and network, use the --ip and --network options.
$ docker container run --network example_network --ip 172.20.0.55 ubuntu cat /etc/hosts127.0.0.1 localhost::1 localhost ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters172.20.0.55 cdbce911318dThe above will start a new ubuntu container and assign it IP 172.20.0.55. The /etc/hosts file shows config that
docker added into the container.
References
Section titled “References”- Networking overview
docker network --help