English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When working on a project, the dynamically mapped container ports of docker are forIt is necessary to record the information of dynamically mapped container ports of docker, for future reference.
Docker comes with the EXPOSE command, which can be used by writing a dockerfile and}}-The p parameter is convenient for mapping the internal port of the container, but for a running container, if you want to open a new port for external access, you need to edit the dockerfile and rebuild it, which is not very convenient.
In fact, docker itself uses iptables for port mapping, so we can implement dynamic port mapping of running containers through some simple operations.
You can see the specific port mapping by running the iptables command (the IP in the following example is192.168.42.41container has opened22and2028(0th port)
[yaxin@ubox ~]$sudo iptables -nvxL Chain INPUT (policy ACCEPT 262 packets, 529689 bytes) pkts bytes target prot opt in out source destination 14355 789552 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:25 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 5479459 653248187 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0 93990 314970368 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 4705395 2183219154 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 282 packets, 622495 bytes) pkts bytes target prot opt in out source destination Chain DOCKER (1 references) pkts bytes target prot opt in out source destination 218 13193 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 192.168.42.41 tcp dpt:22280 4868186 297463902 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 192.168.42.41 tcp dpt:20280 78663 13128102 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 192.168.42.41 tcp dpt:22 47 4321 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 192.168.42.41 tcp dpt:28159 [yaxin@ubox ~]$sudo iptables -t nat -nvxL Chain PREROUTING (policy ACCEPT 210199 packets, 14035875 bytes) pkts bytes target prot opt in out source destination 1219483 82563968 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL Chain INPUT (policy ACCEPT 197679 packets, 13316595 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 271553 packets, 16671466 bytes) pkts bytes target prot opt in out source destination 1643 99251 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL Chain POSTROUTING (policy ACCEPT 271743 packets, 16682594 bytes) pkts bytes target prot opt in out source destination 13468 811013 MASQUERADE all -- * !docker0 192.168.42.0/24 0.0.0.0/0 0 0 MASQUERADE tcp -- * * 192.168.42.41 192.168.42.41 tcp dpt:22280 0 0 MASQUERADE tcp -- * * 192.168.42.41 192.168.42.41 tcp dpt:20280 0 0 MASQUERADE tcp -- * * 192.168.42.41 192.168.42.41 tcp dpt:22 0 0 MASQUERADE tcp -- * * 192.168.42.41 192.168.42.41 tcp dpt:28159 Chain DOCKER (2 references) pkts bytes target prot opt in out source destination 22 1404 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22280 to:192.168.42.41:22280 288 17156 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:20280 to:192.168.42.41:20280 93 5952 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22222 to:192.168.42.41:22 8 512 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:28159 to:192.168.42.41:28159
What we need to do is to configure the iptables rules according to the situation of our container.
Firstly, the filter table, we need to configure it to allow forwarding. By default, docker has already placed all the FORWARD rules into the DOCKER built-in chain (chain), which is convenient for configuration and viewing.
Then configure the nat table to set up DNAT, which is the core configuration for port forwarding. This table only needs to configure POSTROUTING and DOCKER chains, and why this configuration is done is not discussed here. If you want to learn more about iptables, please google it.
Here is an example:
Suppose I have a container named nginx (you can query by running the docker ps command), now I am running the nginx program inside docker, listening to8888Port, I hope the external network can access8899Port (note the port) access
Find the IP assigned to nginx by docker
[yaxin@ubox ~]$sudo docker inspect -f '{{.NetworkSettings.IPAddress}}' nginx 192.168.42.43
Configure the FORWARD (DOCKER) chain in the filter table of iptables
[yaxin@ubox ~]$sudo iptables -A DOCKER ! -i docker0 -o docker0 -p tcp --dport 8888 -d 192.168.42.43 -j ACCEPT
Configure the PREROUTING (DOCKER) and POSTROUTING chains in the nat table of iptables
[yaxin@ubox ~]$sudo iptables -t nat -A POSTROUTING -p tcp --dport 8888 -s 192.168.42.43 -d 192.168.42.43 -j MASQUERADE [yaxin@ubox ~]$sudo iptables -t nat -A DOCKER ! -i dokcer0 -p tcp --dport 8899 -j DNAT --to-destination 192.168.42.43:8888
Access through the external network curl http://IP:8899It will display the html page configured under nginx
Last iptables rule
[yaxin@ubox ~]$sudo iptables -nvxL Chain INPUT (policy ACCEPT 67893 packets, 212661547 bytes) pkts bytes target prot opt in out source destination 14364 790008 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:25 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 5479682 653269356 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0 94186 314986910 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 4705658 2183254076 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 71253 packets, 222512872 bytes) pkts bytes target prot opt in out source destination Chain DOCKER (1 references) pkts bytes target prot opt in out source destination 218 13193 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 192.168.42.41 tcp dpt:22280 4868186 297463902 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 192.168.42.41 tcp dpt:20280 78663 13128102 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 192.168.42.41 tcp dpt:22 47 4321 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 192.168.42.41 tcp dpt:28159 27 4627 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 192.168.42.43 tcp dpt:8888 [yaxin@ubox ~]$sudo iptables -t nat -nvxL Chain PREROUTING (policy ACCEPT 232 packets, 16606 bytes) pkts bytes target prot opt in out source destination 1220281 82620790 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL Chain INPUT (policy ACCEPT 216 packets, 15671 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 317 packets, 19159 bytes) pkts bytes target prot opt in out source destination 1644 99311 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL Chain POSTROUTING (policy ACCEPT 321 packets, 19367 bytes) pkts bytes target prot opt in out source destination 13512 813656 MASQUERADE all -- * !docker0 192.168.42.0/24 0.0.0.0/0 0 0 MASQUERADE tcp -- * * 192.168.42.41 192.168.42.41 tcp dpt:22280 0 0 MASQUERADE tcp -- * * 192.168.42.41 192.168.42.41 tcp dpt:20280 0 0 MASQUERADE tcp -- * * 192.168.42.41 192.168.42.41 tcp dpt:22 0 0 MASQUERADE tcp -- * * 192.168.42.41 192.168.42.41 tcp dpt:28159 0 0 MASQUERADE tcp -- * * 192.168.42.43 192.168.42.43 tcp dpt:8888 Chain DOCKER (2 references) pkts bytes target prot opt in out source destination 22 1404 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22280 to:192.168.42.41:22280 288 17156 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:20280 to:192.168.42.41:20280 93 5952 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22222 to:192.168.42.41:22 8 512 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:28159 to:192.168.42.41:28159 4 208 DNAT tcp -- !dokcer0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8899 to:192.168.42.43:8888
Of course, manual configuration is also quite麻烦, so I wrote a script to automatically configure port mapping, the usage is described in the script
#!/bin/bash # filename: docker_expose.sh if [ `id -u` -ne 0 ];then echo "[ERROR] Please use root to run this script" exit 23 fi if [ $# -ne 3 );then echo "Usage: $0 <container_name> <add|del> [[<machine_ip>:]<machine_port>:]<container_port>"/<protocol_type>"}} exit 1 fi IPV4_RE='(([0-9]|1-9][0-9]|1[0-9}{2]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|1-9][0-9]|1[0-9}{2]|2[0-4][0-9]|25[0-5])' container_name=$1 action=$2 arguments=$3 # Check action if [ "$action"x != "add"x -a "$action"x != "del"x ];then echo '[ERROR] Please use add or del parameter to add port map or delete port map' exit 654 fi if [ "$action"x == "add"x ];then action="A" else action="D" fi # Get container IP by container name container_ip=`docker inspect -f '{{.NetworkSettings.IPAddress}}' $container_name 2> /dev/null` if [ -z $container_ip ];then echo '[ERROR] Get container's (${container_name}) IP error, please ensure you have this container' exit 2 fi # Parse arguments protocol_type=`echo $arguments | awk -F '/' '{print $2}'` if [ -z $protocol_type ];then protocol_type="tcp" fi # Check protocol if [ "$protocol_type"x != "tcp"x -a "$protocol_type"x != "udp"x ];then echo '[ERROR] Only tcp or udp protocol is allowed' exit 99 fi machine_ip='' machine_port='' container_port='' # Split the left arguments arguments=${arguments%/*} machine_ip=`echo $arguments | awk -F ':' '{print $1}'` machine_port=`echo $arguments | awk -F ':' '{print $2}'` container_port=`echo $arguments | awk -F ':' '{print $3}'` if [ -z $machine_port ];then # arguments is: 234 container_port=$machine_ip machine_port=$machine_ip unset machine_ip elif [ -z $container_port ];then # arguments is: 234:456 container_port=$machine_ip machine_port=$machine_port unset machine_ip fi # check port number function _check_port_number() { local port_num=$1 if ! echo $port_num | egrep "^[0-9]+$" &> /dev/null;then echo "[ERROR] Invalid port number $port_num" exit 3 fi if [ $port_num -gt 65535 -o $port_num -lt 1 );then echo "[ERROR] Port number $port_num is out of range(1-56635)" exit 4 fi } # check port and IP address _check_port_number $container_port _check_port_number $machine_port if [ ! -z $machine_ip ];then if ! echo $machine_ip | egrep "^${IPV4_RE}$" &> /dev/null;then echo "[ERROR] Invalid IP Address $machine_ip" exit 5 fi # check which interface binds the IP for interface in `ifconfig -s | sed -n '2',1'`;do interface_ip=`ifconfig $interface | awk ''/inet addr}}/{print substr($2,6)}'` if [ "$interface_ip"x == "$machine_ip"x ];then interface_name=$interface break fi done if [ -z $interface_name ];then echo "[ERROR] Can not find interface bind with $machine_ip" exit 98 fi fi # run iptables command echo "[INFO] Now start to change rules to iptables" echo "[INFO] Changing POSTROUTING chain of nat table" iptables -t nat -${action} POSTROUTING -p ${protocol_type} --dport ${container_port} -s ${container_ip} -d ${container_ip} -j MASQUERADE if [ -z $interface_name ];then echo "[INFO] Changing DOCKER chain of filter table" iptables -${action} DOCKER ! -i docker0 -o docker0 -p ${protocol_type} --dport ${container_port} -d ${container_ip} -j ACCEPT echo "[INFO] Changing DOCKER chain of nat table" iptables -t nat -${action} DOCKER ! -i docker0 -p ${protocol_type} --dport ${machine_port} -j DNAT --to-destination ${container_ip}:${container_port} else echo "[INFO] Changing DOCKER chain of filter table" iptables -${action} DOCKER -i $interface_name -o docker0 -p ${protocol_type} --dport ${container_port} -d ${container_ip} -j ACCEPT echo "[INFO] Changing DOCKER chain of nat table" iptables -t nat -${action} DOCKER -i $interface_name -p ${protocol_type} --dport ${machine_port} -j DNAT --to-destination ${container_ip}:${container_port} fi
Thank you for reading, I hope it can help everyone. Thank you for your support of this site!