Detailed docker port mapping (random port, specified IP port, random ip specified port, specified ip random port)

Table of contents

Detailed docker port mapping

1. Overview of port mapping:

2. Case experiment:

1. -P option, random port

2. Use -p to specify the local port to be mapped to.

Local_Port:Container_Port, the specified port of any address

Local_IP:Local_Port:Container_Port maps to the specified port at the specified address

Local_IP::Container_Port is mapped to the specified address, but the host port is randomly assigned

3. Specify the transport protocol: TCP


Detailed docker port mapping

1. Overview of port mapping:

        In Docker, the container cannot communicate with the outside by default. It is necessary to add corresponding parameters to the startup command to allow the container to communicate with the outside world.

        When running a Web service in Docker, it is necessary to map the port of the Web service application in the container to the port of the local host. In this way, if the user accesses the port specified by the host machine, it is equivalent to accessing the Web service port inside the container.

2. Case experiment:

1. -P option, random port

When using

[root@docker ~]# docker run -d -P --name nginx-test1 nginx

Use docker port to view port mapping

[root@docker ~]# docker port nginx-test1

Access test: 192.168.100.131:32768

[root@docker ~]# docker logs nginx-test1

View mapped random port ranges

[root@docker ~]#

cat /proc/sys/net/ipv4/ip_local_port_range

2. Use -p to specify the local port to be mapped to.

Local_Port:Container_Port, the specified port of any address

        The port mapping parameter specifies that 8000 of the host is mapped to port 80 inside the container, and the -p option can be used multiple times

[root@docker ~]# docker run -d -p 8000:80 --name nginx-test2 nginx

Access test: 192.168.100.131:8000

        This method will map to all interface addresses, and all visitors can access the container through all IP ports of the host.

As follows: View the current ip address of the host

Access test: 192.168.59.153:8000

Local_IP:Local_Port:Container_Port  is mapped to the specified port of the specified address

[root@docker ~]# docker run -d -p

192.168.100.131:9000:80 --name nginx-test3 nginx

Access test: 192.168.100.131:9000

Access test: 192.168.59.153:9000

Local_IP::Container_Port  is mapped to the specified address, but the host port is randomly assigned

[root@docker ~]# docker run -d -p 192.168.100.131::80 --name nginx-test4 nginx

[root@docker ~]# docker port nginx-test4

Access test: 192.168.100.131:32770

3. Specify the transport protocol: TCP

[root@docker ~]# docker run -d -p 80:80/tcp --name nginx-test5 nginx

[root@docker ~]# docker port nginx-test5

Access test: 192.168.100.131:80

 

Guess you like

Origin blog.csdn.net/2302_77582029/article/details/132106326