Docker command: port

The Docker port command allows you to print the public port for a port binding in Docker. It is a useful tool for inspecting network settings and accessing exposed ports on a container.

Docker command: port
Docker command: port

Docker Command: port

In this blog post, we'll explore the port command in Docker and how it can be used to print the public port for a port binding. The port command is a useful tool for inspecting the network settings of a running container and accessing the exposed ports.

The Docker Port Command

The port command is used to print the public port of a port binding in Docker. It is a simple yet powerful command that allows you to view the exposed ports on a running container.

To use the port command, you need to have a running container with one or more ports exposed. The command takes the container name or ID and the private port number as arguments.

Here's the syntax of the port command:

docker port [OPTIONS] CONTAINER [PRIVATE_PORT[/PROTO]]

The OPTIONS parameter allows you to customize the output format. You can use options like --format to specify a Go template for the output, --no-trunc to display the full port number, and --no-resolve to disable resolving the container's IP address to its hostname.

The PRIVATE_PORT parameter is the port number inside the container that you want to print the public port for. If the port is exposed with a specific protocol, you can specify the protocol using the PROTO parameter. The most common protocols are TCP and UDP.

Printing the Public Port

Let's say you have a container running a web server that exposes port 80. To print the public port for this port binding, you can use the following command:

docker port container_name_or_id 80

This command will output the IP address of the host machine and the public port mapped to the container's port 80.

If you want to display the full port number and disable hostname resolution, you can use the following command:

docker port --no-trunc --no-resolve container_name_or_id 80

This will display the complete port number instead of truncating it and will not resolve the container's IP address to its hostname.

Customizing the Output Format

The port command provides flexibility in customizing the output format using the --format option. This option allows you to specify a Go template to control the output.

For example, if you only want to display the public port number, you can use the following command:

docker port --format "{{.PublicPort}}" container_name_or_id 80

This command will output only the public port number, making it easier to parse and use in scripts or other automation tasks.

Conclusion

The port command in Docker is a handy tool for inspecting the network settings of a running container. By using this command, you can easily print the public port for a port binding and access the exposed ports.

Remember to have a running container with the desired ports exposed before using the port command. Also, don't forget to customize the output format using the --format option if needed.

That's all for this blog post. We hope you found it helpful and that it assists you in understanding and using the port command effectively in your Docker workflow. Happy containerizing!