Container Ports in Air Kingdom

DockerDockerBeginner
Practice Now

Introduction

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker/ContainerOperationsGroup -.-> docker/port("`List Container Ports`") subgraph Lab Skills docker/port -.-> lab-268701{{"`Container Ports in Air Kingdom`"}} end

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

Next, inspect the container to reveal its exposed ports:

docker inspect magic-container | grep HostPort

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

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

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.

Other Docker Tutorials you may like