【Self-study Docker】Docker port command

Docker port command

overview

insert image description here

docker port command tutorial

The docker port command can be used to list the port mapping of a specified Docker container , or map a port in a container to the host. The CONTAINER after this command can be the container ID or the container name.

docker port syntax

haicoder(www.haicoder.net)# docker port [OPTIONS] CONTAINER [PRIVATE_PORT[/PROTO]]

the case

no port mapping

Use the docker run -it -d command to start a dokcer container.

haicoder(www.haicoder.net)# docker run --name haicoder -it -d centos
c32d8f59d0df17a87f3a2523da576b67fbcbf9b533dcd0be7c71913ade41e419

Use the docker port command to list the port mappings for the docker container.

haicoder(www.haicoder.net)# docker port haicoder
haicoder(www.haicoder.net)# 

At this point, the terminal does not output any port mappings.

Use **docker kill** and docker rm commands to remove all containers.

haicoder(www.haicoder.net)# docker kill `docker ps -qa` ; docker rm `docker ps -aq`

list port mappings

Use the docker run -it -d command to start a dokcer container.

haicoder(www.haicoder.net)# docker run -it -d --name haicoder -p 8081:80 centos
e50208fb8f874f0454e014b3494fe84172cfe8fc072f76cb2601db06652532e8

Use the docker port command to list the port mappings for the docker container.

haicoder(www.haicoder.net)# docker port haicoder

At this point, the terminal output is as follows:

Please add a picture description

That is, port 80 inside the docker container is exported to port 8081 of the host.

Use the docker kill and docker rm commands to remove all containers.

haicoder(www.haicoder.net)# docker kill `docker ps -qa` ; docker rm `docker ps -aq`

List port mappings for the specified protocol

Use the docker run -it -d command to start a dokcer container.

haicoder(www.haicoder.net)# docker run -it -d --name haicoder -p 8081:80 centos
0b18b91e1be6a643bc77a119701e350732f506783227426fd57c703f2674edcc

Use the docker port command to list the port mapping of the tcp protocol of the docker container.

haicoder(www.haicoder.net)# docker port haicoder 80/tcp

At this point, the terminal output is as follows:

Please add a picture description

That is, only the port mapping based on the tcp protocol is listed at this time, if you replace '80/tcp' with '80/udp' in the above command, no port mapping will be output.

Use the docker kill and docker rm commands to remove all containers.

haicoder(www.haicoder.net)# docker kill `docker ps -qa` ; docker rm `docker ps -aq`

docker port command summary

The docker port command can be used to list the port mapping of a specified Docker container, or map a port in a container to the host.

Use the form of docker port PRIVATE_PORT[/PROTO] to list the port mapping of the specified protocol.

Guess you like

Origin blog.csdn.net/weixin_41384860/article/details/128784481