Docker Networking

Docker Networking

Network Drivers Types

  • bridge - this is the default and most used network driver.
  • host - remove network isolation between host and container, use host’s networking directly. Host mode networking can be useful to optimize performance.
  • overlay - Overlay networks connect multiple Docker daemons together and enable swarm services to communicate with each other.
  • none - disable all networking. Container can’t connect to network. Not used often.

When a container is connected to a network, it can communicate with other containers in the same network.

List Networks

1
docker network ls

default output

1
2
3
4
NETWORK ID          NAME                    DRIVER              SCOPE
cad232899398 bridge bridge local
00cc8b46ebad host host local
ac75839ac3dd none null local

Create a Network

use docker network create to create network. use -d or --driver option to set the driver type

1
docker network create -d bridge my-bridge

You can specify the network’s subnet and gateway. The prefix length is 16 by default, here we can specify a subnet with prefix length of24.

1
docker network create -d bridge --subnet 172.66.66.0/24 --gateway 172.66.66.1  my-bridge

run ifconfig on the host machine, you will see a new network interface added.

1
2
3
4
5
6
7
8
$ ifconfig
br-0a428d94b9d5: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.66.66.1 netmask 255.255.255.0 broadcast 172.66.66.255
ether 02:42:e6:8c:27:40 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Inspect a Network

1
docker network inspect my-bridge

This will show the network information such as Subnet and Gateway information.

It will also contain the containers connected to the network and their IP addresses.

Sample output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ docker network inspect my-bridge
[
{
"Name": "my-bridge",
"Id": "0a428d94b9d528eaf3c52521158e399ec17e7651e98db1eed7570295271bf7db",
"Created": "2019-09-14T04:13:40.981076739-04:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.66.66.0/24",
"Gateway": "172.66.66.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"ac3a42c369ee00afffb5170a1bae619cbaa3ca45b0bd630768e6199c322d123b": {
"Name": "my-alpine",
"EndpointID": "568a17e9c97ef707eec0f37614a630465bdb0634114639ab4abfb9acd40455b8",
"MacAddress": "02:42:ac:42:42:02",
"IPv4Address": "172.66.66.2/24",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]

Remove a Network

1
docker network rm my-bridge

Running a container in a Network

use --net=<network> or --network=<network> option to specify the network. <network> is the name of the network to be used

1
docker run -it --rm --network bridge --name my-alpine alpine

Disconnect from a network

1
docker network disconnect bridget my-alpine

Connect to my-bridge network

1
docker network connect my-bridge my-alpine

The above command will automatically assign an IP address to the container. You can connect to a network with a specified IP address.

1
docker network connect --ip 172.66.66.33 my-bridge  my-alpine

Inspect the container’s network configuration.

1
docker exec my-alpine ifconfig

Example: Run Containers on the Same Network

Here is an example bash script to run both mysql and phpmyadmin container on the same network

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
# Create network
docker network create sample-network

# Create mysql container
docker run -d --name=mysql \
--network=sample-network \
-e MYSQL_ROOT_PASSWORD=password \
-p 3306:3306 \
mysql:8

# Create phpmyadmin container
docker run -d --name phpmyadmin \
--network=sample-network \
-e PMA_HOSTS=mysql-dev \
-e PMA_PORT=3306 \
-e PMA_USER=root \
-e PMA_PASSWORD=password \
-p 7000:80 \
phpmyadmin/phpmyadmin

We first create a bridge network called sample-network. Then create mysql and phpmyadmin container that both connect to sample-network.
You can now open phpmyadmin at http://localhost:7000/

Reference