Jun 19, 2018

Docker: Network configuration - none / brigde / hostname / dns entries

If you are starting your docker container you can add some network configuration details via command line.
Let's start with the easiest network setting:
docker run -it  --net none alpine /bin/ash
This setting starts the container without any connectivity to the network:
# ip addr show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
The default is --net bridge:
docker run -it  --net bridge alpine /bin/ash
With this setting your network access is via a bridge of your host:
# ip addr show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
7: eth0@if8:  mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
This is the docker0 interface on your docker server machine:
alpine:~# ip addr show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:72:ae:ef brd ff:ff:ff:ff:ff:ff
    inet 192.168.178.46/24 brd 192.168.178.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe72:aeef/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0:  mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:ba:e9:4d:6a brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:baff:fee9:4d6a/64 scope link 
       valid_lft forever preferred_lft forever
4: docker_gwbridge:  mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:62:f0:92:82 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.1/16 scope global docker_gwbridge
       valid_lft forever preferred_lft forever
Finally you can configure your hostname and manipulate dns entries:
# docker run -it  --net bridge  --hostname myhostname --add-host mygoogle.com:8.8.8.8  alpine /bin/ash
/ # hostname
myhostname
/ # nslookup mygoogle.com
nslookup: can't resolve '(null)': Name does not resolve

Name:      mygoogle.com
Address 1: 8.8.8.8 mygoogle.com

No comments:

Post a Comment