Docker allows to provide network services through external access to containers or interconnection between containers. After the container is started, some network applications can be run in the container, and the port mapping is specified through the -p or -P parameter.
1) When starting the container, select a port to map to the open port inside the container
-p Lowercase p means docker will select a specific host port to map to the open network port inside the container superior.
-P Capital P means that docker will randomly select a host port and map it to the open network port inside the container.
[root@docker-test ~]# docker run -ti -d --name my-nginx -p 8088:80 docker.io/nginx 2218c7d88ccc917fd0aa0ec24e6d81667eb588f491d3730deb09289dcf6b8125 [root@docker-test ~]# docker run -ti -d --name my-nginx2 -P docker.io/nginx 589237ceec9d5d1de045a5395c0d4b519acf54e8c09afb07af49de1b06d71059 [root@docker-test ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 589237ceec9d docker.io/nginx "nginx -g 'daemon ..." 6 seconds ago Up 5 seconds 0.0.0.0:32770->80/tcp my-nginx2 2218c7d88ccc docker.io/nginx "nginx -g 'daemon ..." About a minute ago Up About a minute 0.0.0.0:8088->80/tcp my-nginx
It can be seen from the above:
Use -p when starting the container my-nginx, select the specific 8088 port of the host machine and map it to the 80 port inside the container, visit http:// localhost/8088 is enough
Use -P when starting the container my-nginx2, select a random port of the host and map it to port 80 inside the container, the random port here is 32770, visit http:// localhost/32770
For more related tutorials, please pay attention to the docker tutorial column on the PHP Chinese website.
The above is the detailed content of How to map docker port to host machine. For more information, please follow other related articles on the PHP Chinese website!