Skip to main content

Development

Docker Bootcamp – Networking Part 2

map

Welcome back to Docker Bootcamp. In this post, we’ll take another look at networking. In the first post on networking, we looked at the different types of network and container isolation types available through docker. Here we will map ports in the container back to the host and connect to services running inside the container.

Terminology

  • 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

  • inspect – Display all the metadata about an image or container in JSON format
  • logs – View logs for a container
  • run – Create and start a new container
  • port – List port mappings for a 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

–expose O Expose a port or range of ports (does not automatically publish to the host)
–publish -p O Publish an exposed port to a specified ip:port or port on the host
–publish-all -P O Publish all exposed ports to random ports on the host (helps avoid manual port mapping conflicts)

Examples

 

Publish – None

  • Create and start a detached container
    • docker run -d –name ngnone nginx
  • View container logs
    • docker logs ngnone
      • The container is running and ready for startup
  • View port mappings
    • docker port ngnone
      • No ports are returned
  • View container metadata
    • docker inspect ngnone
      • Under Config/ExposedPorts, you can see the container declares that it listens on port 80
      • Under HostConfig/PortBindings, you can see an empty list
      • Under NetworkSettings/Ports, you can see port 80 is null
  • Open a browser and navigate to http://localhost
    • Page is not found
  • Find network settings and available host IP addresses
    • ipconfig
      • Note the ip4 address for each of your connected network adapters
  • Open a browser and navigate to the http://ipaddress
    • Do this for each of the available IP addresses on your machine
      • Page is not found each time
  • Stop a running container
    • docker stop ngnone

Publish – All Host IP Addresses

  • Create and start a detached container and map exposed port to a specific host port
    • docker run -d -p 8080:80 –name ngnpub1 nginx
  • View container logs
    • docker logs ngpub1
      • The container is running and ready for startup
  • View port mappings
    • docker port ngpub1
      • The list of port mappings is displayed
      • Port 80 on the container is mapped to 0.0.0.0:8080 on the host (all available host IP addresses)
  • View container metadata
    • docker inspect ngpub1
      • Under Config/ExposedPorts, you can see the container declares that it listens on port 80
      • Under HostConfig/PortBindings, you can see the container port 80 is mapped to host port 8080
      • Under NetworkSettings/Ports, you can see the container port 80 is mapped to host port 8080
  • Open a browser and navigate to http://localhost:8080
    • View Nginx welcome page
  • Find network settings and available host ip addresses
    • Ipconfig
      • Note the ip4 address for each of your connected network adapters
  • Open a browser and navigate to the http://ipaddress:8080
    • Do this for each of the available ip addresses on your machine
      • View Nginx welcome page each time
  • Stop a running container
    • docker stop ngpub1

Publish – Specific Host IP Addresses

  • Find network settings and available host ip addresses
    • ipconfig
      • Choose one ip4 address to use for this example
  • Create and start a detached container and map exposed port to a specific host port on a specific host ip address
    • docker run -d -p <specified host ip>:8181:80 –name ngnpub2 nginx
  • View container logs
    • docker logs ngpub2
      • The container is running and ready for startup
  • View port mappings
    • docker port ngpub2
      • The list of port mappings is displayed
      • Port 80 on the container is mapped to <specified host ip>:8181 on the host
  • View container metadata
    • docker inspect ngpub2
      • Under Config/ExposedPorts, you can see the container declares that it listens on port 80
      • Under HostConfig/PortBindings, you can see the container port 80 is mapped to host port 8181 on the specified host ip
      • Under NetworkSettings/Ports, you can see the container port 80 is mapped to host port 8181 on the specified host ip
  • Open a browser and navigate to http://localhost:8181
    • Page is not found
  • Open a browser and navigate to the http://ipaddress:8181
    • Do this for each of the available ip addresses on your machine
      • View Nginx welcome page only for the specified host ip
  • Stop a running container
    • docker stop ngpub2

Publish – All Exposed Ports

  • Create and start a detached container and map the exposed port to an unspecified host port
    • docker run -d -P –name ngall nginx
  • View container logs
    • docker logs ngall
      • The container is running and ready for startup
  • View port mappings
    • docker port ngall
      • The list of port mappings is displayed
      • Port 80 on the container is mapped to 0.0.0.0:<random port number> on the host (all available host ip addresses)
  • View container metadata
    • docker inspect ngpub1
      • Under Config/ExposedPorts, you can see the container declares that it listens on port 80
      • Under HostConfig/PortBindings, you can see an empty list
      • Under NetworkSettings/Ports, you can see the container port 80 is mapped to host port <random port number>
  • Open a browser and navigate to http://localhost:<random port number>
    • View Nginx welcome page
  • Find network settings and available host ip addresses
    • Ipconfig
      • Note the ip4 address for each of your connected network adapters
  • Open a browser and navigate to the http://ipaddress:<randm port number>
    • Do this for each of the available ip addresses on your machine
      • View Nginx welcome page each time
  • Stop a running container
    • docker stop ngall
  • Create and start a detached container and map exposed port to an unspecified host port
      • docker run -d -P –name ngall2 nginx
  • View port mappings
    • docker port ngall2
      • Notice the container port 80 is mapped to a different port than the previous container
  • Stop a running container
    • docker stop ngall2

Expose a Port

  • Create and start an interactive container
    • docker run -it –name bbconceal busybox /bin/sh
  • View container metadata
    • docker inspect bbconceal
      • Under Config/ExposedPorts, you can see this key does not exist
      • Under HostConfig/PortBindings, you can see an empty list
      • Under NetworkSettings/Ports, you can this key does not exist
    • By default busybox does not expose any ports
  • Create a folder on the host to serve as the wwwroot volume
    • <path_to_folder>\wwwroot
  • Create an index.html file inside the host wwwroot folder
    • Edit the file and add basic hello world html
  • Create and start an interactive container, attach a volume, expose a port and map exposed port to a specified host port
    • docker run -it -v <path to folder>\wwwroot:/var/www/html –expose 80 -p 8282:80 –name bbexpose busybox /bin/sh
  • View container metadata
    • docker inspect bbexpose
      • Under Config/ExposedPorts, this key now exists and declares that it listens on port 80
      • Under HostConfig/PortBindings, this list now is populated and maps port 80 to host port 8282
      • Under NetworkSettings/Ports, this key now exists and declares that it maps port 80 to host port 8282
  • Start the web server inside the container
    • httpd -p 80 -h /var/www/html
      • Run the web server on port 80 and set the document root to the mounted volume
  • Open a browser and navigate to the http://localhost:8282
    • View the basic hello world HTML you created
  • Stop running containers
    • docker stop bbconceal bbexpose

After running these examples, you should have an understanding of how containers reveal themselves to the host and the various way to manage the mapping between containers and the host machine. Don’t forget to clean up your containers.  Join me next time when we modify network configurations inside our 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
TwitterLinkedinFacebookYoutubeInstagram