Development

Docker Bootcamp – Networking Part 3

configuration

Welcome back to Docker Bootcamp.  In the previous post on networking, we mapped ports in the container back to the host and connected to services running inside the container.  Here we will set custom DNS and hostnames. Using these options modifies the network configurations inside the container in various ways.

Terminology

  • Domain Name Service (DNS) – A protocol for mapping user-friendly names to IP addresses.
  • Host file – A file that maps hostnames to IP addresses for use on the local system.
  • IP Address – Represents the location of a network interface on a network. Containers are assigned a unique IP address at startup and lose the IP when stopped.
  • 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.

Commands

  • 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/.

run

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

–add-host O Add a custom host-to-IP mapping (controls outbound traffic from the container)
–dns O Set custom DNS servers (defaults to the nameserver of the host)
–hostname O Set the hostname of the container (defaults to the short container id if not specified)

 

Examples

 

No Host Flags

  • Create, start and run an interactive container
    • docker run -it –name nohost busybox /bin/sh
  • Try to ping an external network interface
    • ping docker.com
      • Notice ping results
      • Note the ip address
  • List running containers
    • docker ps
      • Note the container id (short 12 character name is displayed)
  • View hosts file
    • cat /etc/hosts
      • Notice the line “x.x.x.x <short container id>”
      • Docker automatically adds a hostfile entry for the container id that points to the ip of the container
  • Try to ping the container by id
    • ping <short container id>
      • Notice ping results
      • The response comes from the IP address of the container
  • View the dns info
    • cat /etc/resolv.conf
      • Notice the message that DNS requests are forwarded to the host

Add Host

  • Create, start and run an interactive container with a hostname mapping
    • docker run -it –add-host docker.com:127.0.0.1 –name addhost busybox /bin/sh
  • View hosts file
    • cat /etc/hosts
      • Notice the line “127.0.0.1 docker.com”
  • Try to ping an external network interface
    • ping docker.com
      • Notice ping results
      • Note the ip address points to the local loopback interface as defined with the add-host parameter

Hostname

  • Create, start and run an interactive container with a specified hostname
    • docker run -it –hostname hosttest –name hostname busybox /bin/sh
  • View hosts file
    • cat /etc/hosts
      • Notice the line “x.x.x.x hosttest”
      • Notice the line “x.x.x.x <short container id>” is not present
  • Try to ping the container by hostname
    • ping hosttest
      • Notice ping results
      • The response comes from the ip address of the container

DNS

  • Create, start and run an interactive container with a specified DNS server
    • docker run -it –dns 8.8.8.8 –name hostname busybox /bin/sh
      • This is google’s public dns server
  • View the dns info
    • cat /etc/resolv.conf
      • Notice the nameserver 8.8.8.8

By setting various flags when we create a container, we are able to modify how the container is identified on the network, how it handles outbound traffic and how it resolves domain names.  Be sure to stop and remove your containers to have a clean workspace for the next lesson.

About the Author

More from this Author

Leave a Reply

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

Subscribe to the Weekly Blog Digest:

Sign Up
Categories