简体   繁体   English

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

[英]Can't reach a local website through apache in a docker container (windows & docker toolbox)

I built a Docker image with apache2 and a little configuration in order to run a local web server on windows.我用 apache2 和一些配置构建了一个 Docker 镜像,以便在 Windows 上运行本地 Web 服务器。

Here is the Dockerfile:这是 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

In apache-config.conf, I just have a little virtual host:在 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>

In my /c/Users/public/sites/test folder, I just have an index.php file with the following code in it:在我的 /c/Users/public/sites/test 文件夹中,我只有一个 index.php 文件,其中包含以下代码:

<?php
phpinfo();

I built my image with this command in Docker Quickstart Terminal我在 Docker Quickstart Terminal 中使用此命令构建了我的图像

docker build -t php56 .

Then I built my container:然后我构建了我的容器:

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

The container is well created but I get the following 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
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: '

My problem is that I can't access my test website and I don't know why.我的问题是我无法访问我的测试网站,我不知道为什么。

I tried to add the "ServerName 172.17.0.2" directive in /etc/apache2/apache2.conf, but when I restart apache2 service, it stops my container from running.我试图在 /etc/apache2/apache2.conf 中添加“ServerName 172.17.0.2”指令,但是当我重新启动 apache2 服务时,它会停止我的容器运行。

I tried to inspect my container to see its ip, but sometimes it doesn't have one sometimes it's 172.17.0.2我试图检查我的容器以查看它的 ip,但有时它没有,有时它是 172.17.0.2

I tried the following adresses in my browser but I can't reach anything:我在浏览器中尝试了以下地址,但无法访问任何内容:

http://test.loc http://test.loc

172.17.0.2:80 172.17.0.2:80

0.0.0.0:80 0.0.0.0:80

Does someone knows how to run a webserver with Docker on windows and reaching a site with his Servername?有人知道如何在 Windows 上使用 Docker 运行网络服务器并使用他的服务器名访问站点吗? (test.loc) (测试.loc)

something you need to know is that when you run docker on windows its not actual running on your windows machine but the docker toolbox creates a virtual box VM in wich it runs docker.您需要知道的是,当您在 Windows 上运行 docker 时,它实际上并没有在您的 Windows 机器上运行,而是 docker 工具箱创建了一个虚拟机 VM,它在其中运行 docker。 You can read more on this here https://docs.docker.com/engine/installation/windows/ so you can check your website on the ip the virtual box VM has.您可以在此处阅读更多相关信息https://docs.docker.com/engine/installation/windows/这样您就可以在 virtual box VM 拥有的 ip 上查看您的网站。 To find out the ip address of the vm you can use the following command要找出虚拟机的 ip 地址,您可以使用以下命令

docker-machine ip

Add this row on Dockerfile在 Dockerfile 中添加这一行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM