Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker port command

The docker port command is used to display the public-facing ports that are mapped to the private ports within a running container. This can be helpful when you want to understand the network connections of a container, especially when using dynamic port mapping. In this tutorial, we'll cover the basics of using the docker port command.

Syntax:

docker port CONTAINER [PRIVATE_PORT[/PROTO]]
  • CONTAINER: The name or ID of the container whose port mappings you want to view.
  • PRIVATE_PORT: (Optional) The private port number within the container. If specified, only the mapping for this port will be displayed.
  • PROTO: (Optional) The protocol (tcp or udp) for the port mapping. If not specified, the default is "tcp".

Examples:

  1. Display all port mappings for a container:

    docker port my_container
    

    Replace my_container with the name or ID of the container you want to view port mappings for.

  2. Display the mapping for a specific port:

    docker port my_container 8080
    

    In this example, the command will show the public-facing port that is mapped to the private port 8080 within the my_container.

  3. Display the mapping for a specific port and protocol:

    docker port my_container 53/udp
    

    This command will show the public-facing port that is mapped to the private port 53 using the UDP protocol within the my_container.

The output of the docker port command is in the following format:

PRIVATE_PORT/PROTO -> HOST_IP:HOST_PORT

For example, an output of 8080/tcp -> 0.0.0.0:32768 means that the private port 8080 within the container (using TCP protocol) is mapped to the public-facing port 32768 on the host.

This tutorial should give you a basic understanding of how to use the docker port command to view port mappings for your running containers. This can be helpful for understanding the network connections and accessibility of your containerized applications.

  1. How to Use Docker Port Command:

    • Description: The docker port command is used to view the public-facing ports of a running container.

    • Code Example:

      docker port my_container
      
  2. Viewing Exposed Ports with Docker Port:

    • Description: docker port shows the public-facing ports mapped to the container's internal ports.

    • Code Example:

      docker port my_container
      
  3. Docker Port Command Options and Flags:

    • Description: The docker port command typically does not have additional options or flags.

    • Code Example:

      docker port my_container
      
  4. Mapping Ports in Docker Containers:

    • Description: Ports can be mapped using the -p option during container creation to expose container ports to the host.

    • Code Example:

      docker run -p 8080:80 my_web_app
      
  5. Docker Port and Container Networking:

    • Description: Docker port is used to display port information, irrespective of the container's network mode.

    • Code Example:

      docker port my_container
      
  6. Docker Port and Host Binding:

    • Description: Binding ports to the host allows external access to the container's services.

    • Code Example:

      docker run -p 8080:80 my_web_app
      
  7. Inspecting Port Information with Docker Port:

    • Description: Use docker port to inspect the public-facing ports of a running container.

    • Code Example:

      docker port my_container
      
  8. Security Considerations with Docker Port:

    • Description: Exercise caution when exposing ports, ensuring only necessary ports are accessible from outside the container.

    • Code Example:

      docker run -p 127.0.0.1:8080:80 my_secure_web_app
      
  9. Docker Port and Multiple Containers:

    • Description: docker port can be used for multiple containers to view their exposed ports.

    • Code Example:

      docker port container1 container2
      
  10. Automating Port Management with Docker Port:

    • Description: Automate port management using scripts or tools, especially in dynamic environments.

    • Code Example (Script):

      # script.sh
      docker port my_container
      
  11. Docker Port and Container Discovery:

    • Description: Discover exposed ports of running containers for integration or orchestration purposes.

    • Code Example:

      docker port my_container
      
  12. Troubleshooting Docker Port Issues:

    • Description: Troubleshoot port-related issues using docker port to identify port mappings.

    • Code Example:

      docker port my_troubled_container
      
  13. Docker Port vs Docker Inspect:

    • Description: docker port specifically shows port mappings, while docker inspect provides detailed container information.

    • Code Example (Inspect):

      docker inspect my_container
      
  14. Managing Container Ports in Docker-Compose:

    • Description: Docker Compose allows specifying port mappings in the service configuration.

    • Code Example (docker-compose.yml):

      version: '3'
      services:
        web:
          image: my_web_app
          ports:
            - "8080:80"