Forrest logo
back to the docker tool

docker:tldr:f23be

docker: Display all port bindings.
$ docker inspect --format='${range $p, $conf := -NetworkSettings-Ports} ${$p} -> ${(index $conf 0)-HostPort} ${end}' ${container}
try on your machine

The command docker inspect --format='${range $p, $conf := -NetworkSettings-Ports} ${$p} -> ${(index $conf 0)-HostPort} ${end}' ${container} is used to inspect the network settings and ports of a Docker container.

Here's a breakdown of the command:

  • docker inspect: This command is used to obtain detailed information about a Docker container.

  • --format='${range $p, $conf := -NetworkSettings-Ports} ${$p} -> ${(index $conf 0)-HostPort} ${end}': This option allows you to specify a Go template to format the output of docker inspect command. In this case, the template is provided in single quotes (') and is using Go templating syntax.

  • ${range $p, $conf := -NetworkSettings-Ports}: This part of the template initializes a range loop to iterate over each port mapping in the "NetworkSettings-Ports" section of the inspected container's Docker JSON output. The variable $p represents the port number, and $conf represents the port configuration.

  • ${$p} -> ${(index $conf 0)-HostPort}: This part of the template is the output format for each port. It uses the port number ($p) and the HostPort value from the port configuration ($conf). The index function is used to access the first element of the $conf array, which contains the port configuration.

  • ${end}: This indicates the end of the range loop.

  • ${container}: This is the placeholder for the container ID or name that you want to inspect. It is passed as an argument to the docker inspect command.

Overall, this command will output a formatted list of the container's ports and their corresponding host ports.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the docker tool