如何解决在Docker Container中运行的Apache中的503 Service Unavailable?

时间:2017-10-16 21:22:32

标签: apache docker virtualhost http-proxy http-error

我正在尝试在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)

以下是设置的样子: Docker VHost Example

我可以通过它的相应端口到达每个单独的容器,我应该可以访问以下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错误(在其他答案中建议)

2 个答案:

答案 0 :(得分:2)

我真正需要记住的是,一旦我们传递了请求,我们就在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并返回:

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>

答案 1 :(得分:1)

您的另外两个容器不是localhost,而是分别

  • 构建:80
  • CICD:80

在您的apache代理中反映出来,你应该好好去