无法通过 docker 容器中的 apache 访问本地网站(Windows & docker 工具箱)



我用apache2和一些配置构建了一个Docker镜像,以便在windows上运行本地web服务器。

这是Dockerfile:

FROM php:5.6.15-apache
RUN apt-get update && apt-get install -y 
apt-utils vim git php5-mysql php5-memcache php5-memcached php5-intl 
sendmail aptitude wget
RUN apt-get install libapache2-mod-php5 -y -o Dpkg::Options::="--force-confdef"
RUN docker-php-ext-install mbstring
RUN docker-php-ext-install pdo pdo_mysql
RUN apt-get install libcurl4-gnutls-dev -y
RUN docker-php-ext-install curl
RUN apt-get update && apt-get install -y zlib1g-dev libicu-dev g++
RUN docker-php-ext-configure intl
RUN docker-php-ext-install intl
RUN a2enmod rewrite
ENV APACHE_RUN_USER test
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
EXPOSE 80
COPY php.ini /usr/local/etc/php/php.ini
COPY apache-config.conf /etc/apache2/sites-enabled/000-default.conf

在apache-config.conf中,我只有一个小虚拟主机:

<VirtualHost *:80>
  ServerName test.loc
  DocumentRoot /var/www/html/test
  <Directory /var/www/html/test/>
      Allow from all
  </Directory>
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

在我的/c/Users/public/sites/test文件夹中,我只有一个index.php文件,其中包含以下代码:

<?php
phpinfo();

我在Docker Quickstart Terminal 中用这个命令构建了我的图像

docker build -t php56 .

然后我建造了我的集装箱:

docker run --name=php56_container -t --rm -v "//c/Users/Public/sites:/var/www/html" -p 80:80 -p 8080:8080 php56

容器创建得很好,但我得到了以下消息:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2.
Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2.
Set the 'ServerName' directive globally to suppress this message
[Mon Dec 21 14:18:24.968215 2015] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) configured -- resuming normal operations
[Mon Dec 21 14:18:24.968982 2015] [core:notice] [pid 1] AH00094: Command line: '

我的问题是我无法访问我的测试网站,我不知道为什么

我试图在/etc/apache2/apache2.conf中添加"ServerName 172.17.0.2"指令,但当我重新启动apache2服务时,它会阻止我的容器运行。

我试着检查我的集装箱,看看它的ip,但有时它没有,有时它是172.17.0.2

我在浏览器中尝试了以下地址,但无法访问任何内容:

http://test.loc

172.17.0.2:80

0.0.0.0:80

有人知道如何在windows上使用Docker运行Web服务器,并使用他的Servername访问网站吗?(test.loc)

您需要知道的是,当您在windows上运行docker时,它并不是在您的windows机器上实际运行的,而是docker工具箱创建了一个虚拟机,它在其中运行docker。你可以在这里阅读更多https://docs.docker.com/engine/installation/windows/所以你可以在虚拟机的ip上查看你的网站。要找到vm的ip地址,可以使用以下命令

docker-machine ip

在Dockerfile 上添加此行

RUN echo "ServerName www.example.com" >> /etc/apache2/apache2.conf

最新更新