我试图在Docker容器中创建Apache虚拟主机代理(我在Docker 1.6上),因此我做了以下工作:
首先建立两个Docker容器,每个容器运行自己的web应用程序:
docker run -it -p 8001:80 -p 4431:443 --name build ubuntu:latest
apt-get update
apt-get install apache2 libapache2-mod-php -y
echo “<?php phpinfo(); ?>” >> /var/www/html/info.php
service apache2 restart
然后
docker run -it -p 8002:80 -p 4432:443 --name cicd ubuntu:latest
apt-get update
apt-get install apache2 libapache2-mod-php -y
echo “<?php phpinfo(); ?>” >> /var/www/html/info.php
service apache2 restart
其中每一个都在各自的端口上完美地运行。接下来,我创建一个运行Apache的容器:
docker run -it -p 8000:80 -p 4430:443 --name apache_proxy ubuntu:latest
apt-get update
apt-get install apache2 -y
a2enmod proxy
a2enmod proxy_http
service apache2 restart
在8000端口,这是完全独立的。
然后,我为其他的Docker容器创建了一个虚拟主机文件:
<VirtualHost *:80>
ServerName build.example.com
<Proxy *>
Allow from localhost
</Proxy>
ProxyPass / http://localhost:8001/
</VirtualHost>
和
<VirtualHost *:80>
ServerName cicd.example.com
<Proxy *>
Allow from localhost
</Proxy>
ProxyPass / http://localhost:8002/
</VirtualHost>
然后将两者都放在/etc/apache2/sites-available/
的apache_proxy
容器中。
现在,我回到了apache_proxy
容器并执行了以下操作:
a2ensite build.example.conf
a2ensite cicd.example.conf
service apache2 restart
在apachectl -S
容器的命令行中运行apache_proxy
,我可以看到以下内容是有效的:
VirtualHost configuration:
*:80 is a NameVirtualHost
default server 172.17.0.17 (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost 172.17.0.17 (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost build.example.com (/etc/apache2/sites-enabled/build.example.conf:1)
port 80 namevhost cicd.example.com (/etc/apache2/sites-enabled/cicd.example.conf:1)
下面是设置的样子:
我可以通过其各自的端口到达每个单独的容器,我应该能够访问以下URL来访问相应的站点:
build.example.com:8000应代理8001港口的集装箱/网站,cicd.example.com:8000应代理8002港口的集装箱/网站。
相反,我得到了以下错误:
503服务不可用
查看日志,我得到以下信息:
[Mon Oct 16 21:17:32.510127 2017] [proxy:error] [pid 165:tid 140552167175936] (111)Connection refused: AH00957: HTTP: attempt to connect to 127.0.0.1:8001 (localhost) failed
[Mon Oct 16 21:17:32.510278 2017] [proxy:error] [pid 165:tid 140552167175936] AH00959: ap_proxy_connect_backend disabling worker for (localhost) for 0s
[Mon Oct 16 21:17:32.510302 2017] [proxy_http:error] [pid 165:tid 140552167175936] [client 172.26.16.120:61391] AH01114: HTTP: failed to make connection to backend: localhost
[Mon Oct 16 21:17:32.799053 2017] [proxy:error] [pid 166:tid 140552217532160] (111)Connection refused: AH00957: HTTP: attempt to connect to 127.0.0.1:8001 (localhost) failed
[Mon Oct 16 21:17:32.799232 2017] [proxy:error] [pid 166:tid 140552217532160] AH00959: ap_proxy_connect_backend disabling worker for (localhost) for 0s
[Mon Oct 16 21:17:32.799256 2017] [proxy_http:error] [pid 166:tid 140552217532160] [client 172.26.16.120:61392] AH01114: HTTP: failed to make connection to backend: localhost, referer: http://build.example.com:8000/info.php
在过去的几个小时里,我一直在忙着让这件事发挥作用,现在我肯定我错过了一些很简单的东西。有人能揭露我的错误吗?
备注
我遵循了一个关于SELinux的巨大漏洞,它没有启用,甚至没有真正安装在Ubuntu/Apache容器中。
我也应该声明,我不是一个网络专家或主网站服务器配置者。我只知道足够危险。
编辑
根据建议,我尝试了以下几点:
ProxyPass / http://cicd:80/
导致502错误
ProxyPass / http://ip.address.of.server:8002/
超时
ProxyPass / http://ip.address.of.container:8002/
导致503个错误
ProxyPass / http://127.0.0.1:8002/ retry=0
导致503个错误(在其他答案中建议)
发布于 2017-10-17 15:18:16
我真正需要记住的是,一旦传入请求,我们就在Docker的网络中工作,所以使用像ProxyPass / http://localhost:8002/
这样的东西是行不通的,因为‘localhost’属于我们请求所在的Docker容器。
因此,我开始在错误框外搜索,可以这么说,并找到了这个答案From inside of a Docker container, how do I connect to the localhost of the machine?。
我决定的是我们需要把请求传递给码头网络。为了获取该信息,我从服务器的命令行运行了sudo ip addr show docker0
,它返回:
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
inet 172.17.42.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::5484:7aff:fefe:9799/64 scope link
valid_lft forever preferred_lft forever
显示Docker的内部网络在172.17.42.1上,将pass更改为ProxyPass / http://172.17.42.1:8002/
,将请求交给Docker网络,然后成功。
<VirtualHost *:80>
ServerName cicd.example.com
<Proxy *>
#Allow from localhost
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://172.17.42.1:8002/ retry=0
</VirtualHost>
https://stackoverflow.com/questions/46779313
复制相似问题