Skip to main content

Development

Docker Bootcamp – Networking Part 4

bootcamp

Welcome back to Docker Bootcamp.  In the previous post on networking, we modified our container’s network configurations using flags on the create or run command. Here we will create a user-defined network and set a specific IP address on our container.

Terminology

  • Classless Inter-Domain Routing (CIDR) – A notation representing a network identifier and the number of bits available for the host identifier in the subnet.
    • 198.51.100.0/24 – Uses 24 bits for the network identifier and 8 bits for the host identifier. Addresses 198.51.100.0 – 198.51.100.255 belong in this range.
    • 198.51.0.0/16 – Uses 16 bits for the network identifier and 16 bits for the host identifier. Addresses 198.51.0.1 – 198.51.255.255 belong in this range.
  • Gateway – The entry and exit point all traffic that flows across a network.
  • IP Address – Represents the location of a network interface on a network. For IPv4 this is a 32 bit number.  For IPv6 this is a 128 bit number.
  • Network Interface – Represents a location and an address of a network device.
  • Port – Represents the sender or recipient of a message on the network. The sender and recipient are applications that listen on the specified port waiting for a request and return a response.
  • Subnet – A logical division of an ip network. (For more information, see https://en.wikipedia.org/wiki/Subnetwork).

Commands

  • network – Docker subcommand to manage networks
  • network connect – Connect a container to a network
  • network create – Create a network
  • network disconnect – Disconnect a container from a network
  • network inspect – Display detailed network information
  • network ls – List networks
  • network rm – Remove a network
  • run – Create and start a new container

Command Details

You can find a full list of commands and all available flags at https://docs.docker.com/engine/reference/commandline/docker/.

network connect

docker network connect [options] network container

–ip O Set IPv4 address on the container
–ip6 O Set IPv6 address on the container

network create

docker network create [options] network

–gateway O IPv4 or IPv6 Gateway for the master subnet
–subnet O Set the subnet that represents the network segment in CIDR format

network disconnect

docker network disconnect [options] network container

–force -f O Force the container to disconnect from a network

network disconnect

docker network inspect network

network ls

docker network ls

network rm

docker network rm network

run

docker run [options] image [command] [args…]
Outputs: ContainerID

–ip O Set IPv4 address on the container (only works on a user defined network)
–ip6 O Set IPv6 address on the container (only works on a user defined network)

 

Examples

Set up

  • View available networks
    • docker network ls
  • Inspect network details
    • docker network inspect bridge
      • Notice the IPAM/Config section and note the subnet value.
      • Docker defaults to 172.17.0.0/16 subnet and 172.17.0.1 gateway
      • The first container started is assigned the ip 172.17.0.2

Create a User-Defined Network

  • Create a user defined network
    • docker network create mynet
  • Inspect network details
    • docker network inspect mynet
      • Notice the IPAM/Config section. The subnet and gateway are set for you.
  • Create a user-defined network with a defined subnet
    • Docker network create –subnet 192.168.100.0/24 mysubnet
  • Inspect network details
    • Docker network inspect mysubnet
      • Notice the IPAM/Config section. The subnet matches what you defined.

Connect to Default Network

  • Create, start and run an interactive container on the default bridge network with an assigned ip
    • docker run -it –network bridge –ip 172.17.100.100 –name netbridgeip busybox /bin/sh
      • Use an ip that is in the range for docker’s default bridge network
      • Notice the error “user specified IP is supported on user defined networks only”
  • Create, start and run an interactive container on the default bridge network
    • docker run -it –network bridge –name netbridge busybox /bin/sh
  • Inspect network details
    • docker network inspect bridge
      • Notice the Containers section lists the name and ip address of the connected containers.
  • Inspect container details
    • docker inspect netbridge
      • Notice the NetworkSettings/Networks section lists the network name and ip address of the container.

Connect to User-Defined Network

  • Create, start and run an interactive container on a user defined network with an assigned ip
    • docker run -it –network mynet –ip 172.20.100.100 –name netuser1 busybox /bin/sh
      • Use an IP that is in the range for your network
      • Notice the error “user specified IP is supported on when connecting to networks with user defined subnets”
  • Create, start and run an interactive container on the default bridge network
    • docker run -it –network mynet –name netuser busybox /bin/sh
  • Inspect network details
    • docker network inspect mynet
      • Notice the Containers section lists the name and ip address of the connected containers.
  • Inspect container details
    • docker inspect netuser
      • Notice the NetworkSettings/Networks section lists the network name and ip address of the container.

Connect to User-Defined Network With Subnet

  • Create, start and run an interactive container on a user defined network with an assigned ip
    • docker run -it –network mysubnet –ip 192.168.100.100 –name netsubuser busybox /bin/sh
      • Use an ip that is in the range for your network with defined subnet
  • Inspect network details
    • docker network inspect mysubnet
      • Notice the Containers section lists the name and ip address of the connected containers.
  • Inspect container details
    • docker inspect netsubuser
      • Notice the NetworkSettings/Networks section lists the network name and ip address of the container.

Modify Networks of a Running Container

  • Connect a container to a network
    • docker network connect mysubnet netuser
  • Inspect network details
    • docker network inspect mysubnet
      • Notice the Containers section lists the name and ip address of the connected containers.
  • Inspect container details
    • docker inspect netuser
      • Notice the NetworkSettings/Networks section lists the network names and ip address of the container on the named network.
  • Check network settings inside the netuser container
    • ip addr show
      • Notice two ethernet adapters are listed (eth0 and eth1)
      • Notice the ip addresses match what is listed for the network and container details.
  • Disconnect a container from a network
    • docker network disconnect mysubnet netuser
  • Inspect container details
    • docker inspect netuser
      • Notice the NetworkSettings/Networks section lists the network names and ip address of the container on the named network.

Cleanup

  • Remove all containers
    • docker rm -f netbridgeip netbridge netuser1 netuser netsubuser
  • Remove networks
    • docker network rm mynet mysubnet

We now have more understanding of how Docker manages network connections. We can create our own subnets and assign specific IP addresses for our containers. We’ll use this in the next post to revisit linking multiple containers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Eric Sanner, Solutions Architect

More from this Author

Categories
Follow Us