docker networks

concepts for private and public comms in containers

  • review of docker container run -p
  • for local dev/testing, networks usually "just work"
  • quick port check with docker container port <container>
  • learn concepts of docker networking
  • understand how network packets move around docker

  • each container uses a priv virt net "bridge"

  • each virt net routes through nat firewall
  • all containers on a virt net can talk without -p
  • "batteries included, but removable"
    • defaults work well in many cases, but easy to swap out parts to customise it
  • make new virt nets
  • attach containers to more than one virt net
  • skip virt nets and use host (--net=host)
  • use different docker network drivers to gain new abilities
docker container run -p 80:80 --name webhost -d nginx
docker container port webhost
  • --format - a common option for formatting the output of commands using 'go templates'
    docker container inspect --format '{{ .NetworkSettings.IPAddress }}' webhost

cli management of virtual networks

command description
docker network ls show networks
docker network inspect inspect a network
docker network create --driver create a network
docker network connect attach a network to a container
docker network disconnect detach a network from a container
  • bridge - default docker network
  • host - skip virtual networks but sacrifices security
  • none - removes eth0 leaving on localhost interface in container
docker network create <network_name>
  • uses bridge driver by default
docker container run -d --name <container_name> --network <network_name> <image>

using connect and disconnect you can add more networks or change networks, like plugging additional nics or switching eth cables.

dns and how container find each other

  • understand how dns is the key (can't rely on ips)
    • can't use ip addresses because so dynamic
  • see how it works by default with custom networks
  • learn how to use --link to enable dns on default bridge network

  • containers on the same network have automatically dns resolution with container name

  • default bridge network does not have dns by default
    • can use --link when starting containers to link in default bridge network