Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set Port Mapping in Docker

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

I believe many inexperienced people don't know what to do about how to set up port mapping in Docker. This paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

I. concept

Docker port mapping is to map the service ports applied in the container to the host machine.

II. Realization

When some network applications are running in the container, the port mapping can be specified by-P or-p parameters to allow external access to these applications.

1. Random mapping

When using the-P parameter, Docker will randomly map a port to a network port open in the internal container, and start a nginx service as follows:

$docker run-d-P nginxe93349d539119dc48dc841e117f6388d6afa6a6065b75a5b4aedaf5fb2a051fc$ $docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe93349d53911 nginx "nginx-g'daemon." 11 seconds ago Up 9 seconds 0.0.0.0 daemon 32769-> 80/tcp zen_kirch

Using docker ps, we can see that port 32769 of the local host is mapped to port 80 of the container, and the nginx welcome page appears when we access http://localhost:32769 through the local browser.

two。 Designated port

When using the-p parameter, you can specify the port to map, and only one container can be bound on a specified port. Supported formats are:

IP:HostPort:ContainerPort

IP:ContainerPort

HostPort:ContainerPort

Open a nginx service to map native port 8080 to port 80 of the container:

$docker run-d-p 8080 nginx 80 nginx23e725098712d061a1382f33d6fe54da23ae37597a62f8debdd3731b5f9cc4b9 $$docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES23e725098712 nginx "nginx-g'daemon..." 8 seconds ago Up 6 seconds 0.0.0.0 docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES23e725098712 nginx 8080-> 80/tcp frosty_ptolemy

Using docker ps, we can see that port 8080 of the local host is mapped to port 80 of the container, and the nginx welcome page appears when we access http://localhost:8080 through the local browser.

3. View the mapped port

Use the docker port command to view the currently mapped port configuration, and you can also see the bound address. The format of the command is as follows:

$docker port CONTAINER [PRIVATE_ Port [/ PROTO]]

The container has its own internal network and IP address, and you can use docker inspect + container ID to obtain the specific information of the container.

DOCKER adds a mapped port to a running container

Method 1

1. Get the container IP

Change the container_name to the container name in the real environment

Docker inspect `container_ name` | grep IPAddress

2. Iptable forwarding port

Map port 8000 of the container to port 8001 of the docker host

The copy code is as follows:

Iptables-t nat-A DOCKER-p tcp-- dport 8001-j DNAT-- to-destination 172.17.0.19 tcp 8000

Method 2

1. Submit a running container as an image

Docker commit containerid foo/live

two。 Run the mirror and add a port

Docker run-d-p 8000 foo/live / bin/bash after reading the above, have you mastered how to set up port mapping in Docker? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report