Retrieving the container port

The Docker Engine provides at least three different options to retrieve the container's port binding details. Here, let's first explore the options, and then, move on to dissect the retrieved information. The options are as follows:

  • The docker ps subcommand always displays the port binding details of a container, as shown here:
      $ sudo docker ps
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES
baddba8afa98 apache2:latest
"/usr/sbin/apache2ct
26 seconds ago Up 25 seconds
0.0.0.0:80->80/tcp
furious_carson
  • The docker inspect subcommand is another alternative; however, you have to skim through quite a lot of details. Run the following command:
      $ sudo docker inspect baddba8afa98
  • The docker inspect subcommand displays the port binding related information in three JSON objects, as shown here:
  • The ExposedPorts object enumerates all ports that are exposed through the EXPOSE instruction in Dockerfile, as well as the container ports that are mapped using the -p option in the docker run subcommand. Since we didn't add the EXPOSE instruction in our Dockerfile, what we have is just the container port that was mapped using -p 80:80 as an argument to the docker run subcommand:
              "ExposedPorts": {
"80/tcp": {}
},
  • The PortBindings object is part of the HostConfig object, and this object lists out all the port binding done through the -p option in the docker run subcommand. This object will never list the ports exposed through the EXPOSE instruction in the Dockerfile:
              "PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},

      

  • The Ports object of the NetworkSettings object has the same level of details, as the preceding PortBindings object. However, this object encompasses all ports that are exposed through the EXPOSE instruction in Dockerfile, as well as the container ports that are mapped using the -p option in the docker run subcommand:
              "NetworkSettings": {
"Bridge": "",
"SandboxID":"ID removed for readability",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
},

Of course, the specific port field can be filtered using the --format option of the docker inspect subcommand.

The docker port subcommand enables you to retrieve the port binding on the Docker host by specifying the container's port number:

$ sudo docker port baddba8afa98 80
0.0.0.0:80

Evidently, in all the preceding output excerpts, the information that stands out is the 0.0.0.0 IP address and the 80 port number. The 0.0.0.0 IP address is a meta address, which represents all the IP addresses configured on the Docker host. In effect, the 80 container's port is bound to all the valid IP addresses on the Docker host. Therefore, the HTTP service is accessible through any of the valid IP addresses configured on the Docker host.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset