大网数据广告活动
服务器运维问答

Using iptables port redirection with docker

· 52CE 我爱测

I have a Linux server running Jenkins (HTTP on port 8080) and the same server is also running Docker 1.12.1. I used some iptables rules (as per official Jenkins install docs) to redirect port 8080 to the host's port 80, so that d…

问题原文

原帖链接 · davidA · CC BY-SA 3.0

I have a Linux server running Jenkins (HTTP on port 8080) and the same server is also running Docker 1.12.1. I used some iptables rules (as per official Jenkins install docs) to redirect port 8080 to the host's port 80, so that default HTTP would work for Jenkins (i.e. http://myserver rather than http://myserver:8080):

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080

However these rules break Docker's ability to download the correct resources via HTTP during a docker build command. For instance, RUN apt-get update fails with multiple "failed to download" errors. Doing wget http://www.google.com inside the container returns the HTML for the Jenkins main page. HTTP GETs from the host work fine. Removing the iptables rules results in RUN apt-get update working again. So I think those iptables rules are interfering with docker's network mechanism.

Can such a port redirection coexist with Docker? If so, how would one go about writing iptables rules to solve this problem?

已采纳解答

原帖链接 · davidA · CC BY-SA 3.0

A possible modification to the iptables rules is to remove both and replace with:

iptables -t nat -A PREROUTING -p tcp -m tcp -i eth0 --dport 80 -j REDIRECT --to-ports 8080

This will ensure that, for my case, only TCP traffic on port 80 through interface eth0 will be redirected. Traffic originating from a docker container on the host will not be redirected. Note that I've left out the localhost redirection too, although presumably that could be modified to avoid filtering anything originating from interface docker0.

Another solution is to employ a reverse proxy to forward incoming HTTP requests on TCP port 80 to the host's port 8080. I used Caddy with the following simple Caddyfile:

localhost:80
proxy / localhost:8080

With this in place, I can remove the iptables rules entirely.

This seems to provide a simple redirection on port 80, as well as allowing docker containers to use outgoing HTTP normally.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-08-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。