在当今的容器化世界中,Docker 已成为部署和管理应用程序的流行解决方案。一种常见的用例是将网络流量从主机上的特定端口转发到 Docker 容器内的端口。当您想要将容器内运行的服务公开给外界或需要设置反向代理以进行负载平衡时,这尤其有用。
在本文中,我们将引导您完成使用 iptables(大多数 Linux 发行版中提供的强大而灵活的防火墙实用程序)将端口转发到 Docker 容器所需的步骤。
步骤1:识别Docker容器的IP地址
运行以下命令查找 Docker 容器的 IP 地址:
|
docker inspect -f '{{ .NetworkSettings.IPAddress }}' <CONTAINER_NAME_OR_ID>
|
Replace with the name or ID of your container. Note down the IP address displayed in the output.
第2步:将端口转发到Docker容器
使用 iptables
要使用 iptables 将端口转发到 Docker 容器,可以按照以下步骤操作:
-
Add iptables rules for forwarding: Now, set up the iptables rules to forward the desired port to your Docker container. Replace <HOST_PORT> with the port number on the host machine, <CONTAINER_IP> with the IP address of your container, and <CONTAINER_PORT> with the port number inside the container.
|
sudo iptables -t nat -A 预路由 -p tcp --dport <主机端口> -j DNAT --to-目的地 <CONTAINER_IP>:<CONTAINER_PORT>
sudo iptables -t nat -A 后布线 -j 假面舞会
|
例如,如果要将端口 8080 从主机重定向到 IP 为 172.21.0.7 的 Docker 容器上的端口 80,则需要执行给定的命令:
sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 172.21.0.7:80
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
-
保存 iptables 规则:要使 iptables 规则在重新启动后仍然存在,您可以使用 iptables-save 命令保存它们:
sudo iptables-save > /etc/iptables/rules.v4
请注意,规则文件的路径可能会因您的 Linux 发行版而异。
-
使 iptables 规则持久化:如果您使用基于 Debian/Ubuntu 的发行版,您可以安装 iptables-persistent 软件包以使规则在重新启动后仍然存在:
sudo apt-get install iptables-persistent
安装过程中会询问是否保存当前的iptables规则。选择‘Yes’保存规则。
使用防火墙cmd
要使用firewall-cmd(它是基于 RHEL/CentOS/Fedora 的系统上的 firewalld 实用程序的一部分)将端口转发到 Docker 容器,请执行以下步骤:
-
添加firewall-cmd规则进行转发:现在,设置firewall-cmd规则以将所需的端口转发到您的Docker容器。将 替换为主机上的端口号,将 替换为容器的 IP 地址,将 替换为容器内的端口号。
首先,在活动区域上启用伪装:
sudo firewall-cmd --zone=public --add-masquerade --permanent
-
Next, create the port forwarding rule:
|
sudo firewall-cmd --zone=public --add-forward-port=port=<主机端口>:proto=tcp:toaddr=<CONTAINER_IP>:toport=<CONTAINER_PORT> --永恒的
|
例如,要将主机上的端口 8080 转发到 IP 地址为 172.17.0.7 的 Docker 容器上的端口 80,您可以使用以下命令:
sudo firewall-cmd --zone=public --add-forward-port=port=8080:proto=tcp:toaddr=172.17.0.7:toport=80 --permanent
-
重新加载防火墙规则:添加规则后,您需要重新加载firewalld配置以使更改生效:
sudo firewall-cmd --reload
现在,端口转发应该可以工作,并且任何到达主机上 的流量都将转发到 Docker 容器上的 。
使用UFW
要使用 ufw(简单防火墙)(Ubuntu 和其他基于 Debian 的系统上的默认防火墙工具)将端口转发到 Docker 容器,您需要执行以下步骤:
-
Enable UFW forwarding: Edit the UFW configuration file at /etc/default/ufw:
sudo nano /etc/default/ufw
找到包含 DEFAULT_FORWARD_POLICY=”DROP” 的行并将其更改为 DEFAULT_FORWARD_POLICY=”ACCEPT”:
DEFAULT_FORWARD_POLICY="ACCEPT"
保存文件并退出编辑器。
-
Configure IP forwarding: Edit the sysctl configuration file at /etc/sysctl.conf:
sudo nano /etc/sysctl.conf
添加或取消注释以下行:
保存文件并退出编辑器。通过运行以下命令应用更改:
sudo sysctl -p
-
Update UFW rules: First, create a new UFW rule file for port forwarding. Replace <HOST_PORT> with the port number on the host machine, <CONTAINER_IP> with the IP address of your container, and <CONTAINER_PORT>> with the port number inside the container.
sudo nano /etc/ufw/before.rules
在文件开头、标题注释之后添加以下行:
*nat
:PREROUTING ACCEPT [0:0]
-A PREROUTING -p tcp --dport <HOST_PORT> -j DNAT --to-destination <CONTAINER_IP>:<CONTAINER_PORT>
COMMIT
例如,转发端口8080在主机上连接到端口80在 IP 地址为的 Docker 容器上172.17.0.7,您可以添加:
*nat
:PREROUTING ACCEPT [0:0]
-A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 172.17.0.7:80
COMMIT
保存文件并退出编辑器。
-
重新加载UFW:最后,重新加载 UFW 配置以使更改生效:
sudo ufw disable
sudo ufw enable
现在,端口转发应该可以工作,并且任何到达主机上 的流量都将转发到 Docker 容器上的 。
结论
总之,使用 iptables 将端口转发到 Docker 容器是一个简单的过程,涉及识别容器的 IP 地址、添加必要的 iptables 规则,并确保规则在重新启动后仍然存在。通过执行本文中概述的步骤,您可以确保发送到主机上特定端口的流量无缝重定向到容器内的相应端口。这种方法允许您公开容器内运行的服务、设置反向代理或实现负载平衡等用例,从而提高容器化基础设施的灵活性和效率。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)