[英]How to cure 503 Service Unavailable in Apache running in Docker Container?
我正在尝试在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>
然后将它们都放在apache_proxy
容器的/etc/apache2/sites-available/
中。
现在,我回到apache_proxy
容器并执行以下操作:
a2ensite build.example.conf
a2ensite cicd.example.conf
service apache2 restart
从apache_proxy
容器的命令行运行apachectl -S
,我看到以下效果有效:
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代理容器中。
我还应声明自己不是网络专家或Web服务器配置大师。 我只知道危险。
根据建议,我尝试了以下方法:
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错误(建议其他答案)
您的其他两个容器不是本地主机,而是
在你的Apache代理中反映出来,你应该很好
我真正需要记住的是,一旦传入请求,我们就在Docker的网络中工作,因此无法使用ProxyPass / http://localhost:8002/
类的东西,因为“ localhost”属于Docker容器,我们已经提出了要求。
因此,我可以从错误框外开始搜索,可以这么说,并得到这个答案。 从Docker容器内部,如何连接到计算机的本地主机?
我确定的是,我们需要将请求传递给Docker网络。 为了获取该信息,我从服务器的命令行运行了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上,将通行证更改为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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.