Docker port mapping for network access

  • 2020-06-19 12:06:23
  • OfStack

Docker runs the container and finds no IP and no ports. How do you access the container?

Let me introduce docker to achieve network access through port mapping

1. Access container applications externally

When starting the container, network applications and services inside the container cannot be accessed through the network outside the container without specifying corresponding parameters.
When you run a number of network applications in the container and want external access to these applications, you can specify the port map with the -ES12en or -ES13en parameter.
Let's start with p and P

-ES18en can specify the ports to map to, and only 1 container can be bound on 1 specified port -P it randomly maps 1 port to the open network port inside the container (range unknown, seems to be tens of thousands)

So let me just say one, on my side

client ip address is 192.168.0.225

registry ip address is 192.168.0.216:5000


docker run -d -it --name nginx -P 192.168.0.216:5000/nginx
docker ps -a      # Check to see if the container is running  
docker logs nginx    # To view nginx Of the container log

The formats supported by port mapping are:


ip:hostport:containerport  # The specified ip , specify the host port , specify the container port
ip::containerport      # The specified ip , no host is specified port , specify the container port
hostport:container     # Is not specified ip port , specify the host port , specify the container port   
 

2. Map all interfaces ip

The following command looks at 0.0.0.0 listening on port 5000


docker run -d -it -p 5000:5000 --name registry 192.168.0.216:5000/registry
docker ps -a | grep 5000   

Multiple ports can be bound using the -ES46en tag multiple times


docker run -d -it -p 800:80 -p 8088:8080 --name tomcat 192.168.0.216:5000/tomcat
docker ps -a | egrep "(80|8080)"

3. Maps to the specified port of the specified address

Map the container's 80 port to 192.168.0.225:800


docker run -d -it -p 192.168.0.225:800:80 --name web_server 192.168.0.216:5000/nginx
curl 192.168.0.225:800   # Verify that the default home page is accessible 

4. Map to udp port at the specified address

There is only one way of mapping, not to mention whether it is accessible. No offense, everybody


docker run -d -it -p 192.168.0.225:300:3000/udp --name unknown 192.168.0.216:5000/nginx
netstat -anpu |grep udp
udp  0  0  192.168.0.225:300  0.0.0.0:*   24588/docker-proxy

5. View the map port configuration

docker port container_ID allows you to see which ports and protocols are mapped to the container


[root@docker ~]#docker port tomcat
8080/tcp -> 192.168.0.225:32769
[root@docker ~]#docker port unknown
3000/udp -> 192.168.0.225:300

Related articles: