DEV Community

Cover image for Mastering Container Ports in the Magical Air Kingdom
Labby for LabEx

Posted on

Mastering Container Ports in the Magical Air Kingdom

Introduction

MindMap

In this lab, you will venture into the magical realm of the floating Air Kingdom, where you will take on the role of an Air Kingdom mechanic. Your objective is to master the art of listing container ports on the mystical Docker platform to ensure smooth and efficient communication between containers.

Explore Container Ports

In this step, you will dive into the enchanted world of Docker and learn how to list container ports using the command line interface.

First, let's start by spinning up a mystical container named "magic-container" using the alpine image:

docker run --name magic-container -d -p 80:80 alpine sleep 1000
Enter fullscreen mode Exit fullscreen mode

Next, inspect the container to reveal its exposed ports:

docker inspect magic-container | grep HostPort
Enter fullscreen mode Exit fullscreen mode

List All Running Containers Ports

In this step, you will expand your knowledge by listing the ports of all running containers in the Air Kingdom.

Start by listing all the running containers to gather the container IDs:

docker ps -q
Enter fullscreen mode Exit fullscreen mode

Then, iterate through the running containers and display their respective ports:

for container_id in $(docker ps -q); do
  docker inspect -f '{{.Name}} - {{range $p, $conf := .NetworkSettings.Ports}}{{$p}} -> {{(index $conf 0).HostPort}}{{end}}' $container_id
done
Enter fullscreen mode Exit fullscreen mode

Summary

In this lab, you embarked on a magical journey through the Air Kingdom to master the art of listing container ports in Docker. By exploring various Docker commands, you gained the ability to uncover the mystical connections between containers and their exposed ports, ensuring seamless communication within the kingdom. With this newfound knowledge, you are now prepared to wield the power of Docker port management with confidence and precision.


🚀 Practice Now: Container Ports in Air Kingdom


Want to Learn More?

Top comments (0)