如何在 Docker 容器中解决 Apache 运行 中的 503 服务不可用?

How to cure 503 Service Unavailable in Apache running in Docker Container?

我正在尝试在 Docker 容器中创建一个 Apache 虚拟主机代理(我在 Docker 1.6 上),所以我做了以下操作:

首先设置两个 Docker 容器,每个 运行 自己的网络应用程序:

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

运行 apachectl -Sapache_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 上的 container/website cicd.example.com:8000 应该代理到端口 8002

上的 container/website

相反,我收到以下错误:

503 Service Unavailable

检查日志我得到以下信息:

[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-server 配置师。我只知道危险。

编辑

根据建议,我尝试了以下方法:

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 错误(在其他答案中建议)

您的另外两个容器不是本地主机,而是分别

  • 建造:80
  • cicd:80

在你的 apache 代理中反映出来,你应该可以开始了

我真正需要记住的是,一旦我们传入请求,我们就在 Docker 的网络中工作,因此使用 ProxyPass / http://localhost:8002/ 之类的东西将不起作用,因为 'localhost' 属于到我们发出请求的 Docker 容器。

所以我开始在错误框外搜索,可以这么说,然后找到了这个答案 From inside of a Docker container, how do I connect to the localhost of the machine?

我确定的是我们需要将请求传递给 Docker 网络。为了从服务器的命令行中获取该信息,我 运行 sudo ip addr show docker0 returns:

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>